Question

In: Computer Science

import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new...

import java.util.LinkedList;

public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new LinkedList<Student>(); linkedlist.add(new Student("Ahmed Ali", 20111021, 18, 38, 38)); linkedlist.add(new Student("Sami Kamal", 20121021, 17, 39, 35)); linkedlist.add(new Student("Salem Salim", 20131021, 20, 40, 40)); linkedlist.add(new Student("Rami Mohammed", 20111031, 15, 35, 30)); linkedlist.add(new Student("Kim Joe", 20121024, 12, 32, 32)); linkedlist.addFirst(new Student("Hadi Ali", 20111025, 19, 38, 39)); linkedlist.addLast(new Student("Waleed Salim", 20131025, 10, 30, 30)); linkedlist.set(0, new Student("Khalid Ali", 20111027, 15, 30, 30)); linkedlist.removeFirst(); linkedlist.removeLast(); linkedlist.add(0, new Student("John Don", 20131025, 11, 31, 31)); linkedlist.remove(2); } class Student{ private String name; private Long ID; private int [] marks = new int [3];

public Student(String name, long ID, int quizzes, int mid, int fin) { this.name = name; this.ID = ID; marks[0] = quizes; marks[1] = mid; marks[2] = fin; } public String getName() { return name; }

  

public Long getID() { return ID; }

public int[] getMarks() { return marks; }

@Override public String toString() { String temp = "student: " + "name = " + name + ", ID = " + ID + ", marks = {" + marks[0] + ", " + marks[1] + ", " + marks[2] + "}"; return temp; } }

1- What is the content of the linkedList after the executing the following program?

2- Modify StudentLinkedList class by adding the following methods:  printStudentList: print by calling and printing “toString” of every object in the linkedList. Every student object to be printed in a separate line.  deleteStudentByID(long id): delete student object from the list whose ID is matching with the passed parameter.  sortListByID(): sort the linkedlist according to students IDs.  findMarksAverage(): find the average of all marks for all students in the list.  findMinMark(int markIndex): find the student with the minimum mark in a specific index: o 0: Quizzes o 1: Midterm Exam o 2: Final Exam

Solutions

Expert Solution

Code

import java.util.Collections;
import java.util.LinkedList;
import java.util.ListIterator;

public class StudentLinkedList
{
static LinkedList<Student> linkedlist = new LinkedList<Student>();
public static void main(String[] args)
{
  
linkedlist.add(new Student("Ahmed Ali", 20111021, 18, 38, 38));
linkedlist.add(new Student("Sami Kamal", 20121021, 17, 39, 35));
linkedlist.add(new Student("Salem Salim", 20131021, 20, 40, 40));
linkedlist.add(new Student("Rami Mohammed", 20111031, 15, 35, 30));
linkedlist.add(new Student("Kim Joe", 20121024, 12, 32, 32));
linkedlist.addFirst(new Student("Hadi Ali", 20111025, 19, 38, 39));
linkedlist.addLast(new Student("Waleed Salim", 20131025, 10, 30, 30));
linkedlist.set(0, new Student("Khalid Ali", 20111027, 15, 30, 30));
linkedlist.removeFirst(); linkedlist.removeLast();
linkedlist.add(0, new Student("John Don", 20131025, 11, 31, 31));
linkedlist.remove(2);
printStudentList();
System.out.println("\n\nAfter deleting the studet with id 2011103 list is.");
deleteStudentByID(20111031);
printStudentList();
sortListByID();
System.out.println("\n\nAfter sorted by the ID list is : ");
printStudentList();
findMarksAverage();
System.out.println("\n");
findMinMark(0);
System.out.println("\n");
findMinMark(1);
System.out.println("\n");
findMinMark(2);
}

private static void printStudentList()
{
for(int num=0; num<linkedlist.size(); num++)
{
System.out.println(linkedlist.get(num));
}
}

private static void deleteStudentByID(int id)
{
for(int num=0; num<linkedlist.size(); num++)
{
if(linkedlist.get(num).getID()==id)
{
linkedlist.remove(num);
return;
}
}
}

private static void sortListByID() {
Collections.sort(linkedlist);
}

private static void findMarksAverage() {
double sum=0;
for(int num=0; num<linkedlist.size(); num++)
{
int studentMarkSum[]=linkedlist.get(num).getMarks();
sum+=(studentMarkSum[0]+studentMarkSum[1]+studentMarkSum[2]);
}
System.out.println("\nThe average marks of all student is : "+sum/linkedlist.size());
}

private static void findMinMark(int i) {
int minMarks,minId=0;
int marks[]=linkedlist.get(0).getMarks();
switch(i)
{
  
case 0:
minMarks=marks[0];
for(int num=0; num<linkedlist.size(); num++)
{
marks=linkedlist.get(num).getMarks();

if(marks[0]<minMarks)
{
minMarks=marks[0];
minId=num;
}
}
System.out.println(linkedlist.get(minId).getName()+" has minimum marks in Quizzes is "+minMarks);
break;
case 1:
minMarks=marks[1];
for(int num=0; num<linkedlist.size(); num++)
{
marks=linkedlist.get(num).getMarks();
if(marks[1]<minMarks)
{
minMarks=marks[1];
minId=num;
}
}
System.out.println(linkedlist.get(minId).getName()+" has minimum marks in Midterm is "+minMarks);
break;
case 2:
minMarks=marks[2];
for(int num=0; num<linkedlist.size(); num++)
{
marks=linkedlist.get(num).getMarks();
if(marks[2]<minMarks)
{
minMarks=marks[2];
minId=num;
}
}
System.out.println(linkedlist.get(minId).getName()+" has minimum marks in Final Exam is "+minMarks);
break;
}
}
  
}
class Student implements Comparable<Student>
{
private String name;
private Long ID;
private int [] marks = new int [3];

public Student(String name, long ID, int quizzes, int mid, int fin)
{
this.name = name;
this.ID = ID;
marks[0] = quizzes;
marks[1] = mid;
marks[2] = fin;
}
public String getName() { return name; }

public Long getID() { return ID; }

public int[] getMarks() { return marks; }
  
@Override
public int compareTo(Student o)
{
       Long comparedSize = o.getID();
       if (this.ID > comparedSize) {
           return 1;
       } else if (this.ID == comparedSize) {
           return 0;
       } else {
           return -1;
       }
   }
  
@Override public String toString()
{
String temp = "student: " + "name = " + name + ", ID = " + ID + ", marks = {" + marks[0] + ", " + marks[1] + ", " + marks[2] + "}"; return temp;
}
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num=0; String s = ""; while (true) { System.out.println("Enter an integer greater than 1: "); try { // reading input s = sc.nextLine(); // converting into int num = Integer.parseInt(s); break; } catch (Exception e) { System.out.println(s + " is not valid input."); } } // Now we have a valid number // putting into square int square = num; int count...
import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new...
import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new Scanner(System.in);           boolean gameOver = false; int colSize = 10; int rowSize= 10; String floorTile= "."; int playerX = 0; int playerY= 0; String playerTile="@"; int exitX= colSize-1; int exitY= rowSize-1; String exitTile="# "; int zombieX=5; int zombieY=5; // Defining Second Zombie int zombie2Y= 8; int zombie2X= 3; // Defining third zombie int zombie3Y= 1; int zombie3X= 7; String zombieTile="*"; String zombie2Tile="*";...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // parse the number of strings int numStrings = Integer.parseInt(sc.nextLine());    // parse each string String[] stringsArray = new String[numStrings]; for (int i = 0; i < numStrings; i++) { stringsArray[i] = sc.nextLine(); }    // print whether there are duplicates System.out.println(hasDuplicates(stringsArray)); }    private static boolean hasDuplicates(String[] stringsArray) { // TODO fill this in and remove the below line...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT