In: Advanced Math
Write the class MultiDimList according to the following requirements:
The List will have the following:
Implement and test the following methods:
Input File:
Number of students
Name, ID, Mark separated by space
Testing:
Marking Scheme:
5 Marks/method.
5 Marks for the main method.
5 Marks creativity.
Total out of 40.
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;
}
}
}
}