Question

In: Computer Science

CAN SOMEONE PLEASE DO THIS QUESTION FOR ME. You are given three files Main.Java, Student.Java, LinkedList.java....

CAN SOMEONE PLEASE DO THIS QUESTION FOR ME.

You are given three files Main.Java, Student.Java, LinkedList.java. Student is completed for you and is only used to test the LinkedList with an object (we will pass in Student for the generic parameter of the list). Your mission is to implement LinkedList.java, but I highly recommend you do this in a piecewise fashion as outlined in the //TODO s in Main.java (This assignment is easy if you do it piecewise and traumatic if you don't). You can go down this description of the TODO's for a few additional hints. You can try to pass the test cases as you go through (they should be in order I think but sometimes the order gets scrambled when I create the test cases and there is no easy way to unscramble, so just be aware of that). Each test case includes the code of how I made it so you can try to do it in main if you are not passing a test case for some reason for further investigation For this assignment you can assume that each student in the list is unique (for all of the tests I do not add any duplicate students, we will deal with this case later, it's slightly more code but nothing too crazy)

MAIN.JAVA

import java.util.*;

class Main {

  public static void main(String[] args)

  {

    //TODO 1: Implement the private inner class Node in LinkedList

    //TODO 2: Here in Main,  Make a new LinkedList with the generic parameter of Student. Note that the constuctor in LinkedList is implemented for you. What does the constructor initialize the list to?

LinkedList<Student> myList= new LinkedList<Student>();

  //TODO 3: Define 5 Students, remember to invoke the constructor using the new command, make them all have different ID's

  //TODO 4: Implement addToFront and getLength in Linked List Add some of the students to the list using addToFront

  //print out the length run your program and make sure it equals the number of students before continuing

//TODO 5: Implement toString in Node and LinkedList. The Node toString should just print out the data field of the node. Note that toString is implemented in Student, so this should be easy-- creating a string out of the data should invoke the toString in Student (i.e. if I have a student instance named A saying String x= ""+A; will invoke A's toString. Alternatively you can explicitly call A.toString() from within Node (but understand why ""+A will work). Then implement toString in LinkedList. If one prints out a list with two students (Eugenio and Daniqua), with Danica added to the front first and Eugenio added second our list should look as follows head->name: Eugenio id: 700555555->name: Daniqua id: 700444444->null   note the spaces. Print out your list to ensure it works. //TODO 6: Implement addToEnd in LinkedList and then add another Student, but this time to the end, and print out the length (should be one more) and the list to ensure it added the student to the end.

//TODO 7: Implement removeFromFront in LinkedList and print out what is returned, should be the first student in the list. Then print out the length (should be one less than what you had previously). Finally print out the length to ensure it is correct using getLength

//TODO 8: Implement removeFromEnd in LinkedList and print out what is returned. it should be the student who was at the end of the list, furthermore print out the list and the length to ensure it has the right students in it. Note that this function is a bit more involved, you may need to use 3 references as you iterate through the list (think about why?). Make sure your algorithm will work with a list that is of length 1, length 2, length 3, and some big length like 5

    //TODO 9: Implement removeTarget in LinkedList. This should rely on the the Type T's equals. Remember our T, though it's generic, is guaranteed to have this function because it's guaranteed to be an object. In Student's case two students are equal if their student ID's are equal. Test removeTarget with a student in the list to ensure that removeTarget returns the right value when you test (and removes the actual student from the list when appropriate).

    ///TODO 10: Implement reverseList in LinkedList.  You need to reverse the list in place, i.e., with out allocating data into array.

  }

}

LINKEDLIST.JAVA

public class LinkedList <T>

{ //MUST IMPLEMENT ALL FUNCTIONS

  Node head;

     public LinkedList()

  {

    head=null;

   }

   public int getLength()

  {

    return 0;

  }

public void addToFront(T toAdd)

  {   

  }

   public void addToEnd(T toAdd)

  {

{

    public T removeFromFront()

  {

    return null;

     }

    public T removeFromEnd()

  {

    return null;

      }

public boolean removeTarget(T toRemove)

  {

    return true;

  }

public void reverseList(){

      }

public String toString()

  {

    return "";

  }

private class Node

  {

    private T data;

    private Node next;

    Node(T dataPassed, Node nextPassed)

    {

      data=dataPassed;

      next=nextPassed;

    }

public void setdata(T dataPassed)

    {

     data=dataPassed;

    }

    public T getdata()

    {

      return data;

    }

    public Node getnext()

    {

     return next;

    }

    public void setnext(Node passed)

    {

      next=passed;

    }

    public String toString()

    {

      return (""+data);

    }

    

  }

}

STUDENT.JAVA

public final class Student

{

private int schoolID;

   private String name;

public Student(String namePassed,int schoolIDPassed)

      {

      schoolID= schoolIDPassed;

                 name= namePassed;

            }

public int getSchoolID()

            {

                        return schoolID;

            }

public void setSchoolID(int schoolIDPassed)

            {

                        schoolID=schoolIDPassed;

            }

public String getName()

            {

                        return name;

            }

            public void setName(String namePassed)

            {

                        name=namePassed;

            }

            public boolean equals(Object toCompare)

            {

              Student temp= (Student) toCompare;

                        return(temp.getSchoolID()==schoolID);

            }

            

            public String toString()

            {

              String toReturn="name: "+name+" id: "+schoolID;

              return (toReturn);

            }

}

Solutions

Expert Solution

//Student.java

public final class Student {

      

       private int schoolID;

       private String name;

      

       public Student(String namePassed,int schoolIDPassed)

       {

             schoolID= schoolIDPassed;

             name= namePassed;

       }

       public int getSchoolID()

       {

             return schoolID;

       }

      

       public void setSchoolID(int schoolIDPassed)

       {

             schoolID=schoolIDPassed;

       }

       public String getName()

       {

             return name;

       }

       public void setName(String namePassed)

       {

             name=namePassed;

       }

      

       public boolean equals(Object toCompare)

       {

             Student temp= (Student) toCompare;

           return(temp.getSchoolID()==schoolID);

       }

      

       public String toString()

       {

             String toReturn="name: "+name+" id: "+schoolID;

             return (toReturn);

       }

}

//end of Student.java

//LinkedList.java

public class LinkedList<T> {

      

       //MUST IMPLEMENT ALL FUNCTIONS

       private Node head;

      

       public LinkedList()

       {

             head=null;

       }

      

       public int getLength()

       {

             Node current=head;

             int count=0;

             while(current!=null)

             {

                    count++;

                    current=current.getnext();

             }

             return count;

       }

      

       public void addToFront(T toAdd)

       {

       Node node = new Node(toAdd,head);

       head = node;

      

       }

       public void addToEnd(T toAdd)

       {

       Node node = new Node(toAdd,null);

       if(head == null)

       {

             head = node;

       }else {

             Node curr = head;

             while(curr.getnext() != null)

                    curr = curr.getnext();

             curr.setnext(node);

            

       }

       }

      

       public T removeFromFront()

       {

             if(head == null)

                    return null;

             T data = head.getdata();

             head = head.getnext();

             return data;

       }

      

       public T removeFromEnd()

       {

      

             if(head == null)

                    return null;

            

             Node curr = head;

             Node prev = null;

             while(curr.getnext() != null)

             {

                    prev = curr;

                    curr = curr.getnext();

             }

            

             T data = curr.getdata();

             prev.setnext(null);

             return data;

       }

      

       public boolean removeTarget(T toRemove)

       {

      

             if(head == null)

                    return false;

             else if(head.getdata().equals(toRemove))

             {

                    head = head.getnext();

                    return true;

             }

             Node prev = null;

             Node curr = head;

            

             while(curr.getnext() != null)

             {

                    if(curr.getdata().equals(toRemove))

                    {

                           prev.setnext(curr.getnext());

                           return true;

                    }

                   

                    prev = curr;

                    curr = curr.getnext();

             }

            

             if(curr.getdata().equals(toRemove))

             {

                    prev.setnext(null);

                    return true;

             }

            

             return false;

       }

// method to reverse the list in place

       public void reverseList(){

            

             // if list is empty or contains single element, do nothing

if((head != null) && (head.next != null))

             {

                    Node prev = null; // previous node initially null

                    Node curr = head; // current node

                    Node next = head.next; // node next to current in original list

                   

                    // loop till we reach the last node

                    while(curr.next != null)

                    {

                           curr.next = prev; // make next of current point to prev

                           prev = curr; // make prev = curr

                           curr = next; // make curr = next

                           next = next.next; // set next = node next to next in original list

                    }

                   

                    curr.next = prev; // make the last node point to the prev node

                    head = curr; // make last node head

             }

            

       }

      

       public String toString()

       {

             String llStr = "head -> ";

             Node curr = head;

             while(curr != null)

             {

                    llStr = llStr + curr.getdata()+" -> ";

                    curr = curr.getnext();

             }

             llStr = llStr + "null";

             return llStr;

       }

       private class Node

       {

       private T data;

       private Node next;

       Node(T dataPassed, Node nextPassed)

       {

       data= dataPassed;

       next=nextPassed;

       }

      

       public void setdata(T dataPassed)

       {

       data=dataPassed;

       }

       public T getdata()

       {

       return data;

       }

       public Node getnext()

       {

       return next;

       }

       public void setnext(Node passed)

       {

       next=passed;

       }

       }

      

}

//end of LinkedList.java

//Main.java

import java.util.*;

public class Main{

       public static void main(String[] args) {

            

             //TODO 1: Implement the private inner class Node in LinkedList -- done

            

             //TODO 2: Here in Main, Make a new LinkedList with the generic parameter of Student. Note that the constuctor in LinkedList is implemented for you. What does the constructor initialize the list to? empty list

             // --done

             LinkedList<Student> myList= new LinkedList<Student>();

            

             //TODO 3: Define 5 Students, remember to invoke the constructor using the new command, make them all have different ID's

             Student stud1 = new Student("John Smith",123);

             Student stud2 = new Student("James Carrey",124);

             Student stud3 = new Student("Josephine Day",125);

             Student stud4 = new Student("Phil Hughes",126);

             Student stud5 = new Student("Michael Argan",127);

            

             //TODO 4: Implement addToFront and getLength in Linked List Add some of the students to the list using addToFront

             //print out the length run your program and make sure it equals the number of students before continuing

            

             myList.addToFront(stud1);

             myList.addToFront(stud2);

             System.out.println("Length : "+myList.getLength());

            

             //TODO 5: Implement toString in Node and LinkedList. The Node toString should just print out the data field of the node. Note that toString is implemented in Student, so this should be easy-- creating a string out of the data should invoke the toString in Student (i.e. if I have a student instance named A saying String x= ""+A; will invoke A's toString. Alternatively you can explicitly call A.toString() from within Node (but understand why ""+A will work). Then implement toString in LinkedList. If one prints out a list with two students (Eugenio and Daniqua), with Danica added to the front first and Eugenio added second our list should look as follows head->name: Eugenio id: 700555555->name: Daniqua id: 700444444->null note the spaces. Print out your list to ensure it works.

             System.out.println("List contents : \n"+myList);

             //TODO 6: Implement addToEnd in LinkedList and then add another Student, but this time to the end, and print out the length (should be one more) and the list to ensure it added the student to the end.

             myList.addToEnd(stud3);

             System.out.println("Length : "+myList.getLength()+"\nList contents : \n"+myList);

             //TODO 7: Implement removeFromFront in LinkedList and print out what is returned, should be the first student in the list. Then print out the length (should be one less than what you had previously). Finally print out the length to ensure it is correct using getLength

             Student retStud = myList.removeFromFront();

             System.out.println("Student removed from Front : "+retStud);

             System.out.println("Length : "+myList.getLength()+"\n List contents : \n"+myList);

             //TODO 8: Implement removeFromEnd in LinkedList and print out what is returned. it should be the student who was at the end of the list, furthermore print out the list and the length to ensure it has the right students in it. Note that this function is a bit more involved, you may need to use 3 references as you iterate through the list (think about why?). Make sure your algorithm will work with a list that is of length 1, length 2, length 3, and some big length like 5

             retStud = myList.removeFromEnd();

             System.out.println("Student removed from End : "+retStud);

             System.out.println("Length : "+myList.getLength()+"\n List contents : \n"+myList);

             //TODO 9: Implement removeTarget in LinkedList. This should rely on the the Type T's equals. Remember our T, though it's generic, is guaranteed to have this function because it's guaranteed to be an object. In Student's case two students are equal if their student ID's are equal. Test removeTarget with a student in the list to ensure that removeTarget returns the right value when you test (and removes the actual student from the list when appropriate).

             boolean targetRemoved = myList.removeTarget(stud1);

            

             if(targetRemoved)

                    System.out.println("Student : "+stud1+" removed ");

             else

                    System.out.println("Student : "+stud1+" not found ");

            

             System.out.println("Length : "+myList.getLength()+"\n List contents : \n"+myList);

// add stud4 and stud5 to end of linked list to implement reverseList

             myList.addToEnd(stud4);

             myList.addToEnd(stud5);

             System.out.println("Length : "+myList.getLength()+"\n List contents : \n"+myList);

             //TODO 10: Implement reverseList in LinkedList. You need to reverse the list in place, i.e., with out allocating data into array.

             myList.reverseList();

            

             System.out.println("Reversed List:\nLength : "+myList.getLength()+"\n List contents : \n"+myList);

       }

}

//end of Main.java

Output:


Related Solutions

Can someone please show me how to do the work on this please. Question The transactions...
Can someone please show me how to do the work on this please. Question The transactions of the Fury Delivery Service are recorded in the general journal below. Instructions: 1. Post the journal entries to the attached general ledger “T” accounts. 2. Prepare a trial balance on the form provided after the “T” accounts. General Journal Date Account Titles and Explanation Debit Credit 2017 Sept. 1 Cash Common Stock (Stockholders invested cash in business) 25,000 25,000 4 Equipment Cash Notes...
Can someone please assist me with this question. The information that I found was not of...
Can someone please assist me with this question. The information that I found was not of help. Discuss how using systems thinking will influence your decision-making. I have to do a full paragraph on this question.
Can someone please explain the following program to me? I have ran it, but do not...
Can someone please explain the following program to me? I have ran it, but do not understand how the answers are being derived. #include<iostream> using namespace std; int num1 = 1; int quiz(int num) { static int n1 = 10; int n2 = 20; n1--; n2++; num1++; if (num == 1) return num1; else if (num < 3) return n1; else return n2; } // quiz main() { cout << quiz(num1) << endl; cout << quiz(num1) << endl; cout <<...
Can someone please teach me how to do these? 5. Suppose you save $19,000 per year...
Can someone please teach me how to do these? 5. Suppose you save $19,000 per year in an ordinary annuity promising you an interest rate of i=7.625% compounded once per year. How much will you have after 35 years? 6. A risk-free bond will pay you $1,000 in 1 year. The annual discount rate is i=19.69% compounded annually.  What is the bond’s present value? 7. A risk-free bond will pay you $1,000 in 2 years and nothing in between. The annual...
Small Business Management- Can you please assist me with answering this question. Please do explanation in...
Small Business Management- Can you please assist me with answering this question. Please do explanation in discussion is thorough. Kindly include citation in the discussion where necessary because answer will not be valid.  Also please provide reference. Thank you so much. I will really appreciate this. Again thank you. 1. Discuss the “Big Three” of cash management including its basic principles. Value 10 points
Can someone please assist me with this assignment? Do an economic analysis of two giant competitor...
Can someone please assist me with this assignment? Do an economic analysis of two giant competitor brands, Coke and Pepsi, in the context of them being rivals in the "Twenty-First Century" and use all the knowledge you have gathered over the last several weeks. Please do not make it a financial case.  It is to be an economics case study, utilizing the economic model of pure competition, monopolistic competition, oligopoly or monopoly. Use the most recent sources to make it more...
Can someone show me how to break this down by steps please and thank you. You...
Can someone show me how to break this down by steps please and thank you. You sell short 200 shares of a stock at $95 on a 60% initial margin requirement with a 30% maintenance margin. In 3 months, the stock is $79. What is your margin at this time? When would a margin call occur? If you cover your position when the stock is $79, what would be your HPR? Assume you did the same trade as before but...
Can someone answer these questions for me please? 1- You are tasked with teaching a child...
Can someone answer these questions for me please? 1- You are tasked with teaching a child to stop hitting other children. Using the principles of operant conditioning, devise a strategy that uses both reinforcement and punishment to prevent the child from misbehaving. What might be the possible reasons that the child learned this aggressive behaviour? What could be done to prevent it in the future?
Can someone please give me an example of four types of internet standards? Also, can you...
Can someone please give me an example of four types of internet standards? Also, can you compare and contrast two of those standards? Thank you for your time and help
you need to submit the following files: Main.java Additionally, you need to download file ‘letter_count.csv’, that...
you need to submit the following files: Main.java Additionally, you need to download file ‘letter_count.csv’, that contains counts for characters in a text (see submission folder on iCollege) and put it into the root of related Eclipse project folder. To view your project folder through system file explorer, right-click on ‘src’ folder in the Eclipse project explorer and choose ‘Show In->System Explorer’. Consider the following Java code, that reads a .csv file: BufferedReader csvReader = new BufferedReader(new FileReader("letter_count.csv")); String currentRow...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT