Computer science Class design hw problem for JAVA. Class Design: Person The Pers
ID: 3832813 • Letter: C
Question
Computer science Class design hw problem for JAVA.
Class Design: Person
The Person class is intended to be an abstract and simplified representation of a person.
Each person will have an array of “friends” – which are other “Person” instances.
Data to store
• First name
o Make this a private instance with a public getter and a private setter
• Last name
o Make this a private instance with a public getter and a private setter
• List of friends (e.g. Person[])
o Make this a private instance with no getter and setter Available actions
• [2 Points] Constructor
o Take in as argument the first name and last name (enforce invariants)
o Initialize the list of friends to have at most 5 friends if you haven’t done so already
• [4 Points] Add friend – allow adding a friend. Take in as argument a “Person” instance. Some invariants:
o Can’t add self as a friend
o Don’t add the same Person more than once
o Error checking: Handle the scenario if “Add Friend” is called when the list of friends are “full” – what should happen?
o Hint: use the equals() method for Person
• [2 Points] Remove friend – allow removing a friend from the list given the friend’s first and last name
(i.e. do NOT take in as argument a Person instance).
o Enforce invariants
• [2 Points] List friends – print out a list of friends in a (Last, First) format (how is this different than having a “getter” for List of Friends?)
o Invariant: What should it print if the Person has no friends?
• “Positive” testing (checking for “valid” conditions)
o [1 Point] Create six instances of a Person
o [1 Point] Ensure each Person instance has at least one friend
o [1 Point] Remove a friend from a Person who has several friends
o [1 Point] Print out a list of friends from each Person instance
• “Negative” testing (checking for “invalid” conditions)
o [1 Point] Attempt to create a Person with “invalid” first and last name (e.g. empty string)
o [1 Point] Attempt to add a Person’s instance (“self”) to be its own friend
o [1 Point] Attempt to add a friend that has already been added
o [1 Point] Attempt to remove a “friend” who has already been removed previously
• “Boundary” testing
o [1 Point] Choose one Person instance to have maximum number of friends (e.g. 5)
o [1 Point] Remove a friend from a Person who has one friend
o [1 Point] Attempt to add a friend to a Person whose friend list is “full”
o [1 Point] Attempt to remove a “friend” from a Person who has no friends
Explanation / Answer
Below is your code: -
Person.java
public class Person {
private String firstName;
private String lastName;
private Person[] friends;
private int noOfFriends;
public Person(String firstName, String lastName) {
try {
if (firstName.length() == 0 || lastName.length() == 0) {
throw new NameEmptyException();
} else {
this.firstName = firstName;
this.lastName = lastName;
friends = new Person[5];
noOfFriends = 0;
}
} catch (NameEmptyException e) {
System.out.println("Please enter valid names.");
}
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}
public void addFriend(Person p) {
if (this.noOfFriends < 5) {
if (!this.equals(p)) {
if (this.noOfFriends >= 0) {
boolean isFriendAlready = false;
for (int i = 0; i < this.noOfFriends; i++) {
if (this.friends[i].equals(p)) {
isFriendAlready = true;
break;
}
}
if (isFriendAlready) {
System.out.println("This person is already a friend");
} else {
this.friends[this.noOfFriends++] = p;
System.out.println(p.getLastName() + "," + p.getFirstName()
+ " is successfully added in the friend list.");
}
}
} else {
System.out.println("Cannot add the person as friend of itself");
}
} else {
System.out.println("Your already have alot of friends. So we cannot add more number of friends.");
}
}
public void removeFriends(String firstName, String LastName) {
if (this.noOfFriends > 0) {
Person p;
boolean friendFound = false;
for (int i = 0; i < this.noOfFriends; i++) {
p = this.friends[i];
if (p.getFirstName().equals(firstName) && p.getLastName().equals(LastName)) {
friendFound = true;
this.noOfFriends--;
for (int j = i; j < this.noOfFriends; j++) {
this.friends[j] = this.friends[j + 1];
}
break;
}
}
if (!friendFound) {
System.out.println("You dont have this friend in your friend list.");
} else {
System.out.println(LastName + "," + firstName + " is successfully removed from friend list.");
}
} else {
System.out.println("You dont have any friends as of now. So we cannot remove anything.");
}
}
// function to print all the friends. it is different from getter as it is
// not returning anything just printing the names of friends in specific
// format.
public void listFriends() {
if (this.noOfFriends > 0) {
System.out.println("Below is the list of your friends.");
Person p;
for (int i = 0; i < this.noOfFriends; i++) {
p = this.friends[i];
System.out.println(p.getLastName() + "," + p.getFirstName());
}
} else {
System.out.println("Sorry! your friend list is empty.");
}
}
}
PersonTester.java
public class PersonTester {
public static void main(String[] args) {
System.out.println("Positive Test cases...");
Person p1 = new Person("Dean", "Winchestor");
Person p2 = new Person("Sam", "Winchestor");
Person p3 = new Person("Damon", "Salvator");
Person p4 = new Person("Nicklaous", "Mikelson");
Person p5 = new Person("John", "Diggle");
Person p6 = new Person("Oliver", "Queen");
// adding friends
p1.addFriend(p2);
p2.addFriend(p1);
p3.addFriend(p4);
p4.addFriend(p3);
p5.addFriend(p6);
p6.addFriend(p5);
p6.addFriend(p1);
// removing friend
p6.removeFriends("Dean", "Winchestor");
//printing friends
p1.listFriends();
p2.listFriends();
p3.listFriends();
p4.listFriends();
p5.listFriends();
p6.listFriends();
System.out.println("Negative Test Cases...");
//creating person with invalid names i.e. empty strings
Person p = new Person("", "");
//adding a friend to itself
p1.addFriend(p1);
//adding a friend who is already there
p1.addFriend(p2);
System.out.println("Boundary Testing ...");
p1.addFriend(p3);
p1.addFriend(p4);
p1.addFriend(p5);
p1.addFriend(p6);
//removing friend from one person
p3.removeFriends("Nicklaous", "Mikelson");
Person p7 = new Person("Shawn", "Marsh");
p1.addFriend(p7);
p3.removeFriends("Dean", "Winchestor");
}
}
NameEmptyException.java
public class NameEmptyException extends Exception {
}
Sample Output: -
Positive Test cases...
Winchestor,Sam is successfully added in the friend list.
Winchestor,Dean is successfully added in the friend list.
Mikelson,Nicklaous is successfully added in the friend list.
Salvator,Damon is successfully added in the friend list.
Queen,Oliver is successfully added in the friend list.
Diggle,John is successfully added in the friend list.
Winchestor,Dean is successfully added in the friend list.
Winchestor,Dean is successfully removed from friend list.
Below is the list of your friends.
Winchestor,Sam
Below is the list of your friends.
Winchestor,Dean
Below is the list of your friends.
Mikelson,Nicklaous
Below is the list of your friends.
Salvator,Damon
Below is the list of your friends.
Queen,Oliver
Below is the list of your friends.
Diggle,John
Negative Test Cases...
Please enter valid names.
Cannot add the person as friend of itself
This person is already a friend
Boundary Testing ...
Salvator,Damon is successfully added in the friend list.
Mikelson,Nicklaous is successfully added in the friend list.
Diggle,John is successfully added in the friend list.
Queen,Oliver is successfully added in the friend list.
Mikelson,Nicklaous is successfully removed from friend list.
Your already have alot of friends. So we cannot add more number of friends.
You dont have any friends as of now. So we cannot remove anything.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.