Life 4 Pancakes Life 4 Pancakes - 2 months ago 33
Java Question

Java Exarcise. How to create a class similar to social media site?

Create and implement a class Person. A Person has a firstName and friends. Store the names of the friends as a String, separated by spaces. Provide a constructor that constructs a Person with a given name (passed through arguments) and no friends. Provide the following methods:

public void befriend(Person p)
public void unfriend(Person p)
public String getFriendNames()
public int getFriendCount()


*Hint - you can use p.name to access the name of the Person passed to a method as an argument.

Include a Tester class to make sure your Person has some friends.

How do I store the names of the friends as a String, separated by spaces. (I have to be able to input the names from the main method). I also have no idea how to get rid of already inputted name using the method "unfriend"

public class Person
{
private String firstName;
private String friendNames;
private int friendCount;

public Person(String name)
{
firstName = name;
friendCount = 0;
}
public String getFriendNames()
{
return friendNames;
}
public double getFriendCount()
{
return friendCount;
}
public void befriend(String name)
{
friendNames = friendNames + " " + name;
friendCount++;
}
public void unfriend(String name)
{
String[] parseNames = friendNames.split(name);
friendNames = parseNames[0] + parseNames[1];
friendCount--;
}

}


Main Method:

public class PersonTester {


public static void main(String[] args) {
Person p = new Person("Alex");
p.befriend("John");
p.befriend("Alice");
p.befriend("Mike");
p.befriend("Annette");
p.unfriend("Alice");

System.out.println(p.getFriendCount());
System.out.println(p.getFriendNames());
}

}


Expected output:

2
John Mike

Answer

The problems you are having with the methods using the parameter(Person p) are because you have two different variables: friendName (which exists) and name (which does not). Changing the variable friendName to name will take care of some of the errors you are receiving.

(Also the method getFriendCount() returns friendsCount, but should return friendCount (you have an extra s in there) and your assignment calls for a method called befriend, not bestFriend.)

How to delete friends:

You can delete a friend by parsing the friend out of the friendNames string and then concatenating the two resulting strings back together:

 public void unfriend(String name)
{
    String[] parseNames = friendNames.split(name);
    friendNames = parseNames[0] + parseNames[1];
    friendCount--;
}

I would suggest changing befriend and unfriends parameters to accept a String instead of a Person object. Person already has access to its own object and in your main you are trying to pass them Strings anyways. Here is what befriend should look like:

 public void befriend(String name) //Changed to "befriend"
{
    friendNames = friendNames + " " + name;
    friendCount++;
}

Also, you only need one constructor for Person, which should look like this:

 public Person(String name)
{
    firstName = name;
    friendCount = 0;
}

When I run your program (using these changes) I get the following output:

 2.0
  John  Mike