Question

In: Advanced Math

Write the class MultiDimList according to the following requirements: 1. Each node contains 3 fields of...

Write the class MultiDimList according to the following requirements:

1. Each node contains 3 fields of data (Name, ID, Mark out of 100).
2. Each node will have two pointer: nextMark, nextName
3. The nodes can be sorted in an ascending way based on the Mark or the Name

The List will have the following:

FirstMark : a Pointer to point to the first node based on the Mark sorting scheme
FirstName: a Pointer to point to the first node based on the Name (alphabetical) scheme

Implement and test the following methods:

1. Add (input is a node).
2. Remove (using Name an input)
3. Display (displays the list in order by Name, Mark based on the user’s input)
4. countGrade (the input is the Grade (A,B,C or D) returns a count of the number of students who received the passedgrade. Use the following criteria: A (>= 90), B (80-89), C (70-80) or D (below 70))
5. printBelowAverage (displays a list of students who received below average mark).
6. replaceMark (input is ID and mark).

Input File:

Number of students

Name, ID, Mark separated by space

Testing:

1. Write a driver method that tests all the methods you wrote.
2. You could include a menu that gives the options for users to choose from.

Marking Scheme:

5 Marks/method.

5 Marks for the main method.

5 Marks creativity.

Total out of 40.

Solutions

Expert Solution

SOLUTION

JAVA PROGRAM

import java.util.Scanner;

class Node

{

String Name;

String ID;

int Mark;

public Node nextMark;

public Node nextName;

public Node()

{

ID="";

Name="";

nextMark=null;

nextName=null;

}

public Node(String Name, String ID)

{

this.ID=ID;

this.Name=Name;

nextMark=null;

nextName=null;

}

public String getName()

{

return Name;

}

public String getID()

{

return ID;

}

  

public void setName(String Name)

{

this.Name=Name;

}

public int getMark()

{

return Mark;

}

  

public void setMark(int Mark)

{

this.Mark=Mark;

}

public void setID(String ID)

{

this.ID=ID;

}

}

public class MultiDimList {

Node FirstMark;

Node FirstName;

public static int size;

public MultiDimList()

{

FirstMark=null;

FirstName=null;

size=0;

}

public static void main(String[] args) {

MultiDimList ML=new MultiDimList();

int choice=0;

while(choice<=6)

{

System.out.println("MENU");

System.out.println("1. Add a Node");

System.out.println("2. Remove a Node");

System.out.println("3. Display list");

System.out.println("4. countGrade");

System.out.println("5. calculateDistance");

System.out.println("6. printAboveAverage");

System.out.println("Enter your choice");

Scanner scan=new Scanner(System.in);

choice=scan.nextInt();

switch(choice)

{

case 1: Node N=new Node();

System.out.println("Enter a name to add");

N.Name=scan.next();

System.out.println("Enter a mark ");

N.Mark=scan.nextInt();

System.out.println("Enter a ID");

N.ID=scan.next();

N.nextMark=null;

N.nextName=null;

Add(N,ML);

break;

case 2: System.out.println("Enter a name to remove");

String name=scan.next();

Remove(name,ML); break;

case 3: print(ML);break;

case 4: System.out.println("Enter a grade");

String grade=scan.next();

int count=countGrade(ML,grade);

if(count==0)

System.out.println("No student is available with this grade");

else

System.out.println("Number of students with this grade: "+count);

break;

case 5: System.out.println("Enter two marks one by one to calculate distance b/w them");

int mark1=scan.nextInt();

int mark2=scan.nextInt();

int d=calculateDistance(ML,mark1, mark2);

if(d==-1)

{

System.out.println("One of the mark you entered is not present in this list");

}

else

System.out.println("Distance between two marks: "+d);

break;

case 6: printAboveAverage(ML);break;

}

}

  

}

public static void Add(Node N,MultiDimList ML)

{

if(ML.FirstMark==null)

{

ML.FirstMark=new Node();

ML.FirstMark=N;

}

else

{

Node ptr=ML.FirstMark;

Node ptr1=ptr;

while(ptr!=null)

{

if(N.getMark()<=ptr.getMark())

{

break;

}

ptr1=ptr;

ptr=ptr.nextMark;

}

ptr1.nextMark=N;

}

if(ML.FirstName==null)

{

ML.FirstName=new Node();

ML.FirstName=N;

}

else

{

Node ptr=ML.FirstName;

Node ptr1=ptr;

while(ptr!=null)

{

int v=N.getName().compareTo(ptr.getName());

if(v>=0)

{

break;

}

ptr1=ptr;

ptr=ptr.nextName;

}

ptr1.nextName=N;

}

size++;

System.out.println("Student added in the list");

}

public static void Remove(String Name,MultiDimList ML )

{

Node ptr=ML.FirstName;

Node ptr1=ptr;

while(ptr!=null)

{

if(ptr.getName().equalsIgnoreCase(Name))

{

break;

}

ptr1=ptr;

ptr=ptr.nextName;

}

ptr1.nextName=ptr.nextName;

System.out.println("Student: "+Name+" is removed from list");

size--;

}

public static void print(MultiDimList ML)

{

System.out.println("Display list by Mark / Name ? Enter <1/2>");

Scanner scan=new Scanner(System.in);

int ch=scan.nextInt();

if(ch==1)

{

System.out.println("Display list by Mark");

Node ptr=ML.FirstMark;

while(ptr!=null)

{

System.out.println("Mark: "+ptr.getMark()+" ID: "+ptr.getID()+" Name: "+ptr.getName());

ptr=ptr.nextMark;

}

}

else if(ch==2)

{

System.out.println("Display list by Name");

Node ptr=ML.FirstName;

while(ptr!=null)

{

System.out.println("Name: "+ptr.getName()+" ID: "+ptr.getID()+" Mark: "+ptr.getMark());

ptr=ptr.nextMark;

}

}

}

public static int countGrade(MultiDimList ML, String grade)

{

int count=0;

String g=null;

Node ptr=ML.FirstMark;

while(ptr!=null)

{ if(ptr.getMark()>70)

{

if(ptr.getMark()>=90)

g="A";

else if(ptr.getMark()>=80 && ptr.getMark()<=89)

g="B";

else if(ptr.getMark()>=70 && ptr.getMark()<=80)

g="C";

}

else if(ptr.getMark()<70)

{

g="D";

}

if(grade.equalsIgnoreCase(g))

count++;

ptr=ptr.nextMark;

}

return count;

}

public static int calculateDistance(MultiDimList ML,int mark1, int mark2)

{

int distance=0;

Node ptr=ML.FirstMark;

boolean flag1=false;

boolean flag2=false;

while(ptr!=null)

{

if(mark1==ptr.getMark())

{

flag1=true;

Node p=ptr.nextMark;

distance++;

while(p!=null)

{

if(mark2==ptr.getMark())

{

flag2=true;

break;

}

else

distance++;

p=p.nextMark;

}

}

ptr=ptr.nextMark;

}

if(!(flag1==true && flag2==true))

distance=-1;

return distance;

}

public static void printAboveAverage(MultiDimList ML)

{

double average=0;

Node ptr=ML.FirstMark;

while(ptr!=null)

{

average+=ptr.getMark();

ptr=ptr.nextMark;

}

average/=size;

ptr=ML.FirstMark;

System.out.println("Average value: "+average);

System.out.println("Printing Students who are above the average");

while(ptr!=null)

{

if(ptr.getMark()>average)

{

System.out.println("Mark: "+ptr.getMark()+" ID: "+ptr.getID()+" Name: "+ptr.getName());

ptr=ptr.nextMark;

}

}

}

}


Related Solutions

Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file Electricity.txt and read each column into an array (8 arrays total). 2.   Also create 2 arrays for the following: Total Fossil Fuel Energy (sum of all fossil fuels) Total Renewable Energy (sum of all renewable sources) Electricity.txt: Net generation United States all sectors monthly https://www.eia.gov/electricity/data/browser/ Source: U.S. Energy Information Administration All values in thousands of megawatthours Year   all fuels   coal       natural gas   nuclear  ...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3 integers which represent the sides of a triangle. Returns true if the triangle is isosceles and false otherwise. 2. perimeter - accepts 3 integers that represent the sides of a triangle and returns the perimeter of the triangle. 3. area -- accepts 3 integers, which represent the sides of a triangle and calculates and returns the area of the triangle. Hint: use Heron's formula....
Requirements? 1, 2 and 3. Classify each of these costs according to its category in the...
Requirements? 1, 2 and 3. Classify each of these costs according to its category in the value chain and further breakdown production costs into three? subcategories: Direct Materials? (DM), Direct Labor? (DL), or Manufacturing Overhead? (MOH). Compute the total costs for each value chain category. ?(Leave cells blank that do not require numerical? inputs.) Plant janitors' wages. . . . . . . . . . . . . . . . . . . . . . . ....
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Consider a Class C network 200.100.100.0. and divide it into 10 subnets, according the following requirements...
Consider a Class C network 200.100.100.0. and divide it into 10 subnets, according the following requirements of number of hosts on each subnet. Draw a diagram clearing labelling each subnet with its name (i.e. A, B, C etc.) and subnet ID. (Use variable length subnet mask) 30 Hosts 12 Hosts 28 Hosts 10 Hosts 25 Hosts 25 Hosts 30 Hosts 5 Hosts 15 Hosts 10 Hosts
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
Write a java program using the following information Design a LandTract class that has two fields...
Write a java program using the following information Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and...
Write a class Car that contains the following attributes: The name of car The direction of...
Write a class Car that contains the following attributes: The name of car The direction of car (E, W, N, S) The position of car (from imaginary zero point) The class has the following member functions: A constructor to initialize the attributes. Turn function to change the direction of car to one step right side (e.g. if the direction is to E,it should be changed to S and so on.) Overload the Turn function to change the direction to any...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT