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;
}
}
}

Edited 3 Years Ago by harshd89

It looks like you aren't actually calling name.add() anywhere in the main function. Only reading the input into local variables.

I think line 21 StudentList name = new StudentList(); should be outside the loop, and this is the spot where you need to call the add() function.

Thanks dude that did help but now i m stuck again. We are supposed to get a list of students and averages after we are done inputting the grades. My program is outputting the average for student 1 and then asking for grades for student 2. The prof just spent 1 lectue on it and just gave us a sample program without user inputs so having difficulties. I tried a whole bunch of things (like taking the lines from StudentList name = new StudentList on to System.out (name)) but couldnt get it to work. Feedback greatly appreciated.

import java.util.*;
public class n
{
public static void main(String []args)
{
int num,gd1,gd2,gd3,gd4;
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: ");
gd1 = input.nextInt();
System.out.println("Grade 2: ");
gd2= input.nextInt();
System.out.println("Grade 3: ");
gd3 = input.nextInt();
System.out.println("Grade 4: ");
gd4= input.nextInt();
StudentList name = new StudentList();
name.add(new Student("Student " ,gd1,gd2,gd3,gd4));
System.out.println("Student Name" + "\t" + "Grade 1"+ "\t" +"Grade 2"+ "\t" +"Grade 3"+ "\t" +"Grade 4"+ "\t" + "Average" +"\t" + "Weighted");
System.out.println();
System.out.println(name);
}

}
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;
}
}
}

Edited 3 Years Ago by harshd89

OK. So a little clarification on linked lists, because I can see where you are having trouble.

You have the list declaration inside the loop, which means each time you input for a new student you are actually creating a whole new list (not just adding to the existing list). In the same way, your output needs to be outside the loop in order to output once for the entire list and not for each item during the input process.

So logically (in pseudocode) it would look like this.

instantiate list
loop through students
    collect user input for each student
    add student to list
end loop
ouput list

Hopefully that makes sense, and should be enough to help modify your code so that it works - you're really close.

This article has been dead for over six months. Start a new discussion instead.