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

in java This class will require the following fields: Node<T> head: the first Node in the...
in java This class will require the following fields: Node<T> head: the first Node in the chain Node<T> tail: the last Node in the chain int size: Keeps a count of the number of Nodes in the chain Your LinkedList class must also support the following public methods. LinkedList(): A default constructor sets both pointers to null and sets the size to 0. int size(): Returns the size of the LinkedList. void push_back(T): Creates a new Node and assigns it...
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  ...
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. . . . . . . . . . . . . . . . . . . . . . . ....
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....
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
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...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
Write a Circle Class that has the following fields: • radius: a double • PI: a...
Write a Circle Class that has the following fields: • radius: a double • PI: a final double initialized with the value 3.14159 • Constructor. Accepts the radius of the circle as an argument • Constructor. A no-arg constructor that sets the radius field to 0.0. • setRadius. A mutator method for the radius field. • getRadius. An accessor method for the radius field. • getArea. Returns the area of the circle, which is calculated as area = PI *...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT