Hello,
We were assigned a hw to come up with a linked list implementation for grades. We are supposed to ask the user how many students there are and then ask for 4 grades for each student and then come up with an avg in 2 different ways i.e sum/4 and 1st0.2+2nd0.3+3rd0.3+4th0.2. i was able to get it to work when i didnt have the user input the numbers (using just a name.add(new Student("Darrele Revis",100,99,94,90));)
but i am stuck when i do ask for users to input values. Feedback really aapreciated.
import java.util.*;
public class n
{
public static void main(String []args)
{
int num,grd1,grd2,grd3,grd4;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of Students: ");
num = input.nextInt();
for (int i=1;i<=num;i++)
{
System.out.println("Enter the grades for Student "+ i +": ");
System.out.println("Grade 1: ");
grd1 = input.nextInt();
System.out.println("Grade 2: ");
grd2= input.nextInt();
System.out.println("Grade 3: ");
grd3 = input.nextInt();
System.out.println("Grade 4: ");
grd4= input.nextInt();
StudentList name = new StudentList();
}
}
public static class StudentList
{
private StudentNode list;
public StudentList()
{
list = null;
}
public void add (Student stu)
{
StudentNode node = new StudentNode(stu);
StudentNode current;
if (list==null)
list = node;
else
{
current = list;
while (current.next != null)
current = current.next;
current.next = node;
}
}
public String toString()
{
String result = "";
StudentNode current = list;
while (current != null)
{
result += current.student.title + "\t" + current.student.grd1 + "\t"+ current.student.grd2 + "\t"
+ current.student.grd3 + "\t"+ current.student.grd4 +"\t" + current.student.avg1 +"\t" + current.student.avg2 +"\n\n";
current = current.next;
}
return result;
}
private static class StudentNode
{
public Student student;
public StudentNode next;
public StudentNode(Student stu)
{
student = stu;
next = null;
}
}
}
public static class Student
{
private String title;
int grd1;
int grd2;
int grd3;
int grd4;
double avg1;
double avg2;
public Student(String newTitle, int a, int b, int c, int d)
{
title = newTitle;
grd1 = a;
grd2 = b;
grd3 = c;
grd4 = d;
avg1 = (grd1+grd2+grd3+grd4)/4.;
avg2 = ((0.2*grd1)+(0.3*grd2)+(0.3*grd3)+(0.2*grd4));
}
public String toString()
{
return title;
}
}
}