Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This Should Be Done In Java!!!! Problem Set 3 Objective: The goal of this assign

ID: 670674 • Letter: T

Question

This Should Be Done In Java!!!!

Problem Set 3
Objective: The goal of this assignment is to practice implementing a linked list
to solve the exercise.
Background: During the winter holidays, people usually visit their friends and
relatives. Let us use lists to help organize a sequence of such trips.
Assignment: The elements of your list are objects representing a friend or a
relative. Each such object should contains three fields:
• the name of the main person in the household,
• their relation to you (e.g., "cousin", "friend", etc.), and
• the number of people in the household -- i.e., the number of gifts that
you need to bring.
Write methods:
• for starting such a list,
• for inserting a new person to the list,
• for deleting a person from the list,
• for searching whether a given person is on your list,
• for printing the total list of people to visit, and
• for computing the total number of gifts that you need to buy.
In the main program, test your methods by filling in information about at least
five different people to visit, and by printing the resulting list. To test insertion
and deletion, add our favorite TA to the list, show that you can search for this
TA, then delete the poor TA from the list :-).
For extra credit: write your method by using the ADT list and check that it
works both for a linked list and for an array implementation of this ADT.

Explanation / Answer

import java.util.LinkedList;

public class Person {
       String name,relation;
       int number;
   LinkedList<Person> l;
   public Person(String n,String rel,int num){
       this.name=n;
       this.relation=rel;
       this.number=num;
   }
   public Person() {
       // TODO Auto-generated constructor stub
   }
   public void startList(){
       l=new LinkedList<Person>();
   }
   public void insert(Person a){
       l.add(a);
   }
   public void delete(Person a){
   for(int i=0;i<l.size();i++){
       if(a.equals(l.get(i))){
           l.remove(i);
           break;
       }
   }
   }
   public void search(Person a){
       for(int i=0;i<l.size();i++){
           if(a.equals(l.get(i))){
               System.out.println("Person exists in the list!!");
           }
       }
   }
   public void print(){
       for(int i=0;i<l.size();i++){
           System.out.println("Name of the person: "+l.get(i).name);
           System.out.println("Relation to the person: "+l.get(i).relation);
           System.out.println("Number of people in the house hold: "+l.get(i).number);  
       }
   }
   public static void main(String[] args) {
Person p1=new Person("name1", "rel1", 1);
Person p2=new Person("name2", "rel2", 2);
Person p3=new Person("name3", "rel3", 3);
Person p4=new Person("name4", "rel4", 4);
Person p5=new Person("name5", "rel5", 5);
Person p=new Person();
       p.startList();
       p.insert(p1);
       p.insert(p2);
       p.insert(p3);
       p.insert(p4);
       p.insert(p5);
       p.print();
       p.delete(p5);
       p.print();
       p.search(p1);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote