Please read the instructions properly! Only need the simple Java classes not GUI
ID: 3679586 • Letter: P
Question
Please read the instructions properly! Only need the simple Java classes not GUI or i/o.
First download Node.java and LinkedList.java. Also download LinkedListDemo1.java and LinkedListDemo2.java. FriendList is a new social media tool. It keeps track of all of its users and their friends. It can calculate the total users of the social medial tool, the user with the most friends, the user with the oldest friend, and can also find the common friends between two users. Users can also be added and removed from Friendlist. In order to join FriendList a user must be at least 13 years old and when a user is removed, not only is the user removed from FriendList but also from any user’s friend lists. Each user has a name, their current location (name of the city or town), and birth year. Users also have a list of friends (a linklist of a User’s friends). You will need to implement two classes for this social media tool: FriendList, and User. The attributes and methods for each class are listed below (as well as a short explanation of some of the methods). You will implement all the lists in this program as linked lists. In addition, you will need to modify the Node and LinkedList classes so that data stored/retrieved is of User type.
Make sure you do proper error testing as well, for example you should not add a user to FriendList if they already exist. And new members need to be at least 13 years old. You can add extra methods or attributes if you find it necessary.
FriendList This class has one attribute – allUsers (a Linkedlist of Nodes that stores Users). It also can implement the following methods:
• FriendList( ): //no args constructor that creates a new empty list
• addUser (User u) : void //adds a user after checking that they are at least 13 years old and do not already exist • removeUser (User u ) : void //remove user from FriendList and remove that user as a friend of any other users
• totalUsers ( ) : int //returns the total number of users on FriendList
• getUsers () : LinkedList //returns the list of all users
• mostFriends ( ) : User //returns the user who has the most friends. If two or more users have the 'most friends' (i.e., a tie for most friends, return the first User with that number (similar to how ArrayLists handle this case)
• oldestFriend() : User //returns the user with the oldest friend. If two or more users have 'the oldest' friend (i.e., same birthday – return the first User (similar to how ArrayLists handle this case)
• commonFriends (User, User): LinkedList //find common friends between users and returns new list
User Attributes:
String name
String location //this is where they currently live (city or town)
int birthYear
LinkedList friends //this is a LinkedList that holds Nodes of Users who are their friends
It implements the following methods:
• User (String,String, int)://setname, location and birthYear, initialize the LinkedList
• getName ():String
• getLocation ():String
• getBirthYear ():int
• isEqual (User):boolean
• getFriends ():LinkedList
• getNumFriends ():int//returns the number of friends the user has
• toString ():String //returns the name and location of the user
• addFriend (User):void//adds a friend to their list
• removeFriend (User):void//removes a friend from their list
• oldestFriend ():User//returns the friend who is the oldest (e.g., 30 years old). If there is tie (i.e., same birthday) return the first
friend with that birthdate.
In order to use Node class you will need to change it to hold User objects instead of just Strings. You will also need to make the necessary changes to set and get to accommodate the data stored as type User.
For example, the new Node class could look like the UML diagram below:
Node
- user :User
- next :Node
+Node(User ud,Node n)
+ setUserData (User):void
+setNext(Node) :void
+getUser():User
+getNext():Node
+toString():String
You will also need to adapt the LinkedList class to accommodate the fact that Nodes store User data (see the UML diagram below for a list of suggested methods that you could include and you can add other methods)
LinkedList
- front : Node
- count : int
+LinkedList() //set front to null and count to 0
+size (): int
+isEmpty(): boolean
+clear (): void //make the list empty
+addNodeToFront (User) :void
+getFront () :Node //get the first Node in the list
+enumerate () :void //scan list and print the content
+removeFront () :void //remove first Node in list
+removeLast () :void //remove last Node from list
+addNodeToEnd (User) :void //add a Node with User data to end of list
+contains (User) :int //check to see if the list has a User and return the index of the user or return -1 if not in list
+removeNode (int) :void //remove the Node at a given index
+getUserAt (int) : User //return the User of the Node at the given index from the list
+toString () :String
You should write a demo program that implements the FriendsList.
Any help is appreciated! thanks in advance!
Explanation / Answer
Please use the code as shown below :
User Class :-
public void removeUser(User u){
while (allUsers.remove(u)) {
// this loops continue until there are no more entries of user in allUsers (as defined by the User.equals() method)
}
// now remove this user from all the remaining user's friends list
for (User user : allUsers) {
while (user.getFriends().remove(u)) {
// remove all occurances
}
}
}
public class User{
private String name;
private String location;
private int birthYear;
private LinkedList friends;
public User(String n, String l, int b){
name=n;
location=l;
birthYear=b;
friends=new LinkedList();
}
public String getName(){return name;}
public String getLocation(){return location;}
public int getBirthYear(){return birthYear;}
public boolean equals(User u){
if(this.getName().equals(u.getName())&&this.getLocation().equals(u.getLocation())&&this.getBirthYear()==u.getBirthYear())
return true;
else
return false;
}
public LinkedList getFriends(){return friends;}
public int getNumFriends(){return friends.size();}
public String toString(){
return name+" from "+location;
}
public void addFriend(User u){friends.addToEnd(u);}
public void removeFriend(User u){
if(friends.indexOf(u)!=-1)
friends.remove(friends.indexOf(u));
}
}
Friendlist class:-
import java.util.Scanner;
public class FriendsTest
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("1.Add a friend");
System.out.println("2.Remove a friend");
System.out.println("3.Display all friends");
System.out.println("4.Exit Program");
int selection = input.nextInt();
FriendsList bornList = new FriendsList();
int count = 0;
while (selection != 4)
{
int number = 0;
if (selection == 1)
{
System.out.printf("Whats the first Name: ");
String first = input.next();
System.out.println("Whats there Last Name: ");
String last = input.next();
System.out.println("Whats there Age");
String age = input.next();
bornList.setList(first, last, age); //sets the values for friend
bornList.addFriend(); // adds the friend to the array list
count++;
}
if (selection == 2)
{
System.out.printf("Whats the first Name: ");
String first = input.next();
System.out.println("Whats there Last Name: ");
String last = input.next();
System.out.println("Whats there Age");
String age = input.next();
bornList.setList(first, last, age); // sets up the friend to remove
bornList.removeFriend(); // removes the friend from the array list
}// end of if
if (selection == 3)
{
//System.out.println(bornList.toString());
for(int i= 0; i < 1;i++)
{
System.out.println(bornList.list);
}
}
System.out.println("1.Add a friend (First Name, Last Name, Age)");
System.out.println("2.Remove a friend");
System.out.println("3.Display all friends");
System.out.println("4.Exit Program");
selection = input.nextInt();
} // end of else statement
} // end of while loop
} // end of main method
// end of FriendsTest
FriendsList Class
import java.util.ArrayList;
public class FriendsList {
// initialize
public String firstName;
public String lastName;
public String age;
public ArrayList list = new ArrayList();
//parameterized constructor
public void setList(String first, String last, String age)
{
setFirstName(first);
setLastName(last);
setAge(age);
}//end constructor for friend
/****************Starting of get methods************************/
public String getFirstName()
{
return firstName;
}//end getFirstName()
public String getLastName()
{
return lastName;
}//end getlastName()
public String getAge()
{
return age;
}//end of getAge()
/****************End of get methods************************/
/****************Starting of set methods************************/
public void setFirstName( String FirstName )
{
firstName = FirstName;
}//end setFirstName()
public void setLastName( String LastName )
{
lastName = LastName;
}//end setLastName()
public void setAge(String Age)
{
age = Age;
}// end of setAge()
/****************End of set methods************************/
/****************Add to Array Method************************/
public void addFriend()
{
list.add(firstName);
list.add(lastName);
list.add(age);
}
/****************Remove a friend method************************/
public void removeFriend()
{
list.remove(firstName);
list.remove(lastName);
list.remove(age);
}
//utility methods
@Override
public String toString()
{
return String.format( "Name: %s Student Last Name: %s Student Age: %s Student", firstName, lastName, age);
}//end toString()
}// end of main Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.