Question

Using Java coding, complete the following: This program should implement a LinkedList data structure to handle...

Using Java coding, complete the following:

This program should implement a LinkedList data structure to handle the management of student records. It will need to be
able to store any number of students, using a Linked List.

LinearNode.java can be used to aid creating this program

Student should be a class defined with the following:
   • String name
   • int year
   • A linked list of college classes (college classes are represented using strings)
   • An ArrayList of grades (these are ints)
   • Getter/Setters for the private data
   • Method to determine average of all grades
   • ToString method to print students name, year, number of courses taken, and grade average as a
   letter

Also add a Main class that performs the following operations:
   • Add four students to the linked list
   • Add five grades to each student such that one student is an A, B, C, and D student (should be in
   descending order)
   • Print the second student using its toString()
   • Print the last student using its toString()
   • Remove the third student from the linked list
   • Print the last student using its toString()
   • Determine the average of all students first grade and print this

LinearNode.java

public class LinearNode<T> {

        private T element;                                      // element
        private LinearNode<T> next;                       // reference to next element
        
        public LinearNode ()                            // POST: empty node
        {       element = null;
                next = null;
        }
        
        public LinearNode (T elem)                      // POST: node set to element
        {       element = elem;
                next = null;    }
        
        // accessors and modifiers
        public LinearNode<T> getNext()
        {       return next;    }
        
        public void setNext (LinearNode<T> node)
        {       next = node;    }
        
        public T getElement ( )
        {       return element; }
        
        public void setElement (T elem)
        {       element = elem; }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Student.java

import java.util.ArrayList;

public class Student {
   // instance variables
   private String name;
   private int year;
   private String college;
   // ArrayList to store grades
   private ArrayList<Integer> grades;
   // default constructor
   Student() {
       super();
   }
   // parameterized constructor
   Student(String name, int year, String college, ArrayList<Integer> grades) {
       super();
       this.name = name;
       this.year = year;
       this.college = college;
       this.grades = grades;
   }
   // setter and getter methods
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getYear() {
       return year;
   }
   public void setYear(int year) {
       this.year = year;
   }
   public String getCollege() {
       return college;
   }
   public void setCollege(String college) {
       this.college = college;
   }
   public ArrayList<Integer> getGrades() {
       return grades;
   }
   public void setGrades(ArrayList<Integer> grades) {
       this.grades = grades;
   }
   // method to calculate average of all grades
   public double averageOfAllGrades() {
       double gradesTotal=0;
       for (Integer grade : grades)
           gradesTotal+=grade;
       return gradesTotal/grades.size();
   }
   @Override
   public String toString() {
       return "Student:"
               +"\nName: "+name
               +"\nYear: "+year
               +"\nNumber of courses taken: "+grades.size()
               +"\nGrades average: "+averageOfAllGrades()+"\n";
   }
}

Main.java

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Main {
   public static void main(String[] args) {
       // create a LinkedList of type Student
       List<Student> students=new LinkedList<>();
       // create array list to store grades
       ArrayList<Integer> grades=new ArrayList<>();
       grades.add(85);
       grades.add(91);
       grades.add(78);
       grades.add(83);
       grades.add(85);
       // create student object
       Student student1=new Student("Steve", 2, "FC College",grades);
       grades=new ArrayList<>();
       grades.add(75);
       grades.add(91);
       grades.add(78);
       grades.add(83);
       grades.add(81);
       Student student2=new Student("Rohit", 2, "NJ College",grades);
       grades=new ArrayList<>();
       grades.add(98);
       grades.add(71);
       grades.add(91);
       grades.add(78);
       grades.add(83);
       Student student3=new Student("Tony", 3, "SD College",grades);
       grades=new ArrayList<>();
       grades.add(77);
       grades.add(75);
       grades.add(91);
       grades.add(78);
       grades.add(83);
       Student student4=new Student("John", 1, "PN College",grades);
       // add all four students into linked list
       students.add(student1);
       students.add(student2);
       students.add(student3);
       students.add(student4);
       // print second student from linked list
       // -- get(1) means second position because index starts from 0
       System.out.println("Second student\n"+students.get(1));
       // print last student
       System.out.println("Last student:\n"+students.get(students.size()-1));
       students.remove(2); // remove third student
       // again print last student
       System.out.println("Last student:\n"+students.get(students.size()-1));
       double sum=0;
       // calculate average of all students first grade
       for (Student stud : students) {
           sum+=stud.getGrades().get(0);
       }      
       // print average of all students first grade
       System.out.println("\nAverage of all students first grade: "+sum/students.size());
   }
}

Output

Add a comment
Know the answer?
Add Answer to:
Using Java coding, complete the following: This program should implement a LinkedList data structure to handle...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These...

    In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...

  • When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...

    When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> {    Node<T> itsFirstNode;    Node<T> itsLastNode;    private int size;    public LinkedList() {        itsFirstNode = null;        itsLastNode = null;          size = 0;    }    public Iterator<T> getIterator() {        return new Iterator(this);    }    // THIS WILL NEED...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

  • // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...

    // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test...

    Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT