Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Assignment #3: The Social Class For this project you will write a class Person. A Person has a name (assume a first name only,

Programming Assignment #3: The Social Class For this project you will write a class Person. A Person has a name (assume a first name only, with no spaces) and friends. Store the names of the friends in a string, separated by spaces. Provide a constructor that constructs a person with a given name and no friends. Provide the following methods: public String getName() public void befriend(Person p) public void unfriend(Person p) public String getFriendNames() public int getNumberOfFriends() Functional Requirements The befriend and unfriend methods ought to be one-directional. In other words, typing Alicebefriend(bob) should add Bob to Alices list of friends, but not Alice to Bobs list. To do the latter, youll have to call bob. befriend(Alice), as I do in my unit tester. (Note that this requirement is here to make your programming job easier. It takes more work to make the befriend method symmetrically; that is, to make Alice. befriend(bob) simultaneously add Bob to Alices list and Alice to Bobs list.) When run alongside the main class in the unit tester I have posted on Moodle, you should get results equivalent to the results I recorded in the screenshot (see the following page). The class should be tested a few different ways, for I will also test it with a different unit tester that I am not posting on Moodle. Methodological Requirements Every class, every instance variable, every constructor, every method, and every parameter/return value should have a comment. No more requirements just make it work! Advice Try using the replace method of the String class within your unfriend method.

package project3;

/** * This is a unit tester for the Person class. * A fictitious friendship scenario is drawn up, * with some drama, and then everyone takes stock at the end. * @author ndaxvig */ public class PersonTester {

/** * The main method executes the unit tester. * @param args the command line arguments */ public static void main(String[] args) { // let's conjure up some people Person alice = new Person("Alice"); Person bob = new Person("Bob"); Person charlie = new Person("Charlie"); Person doug = new Person("Doug"); Person eve = new Person("Eve"); Person francine = new Person("Francine"); // friendships are made alice.befriend(eve); eve.befriend(alice); alice.befriend(charlie); charlie.befriend(alice); alice.befriend(doug); doug.befriend(alice); bob.befriend(doug); doug.befriend(bob); charlie.befriend(francine); francine.befriend(charlie); //check to see whether things are working as expected System.out.println("Alice has " + alice.getNumberOfFriends() + " friends: " + alice.getFriendNames()); System.out.println("Alice SHOULD have 3 friends: Eve Charlie Doug "); System.out.println("Bob has " + bob.getNumberOfFriends() + " friends: " + bob.getFriendNames()); System.out.println("Bob SHOULD have 1 friends: Doug "); System.out.println("Charlie has " + charlie.getNumberOfFriends() + " friends: " + charlie.getFriendNames()); System.out.println("Charlie SHOULD have 2 friends: Alice Francine "); System.out.println("Doug has " + doug.getNumberOfFriends() + " friends: " + doug.getFriendNames()); System.out.println("Doug SHOULD have 2 friends: Alice Bob "); System.out.println("Eve has " + eve.getNumberOfFriends() + " friends: " + eve.getFriendNames()); System.out.println("Eve SHOULD have 1 friends: Alice "); System.out.println("Francine has " + francine.getNumberOfFriends() + " friends: " + francine.getFriendNames()); System.out.println("Francine SHOULD have 1 friends: Charlie "); System.out.println("------------------------"); System.out.println("Now some drama ensues..."); System.out.println("------------------------"); //drame ensues, destroying friendships and forging new alliances... alice.unfriend(charlie); charlie.unfriend(alice); francine.unfriend(charlie); charlie.unfriend(francine); francine.befriend(doug); doug.befriend(francine); //check again to make sure things are working as expected System.out.println("Alice has " + alice.getNumberOfFriends() + " friends: " + alice.getFriendNames()); System.out.println("Alice SHOULD have 2 friends: Eve Doug "); System.out.println("Bob has " + bob.getNumberOfFriends() + " friends: " + bob.getFriendNames()); System.out.println("Bob SHOULD have 1 friends: Doug "); System.out.println("Charlie has " + charlie.getNumberOfFriends() + " friends: " + charlie.getFriendNames()); System.out.println("Charlie SHOULD have 0 friends: "); System.out.println("Doug has " + doug.getNumberOfFriends() + " friends: " + doug.getFriendNames()); System.out.println("Doug SHOULD have 3 friends: Alice Francine Bob "); System.out.println("Eve has " + eve.getNumberOfFriends() + " friends: " + eve.getFriendNames()); System.out.println("Eve SHOULD have 1 friends: Alice "); System.out.println("Francine has " + francine.getNumberOfFriends() + " friends: " + francine.getFriendNames()); System.out.println("Francine SHOULD have 1 friends: Doug "); } }

image text in transcribedimage text in transcribed

public class PersonTester { private String name; // store the name of the person private String friends; // Stores the name of friends separated by space /** * Constructor * @param firstName the first name of the person */ public PersonTester(String firstName) { // set name name = firstName; // initialise friends to empty string friends = ""; }

/** * Returns the name of the person * @return the name of the person */ public String getName() { return name; }

/** * Add a given person to the friend list of this person * @param p Person to befriend */ public void befriend(Person p) { // get name of p String pName = p.getName(); // add to friends String followed by space friends = friends + pName + " "; }

/** * Removes a person from friendlist if he exists * @param p Person to unfriend */ public void unfriend(Person p) { // get name of p String pName = p.getName(); // repace name and a space by empty String friends.replace("pName" + " ", ""); }

/** * Get the name of all friends separated by space * @return String */ public String getFriendNames() { return friends; }

/** * Retuens the number of friends * @return int Number of friends */ public int getNumberOfFriends() { // Split friends names along spaces. // number of word is the number of frinds return friends.split(" ").length; } }

HnX Proi Love Girl The Ne.. WAN Other bookmarks Projects pdf.pdf 2/2 93% + Alice has 3 friendar Eve Charlie Doug Alice SHOULD have 3 friends: Eve Charlie Doug Bob has 1 friends: Doug Bob SHOULD have 1 friends: Doug Charlie has 2 friends: Alice Francine Charlie SHOULD have 2 friends: Alice Prancine Doug has 2 friends: Alice Bob Doug SHOULD have 2 friends: Alice Bob Eve has 1 friends: Alice Eve SHOULD have 1 friends: Alice Francine has 1 friends! Charlie Francine SHOULD have 1 friends: Charlie 1 Now some drama enauea... 2 Alice has 2 friends: Eve Doug Alice SHOULD have 2 friends: Eve Doug Bob has 1 friends: Doug Order does not matter Bob SHOULD have 1 friends Doug Charlie has 0 friends! in your lists - only the - Charlie SHOULD have O friends: contents do Doug has 3 friends: Alice Bob Francine Doug SHOULD have 3 friends: Alice Francine Bob Eve has 1 friends: Alice Eve SHOULD have 1 friends: Alice Prancine has 1 friends: Doug Francine SHOULD have 1 friends: Doug BUILD SUCCESSFUL (total time: 0 seconds) O BE c Other bookmarks Project3 (2).pdf 1/2 86% + CSC 125 Axig! 1 Programming Assignment #3: The Social Class For this project you will write a class Person A Person has a name assume a first name only, with no spaces) and friends. Store the names of the friends in a string, separated by spaces. Provide a constructor that constructs a person with a given name and no friends. Provide the following methods: public String getName> . public void berriendPerson public void untriend (Forson ) . public String getFriendNames) public int Get Number of Friends Functional Requirements The e befriend and unfriend methods ought to be one-directional. In other words, typing alace.befriend bobl should add Bob to Alice's list of friends, but not Alice to Bob's list. To do the latter, you'll have to call bob.befriend{alicol, as I do in my unit tester. (Note that this requirement is here to make your programming job easier. It takes more work to make the befriend method symmetric that is, to make alice.befriend(bob) simultaneously add Bob to Alce's list and Alice to Bob's list) When run alongside the main class in the unit tester I have posted on Moodle, you should get results equivalent to the results I recorded in the screenshot (see the following page). The class should be tested a few different ways, for I will also test it with a different unit tester I am not posting on Moodle. Methodological Requirements Every class, every Instance variable, every constructor, every method, and every parameter/return value should have a comment. No more requirements - just make it work! Advice Try using the replace method of the String class within your unfriend method

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Demystifying Databases A Hands On Guide For Database Management

Authors: Shiva Sukula

1st Edition

8170005345, 978-8170005346

More Books

Students also viewed these Databases questions

Question

1. Television more Over watching faceing of many problems ?

Answered: 1 week ago

Question

Is there a link between chronic stress and memory function?

Answered: 1 week ago