This assignment will test your skills in creating and using linked lists and fil
ID: 3809319 • 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 ifthey 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 Friendi istExplanation / Answer
package newpkg;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
class Node{
private User user;
private Node next;
public Node(User user,Node next){
this.user=user;
this.next=next;
}
public User getUser(){
return user;
}
public Node getNext(){
return next;
}
public void setUserData(User user){
this.user=user;
}
public void setNext(Node next){
this.next=next;
}
}
class LinkedList{
Node front;
int count;
public LinkedList(){
front=null;
count=0;
}
public int size(){
return this.count;
}
public boolean isEmpty(){
return count==0;
}
public void clear(){
}
public void addNodeToFront(User user){
Node node=new Node(user,front);
front=node;
}
public Node getFront(){
return front;
}
public void enumerate(){
Node tmp=front;
while(tmp!=null){
System.out.println(tmp.getUser()+"-->");
tmp=tmp.getNext();
}
}
public void removeFront(){
Node tmp=front;
front=front.getNext();
tmp=null;
}
public void removeLast(){
Node tmp=front;
while(tmp.getNext()!=null ){
tmp=tmp.getNext();
}
tmp=null;
}
public void addNodeToEnd(User user){
Node node=new Node(user,null);
Node tmp=front;
while(tmp.getNext()!=null){
tmp=tmp.getNext();
}
tmp.setNext(node);
}
public int contains(User user){
Node tmp=front;
int index=0;
while(tmp!=null){
if(tmp.getUser()==user){
return index;
}else
index++;
}
return -1;
}
public void removeNode(int index){
Node tmp=front;
int i=0;
while(tmp!=null && tmp.getNext()!=null && tmp.getNext().getNext()!=null && i<index){
tmp=tmp.getNext();
i++;
}
Node rs=tmp.getNext().getNext();
tmp.setNext(rs);
}
public User getUserAt(int index){
int i=0;
Node tmp=front;
while(tmp!=null && i<index){
tmp=tmp.getNext();
i++;
}
return tmp.getUser();
}
public String toString(){
return null;
}
}
class User{
String name;
String location;
int birthyear;
LinkedList friends;
User(String name,String location,int year){
this.name=name;
this.location=location;
this.birthyear=year;
}
public String getName(){
return name;
}
public String getLocation(){
return location;
}
public int getBirthYear(){
return birthyear;
}
public boolean isEqual(User user){
return this==user;
}
public LinkedList getFriends(){
return friends;
}
public int getNumFriends(){
return friends.size();
}
public String toString(){
return null;
}
public void addFriend(User user){
this.friends.addNodeToFront(user);
user.getFriends().addNodeToFront(this);
}
public void removeFriend(User user){
Node nd=friends.getFront();
Node prev=null;
if(nd.getUser()==user){
friends.removeFront();
}
while(nd!=null && nd.getUser()!=user){
prev=nd;
nd=nd.getNext();
}
if(nd!=null){
prev.setNext(nd.getNext());
}else{
prev.setNext(null);
}
///*************8
}
public User oldestFriend(){
Node tmp=friends.getFront();
int index=0;
int min=tmp.getUser().getBirthYear();
User oldestfrnd=tmp.getUser();
tmp=tmp.getNext();
while(tmp!=null){
if(tmp.getUser().getBirthYear()<min){
min=tmp.getUser().getBirthYear();
oldestfrnd=tmp.getUser();
}
tmp=tmp.getNext();
}
return oldestfrnd;
}
}
public class FriendList {
LinkedList allUsers;
public FriendList(){
allUsers=new LinkedList();
}
public void addUser(User user){
if(2017-user.getBirthYear()>13)
allUsers.addNodeToFront(user);
}
public void removeUser(User user){
}
public int totalUsers(){
return allUsers.size();
}
public LinkedList getUsers(){
return allUsers;
}
public User mostFriends(){
Node tmp=allUsers.getFront();
User mostfrndUser=null;
int max=0;
while(tmp!=null){
int count=tmp.getUser().getNumFriends();
if(count>max){
max=count;
mostfrndUser=tmp.getUser();
}
}
return mostfrndUser;
}
public LinkedList commonFriends(User user1,User user2){
LinkedList frnds1=user1.getFriends();
LinkedList frnds2=user2.getFriends();
LinkedList res=new LinkedList();
while(frnds1!=null){
Node tmp=frnds2.getFront();
if(frnds1.getFront().getUser().isEqual(tmp.getUser())){
res.addNodeToFront(tmp.getUser());
}
tmp=tmp.getNext();
}
return res;
}
public User getUser(String name){
Node tmp=allUsers.getFront();
while(tmp!=null){
if(tmp.getUser().getName().equals(name)){
return tmp.getUser();
}
}
return null;
}
public static void main(String args[]){
FriendList frndList=new FriendList();
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
fileInputStream = new FileInputStream("C:\Users\IBM_ADMIN\workspace\testnew\src ewpkg\users.txt");
inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String s;
while((s = bufferedReader.readLine()) != null){
String[] var = s.split(" ");
//System.out.println(var[0]+ var[1]+" "+var[2]);
User user=new User(var[0],var[1],Integer.parseInt(var[2]));
frndList.addUser(user);
}
}catch(Exception e){
e.printStackTrace();
}
try {
fileInputStream = new FileInputStream("C:\Users\IBM_ADMIN\workspace\testnew\src ewpkg\friends.txt");
inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String s;
while((s = bufferedReader.readLine()) != null){
String[] var = s.split(" ");
//System.out.println(var[0]+ var[1]+" "+var[2]);
User user=frndList.getUser(var[0]);
int i=0;
while(i<var.length){
User frnd=frndList.getUser(var[++i]);
user.addFriend(frnd);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}