This assignment will test your skills in creating and using linked lists and fil
ID: 3808481 • Letter: T
Question
This assignment will test your skills in creating and using linked lists and file processing. You should not use the Java built in linked list class. Instead, you must use the Node class and LinkedList class discussed in the lectures, and make necessary modifications to these classes. First download Node.java and LinkedList.java. Also download LinkedListDemo1.java and LinkedListDemo2.java from the assignment webpage. Study the programs before you begin the assignment. The specification for this assignment is long, but the assignment itself is not difficult once you start understanding the basic structure and the methods, and implementing the classes. 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 linked list of a User’s friends). If a user adds another user as a friend, then the other user will also add this user as a friend (that is, if A is a friend of B, then automatically B is a friend of A - similar to Facebook). 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.
*I am using JGrasp so please make sure it is compatible *Please comment throughout code *FULL QUESTION IN IMAGES BELOW
https://web.cs.dal.ca/~srini/cs1101/Node.java
https://web.cs.dal.ca/~srini/cs1101/LinkedList.java
https://web.cs.dal.ca/~srini/cs1101/LinkedListDemo1.java
This assignment will test your skills in creating and using linked lists and file processing. You should not use the Java built in linked list class Instead, you must use the Node class and LinkedList class discussed in the lectures, and make necessary modifications to these classes. First download Node.java and LinkedList.java. Also download LinkedListDemoljava and LinkedListDemo2.java from the assignment webpage. Study the programs before you begin the assignment. The specification for this assignment is long, but the assignment itself is not difficult once you start understanding the basic structure and the methods, and implementing the classes. 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 linked list of a User's friends). If a user adds another user as a friend, then the other user will also add this user as a friend (that is, if A is a friend of B, then automatically Bis a friend of A similar to Facebook) 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 nec 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 lis addUser (User u) void //adds a user after checking that they are at least 13 years old and do not already exist remove User (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 FriendLis getUsers 0:LinkedList turns the list of all us 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) oldest Friend0:User eturns the user(s) with the oldest friend. If two or more users have 'the oldest' friend (i.e., same birthday eturn the first User (similar to how ArrayLists handle this case) common Friends (User, User): find common friends between us and returns new list User Attributes String name String location //this is where they currently live (city or town) int birth Year LinkedList friends //this is a LinkedList that holds Nodes of Users who are their friends It implements the following methods: User (String, String, int): //set name, location and birthYear, initialize the LinkedList getName String getLocation String getBirthYear(): int sEqual (User) boolean getFriends LinkedListExplanation / Answer
I have mentioned all classes below:
public class FriendList {
LinkedList2 allUsers;
FriendList()
{
allUsers=new LinkedList2();
}
void addUser(User u)
{
if(2017-u.birthYear>=13 && allUsers.contains(u)!=-1)
allUsers.addNodetoEnd(u);
}
void removeUser(User u)
{
int pos=allUsers.contains(u);
allUsers.removeNode(pos);
for(int i=0;i<allUsers.getCount();i++)
{
int pos1=allUsers.getUser(i).getFriends().contains(u);
if(pos1!=-1)
{
allUsers.getUser(i).getFriends().removeNode(pos1);
}
}
}
int totalUsers()
{
return allUsers.getCount();
}
LinkedList2 getUsers()
{
return allUsers;
}
User mostFriends()
{
User user=null;
int max=0;
for(int i=0;i<allUsers.getCount();i++)
{
if(allUsers.getUser(i).getNumFriends()>max)
{
max=allUsers.getUser(i).getNumFriends();
user=allUsers.getUser(i);
}
}
return user;
}
User oldestFriend()
{
User user=null;
int max=0;
for(int i=0;i<allUsers.getCount();i++)
{
if(2017-allUsers.getUser(i).getBirthYear()>max)
{
max=2017-allUsers.getUser(i).getBirthYear();
user=allUsers.getUser(i);
}
}
return user;
}
LinkedList2 commonFriends(User u1, User u2)
{
LinkedList2 temp=new LinkedList2();
for(int i=0;i<u1.getFriends().getCount();i++)
{
if(u2.getFriends().contains(u1.getFriends().getUser(i)) !=-1)
{
temp.addNodeToFront(u1.getFriends().getUser(i));
}
}
return temp;
}
}
public class LinkedList2 {
Node2 front;
int count;
public
LinkedList2()
{
front=null;
count=0;
}
int size()
{
return count;
}
public boolean isEmpty()
{
return front == null;
}
int getCount()
{
return count;
}
void clear()
{
front=null;
}
public void addNodeToFront(User u)
{
Node2 nptr = new Node2(u, null);
count++ ;
if(front == null)
{
front = nptr;
}
else
{
nptr.setNext(front);
front = nptr;
}
}
Node2 getFront()
{
return front;
}
public void enumerate()
{
if (count == 0)
{
System.out.print("empty ");
return;
}
if (front.getNext() == null)
{
System.out.println(front.getUser());
return;
}
Node2 ptr = front;
System.out.print(front.getUser()+ "->");
ptr = front.getNext();
while (ptr.getNext() != null)
{
System.out.print(ptr.getUser()+ "->");
ptr = ptr.getNext();
}
System.out.print(ptr.getUser()+ " ");
}
void removeFront()
{
if(front==null)
{
System.out.println("Empty");
}
front = front.getNext();
count--;
}
void removeLast()
{
Node2 s = front;
Node2 t = front;
while (s != null)
{
t = s;
s = s.getNext();
}
t.setNext(null);
count --;
}
void addNodetoEnd(User u)
{
Node2 nptr = new Node2(u,null);
count++ ;
if(front == null)
{
front = nptr;
}
else
{
Node2 s = front;
Node2 t = front;
while (s != null)
{
t = s;
s = s.getNext();
}
t.setNext(nptr);
}
}
int contains(User u)
{
int index=0;
Node2 s = front;
Node2 t = front;
while (s != null)
{
index++;
t = s;
s = s.getNext();
if(t.user.isEqual(u))
return index;
}
return -1;
}
void removeNode(int pos)
{
if (pos == 1)
{
front = front.getNext();
count--;
return ;
}
if (pos == count)
{
Node2 s = front;
Node2 t = front;
while (s != null)
{
t = s;
s = s.getNext();
}
t.setNext(null);
count --;
return;
}
Node2 ptr = front;
pos = pos - 1 ;
for (int i = 1; i < count - 1; i++)
{
if (i == pos)
{
Node2 tmp = ptr.getNext();
tmp = tmp.getNext();
ptr.setNext(tmp);
break;
}
ptr = ptr.getNext();
}
count-- ;
}
User getUser(int pos)
{
Node2 ptr = front;
for (int i = 0; i < count; i++)
{
if (i == pos)
{
return ptr.user;
}
ptr = ptr.getNext();
}
return null;
}
public String toString()
{
String st=null;
Node2 s = front;
Node2 t = front;
while (s != null)
{
t = s;
s = s.getNext();
st=t.getUser()+ "->";
}
return st;
}
}
public class Node2 {
User user;
Node2 next;
/* Constructor */
public Node2()
{
next = null;
}
/* Constructor */
public Node2(User ud,Node2 n)
{
user = ud;
next = n;
}
/* Function to set link to next Node */
public void setNext(Node2 n)
{
next = n;
}
/* Function to set data to current Node */
public void setUserData(User ud)
{
user = ud;
}
/* Function to get link to next node */
public Node2 getNext()
{
return next;
}
/* Function to get data from current Node */
public User getUser()
{
return user;
}
public String toString()
{
return user.toString();
}
}
public class User {
String name;
String location;
int birthYear;
LinkedList2 friends;
public
User(String n,String loc, int year)
{
name=n;
location=loc;
birthYear=year;
}
String getName()
{
return name;
}
String getLocation()
{
return location;
}
int getBirthYear()
{
return birthYear;
}
LinkedList2 getFriends()
{
return friends;
}
boolean isEqual(User u)
{
if(this.name==u.name && this.location==u.location && this.birthYear==u.birthYear)
return true;
return false;
}
int getNumFriends()
{
return friends.getCount();
}
public String toString()
{
return name+"from"+location;
}
void addFriend(User u)
{
friends.addNodetoEnd(u);
}
void removeFriend(User u)
{
int pos=friends.contains(u);
if(pos!=-1)
friends.removeNode(pos);
}
User oldestFriend()
{
int age=0;
User u=null;
for(int i=0;i<friends.getCount();i++)
{
if(2017-friends.getUser(i).birthYear>age)
{
age=2017-friends.getUser(i).birthYear;
u=friends.getUser(i);
}
}
return u;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Welcome to FriendList! What would you like to do(press a number)?");
int num=0;
Scanner sc= new Scanner(System.in);
FriendList fl=new FriendList();
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
try{
String strLine;
while ((strLine = br.readLine()) != null)
{
String[] tokens = strLine.split(" ");
User user = new User(tokens[0],tokens[1],(Integer.parseInt(tokens[2])));
fl.addUser(user);
}
br.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
BufferedReader br1 = new BufferedReader(new FileReader("friends.txt"));
try{
String strLine;
while ((strLine = br1.readLine()) != null)
{
String[] tokens = strLine.split(" ");
String name=tokens[0];
for(int j=0;j<fl.getUsers().count;j++)
{
User u=fl.getUsers().getUser(j);
if (u.getName().equals(name)){
for(int i=1;i<tokens.length;i++)
{
for(int k=0;k<fl.getUsers().count;k++)
{
if(fl.getUsers().getUser(j).getName().equals(name));
User u2=fl.getUsers().getUser(j);
u.addFriend(u2);
}
}
}
}
User user = new User(tokens[0],tokens[1],(Integer.parseInt(tokens[2])));
fl.addUser(user);
}
br1.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
while(num!=12)
{
System.out.println("1. Print out all the users");
System.out.println("2 Print the total number of users");
System.out.println("3. Print out all the friends of user");
System.out.println("4. Add new user");
System.out.println("5 Remove user");
System.out.println("6 Add a friend");
System.out.println("7 Print user with most friends");
System.out.println("8 Print user with most friends");
System.out.println("9 Find common friends between two friends");
System.out.println("10 Print oldest friend");
System.out.println("11 Find user with the oldest friend on friendlist");
System.out.println("12 Quit");
num=sc.nextInt();
if(num==1)
{
System.out.println(fl.getUsers());
}
else if(num==2)
{
System.out.println(fl.getUsers().getCount());
}
else if(num==3)
{
System.out.println("Enter name");
String name=sc.next();
User u2=null;
for(int j=0;j<fl.getUsers().count;j++)
{
User u=fl.getUsers().getUser(j);
if (u.getName().equals(name)){
u2=u;
}
}
int pos=fl.getUsers().contains(u2);
System.out.println(fl.getUsers().getUser(pos).getFriends());
}
else if(num==4)
{
System.out.println("Enter name Location BirthYear");
String name=sc.next();
String loc=sc.next();
String year=sc.next();
User u=new User(name,loc,Integer.parseInt(year));
try (BufferedWriter bw = new BufferedWriter(new FileWriter(users.txt))) {
String content = name +" "+loc+" "+year+" ";
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
else if (num==5)
{
System.out.println("Enter name");
String name=sc.next();
User u2=null;
for(int j=0;j<fl.getUsers().count;j++)
{
User u=fl.getUsers().getUser(j);
if (u.getName().equals(name)){
u2=u;
}
}
int pos=fl.getUsers().contains(u2);
fl.getUsers().removeNode(pos);
}
else if (num==6)
{
System.out.println("Enter name");
String name=sc.next();
User u2=null;
for(int j=0;j<fl.getUsers().count;j++)
{
User u=fl.getUsers().getUser(j);
if (u.getName().equals(name)){
u2=u;
}
}
int pos=fl.getUsers().contains(u2);
System.out.println("Enter friend");
String name1=sc.next();
User u3=null;
for(int j=0;j<fl.getUsers().count;j++)
{
User u=fl.getUsers().getUser(j);
if (u.getName().equals(name1)){
u3=u;
}
}
fl.getUsers().getUser(pos).addFriend(u3);
}
else if (num==7)
{
System.out.println("Enter name");
String name=sc.next();
User u2=null;
for(int j=0;j<fl.getUsers().count;j++)
{
User u=fl.getUsers().getUser(j);
if (u.getName().equals(name)){
u2=u;
}
}
int pos=fl.getUsers().contains(u2);
System.out.println("Enter friend");
String name1=sc.next();
User u3=null;
for(int j=0;j<fl.getUsers().count;j++)
{
User u=fl.getUsers().getUser(j);
if (u.getName().equals(name1)){
u3=u;
}
}
fl.getUsers().getUser(pos).removeFriend(u3);
}
else if (num==8)
{
System.out.println(fl.mostFriends());
}
else if (num==9)
{
System.out.println("Enter name");
String name=sc.next();
User u2=null;
for(int j=0;j<fl.getUsers().count;j++)
{
User u=fl.getUsers().getUser(j);
if (u.getName().equals(name)){
u2=u;
}
}
int pos=fl.getUsers().contains(u2);
System.out.println("Enter other");
String name1=sc.next();
User u3=null;
for(int j=0;j<fl.getUsers().count;j++)
{
User u=fl.getUsers().getUser(j);
if (u.getName().equals(name1)){
u3=u;
}
}
System.out.println(fl.commonFriends(u2, u2));
}
else if (num==10)
{
System.out.println("Enter name");
String name=sc.next();
User u2=null;
for(int j=0;j<fl.getUsers().count;j++)
{
User u=fl.getUsers().getUser(j);
if (u.getName().equals(name)){
u2=u;
}
}
User user=null;
int max=0;
for(int i=0;i<u2.getFriends().getCount();i++)
{
if(2017-u2.getFriends().getUser(i).getBirthYear()>max)
{
max=2017-u2.getFriends().getUser(i).getBirthYear();
user=u2.getFriends().getUser(i);
}
}
System.out.println(user);
}
else if (num==11)
{
System.out.println(fl.oldestFriend());
}
else if (num==12)
{
break;
}
}
}
}