2 Replies - 708 Views - Last Post: 05 May 2013 - 03:17 PM Rate Topic: -----

#1 cartertevin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-July 11

Need help with Linked List Project. VERY confused.

Posted 02 May 2013 - 08:34 AM

Hey guys I am doing my final project and I need help with the implementation of a Linked List. Our book only teaches us a minimal of what we need to know when dealing with linked list and I know our project goes beyond the scope of the book.
What I basically need to do is program an OOP Java program that basically collects 4 grades for a defined number of students [as defined by the user]. I got the main function pretty much working, and the program prompts [via for loop] the user for the number of students and then runs through another for loop and stores these students into an array and then that array is stored as a new link in my student linklist, and the process begins again until the for loop breaks. But when I run it I keep on getting overflow errors. Can someone please point me in the right direction, I am new to list and didn't really learn much from the book [it's the last chapter and only has about 8-10 pages dedicated to this].

Here is my code thus far [I don't have the calculate avg function working yet because I am still testing the functionality of my program]
---
package javafinalproject;
import java.util.Scanner;

public class JavaFinalProject {
   
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
    	double grade[];
    	grade = new double[4];
    	int numStudent;
    	
    	StudentList student = new StudentList();
    	
    	
    	
    	System.out.println("Enter the number of Students: ");
    	
    	numStudent = input.nextInt();
    	
    	for(int i = 1; i <= numStudent; i++){
    		
    	System.out.println("Enter the grade for Student " + i);
    	
    		for(int j = 0; j < 4; j++){
    		
    		System.out.println("Grade" + j + ":");
    		grade[j] = input.nextDouble();
    		
    		
    		}
        	
    	  	student.add (new Student(grade[0],grade[1],grade[2],grade[3]));
	
    	}
        
    	student.printStatment();
    	
    	  	
	}
	    
//========================================================================================//
    
    
    }


---
package javafinalproject;
import java.util.Scanner;

public class Student {
    	
	    public  double grade1;
	    public  double grade2;
	    public  double grade3;
	    public  double grade4;
	    
	    
	    //Constructor 
	    public Student(double grade1, double grade2, double grade3, double grade4){
	        
	    	this.grade1 = grade1;
	        this.grade2 = grade2;
	        this.grade3 = grade3;
	        this.grade4 = grade4;
	    }
	    // Returns calculated grades
	    public void printStatment(){
	    	System.out.println(grade1 +  grade2 +  grade3 +  grade4);//Prints the class grade report THIS IS TEST
	    }
            
            public String toString(){
                return("");
                
            }
}



---
package javafinalproject;


public class StudentList  {
    
	
   private StudentNode list;
	    
   public StudentList(){
                list = null;          
      }
          
   public void add (Student std){

      StudentNode node = new StudentNode (std);
      StudentNode current, next;

      if (list == null)
         list = node;
      else
      {
         current = list;
         while (current.next != null)
            {current = current.next;}
         current.next = node;
      }
   }
   public void printStatment(){
    
     

      StudentNode current = list;

      while (current != null)
      {
         printStatment();
         current = current.next;
      }

      
   }
   
  

//*****************************************************************
   //  An inner class that represents a node in the magazine list.
   //  The public variables are accessed by the MagazineList class.
   //*****************************************************************
   private class StudentNode
   {
      public Student student;
      public StudentNode next;

      //--------------------------------------------------------------
      //  Sets up the node
      //--------------------------------------------------------------
      public StudentNode (Student std)
      {
         student = std;
         next = null;
      }
   }	

}	    	

	    
	   
	    



Why do I keep on getting StackOverFlow error? PLEASE any help or guidance will be MUCH appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with Linked List Project. VERY confused.

#2 cartertevin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-July 11

Re: Need help with Linked List Project. VERY confused.

Posted 05 May 2013 - 01:33 PM

Hey guys after a break and some MUCH needed sleep [I was up for 48 hours when I submitted this earlier], I finally got the project to work. In actuality it wasn't really that hard [took me like 1 hour to get it once I started up on it again today], I probably just needed some rest. But there is ONE problem. Each time I run this I end up getting the students grades and everything printed out normally, BUT for some reason the numbers and calculated grades come out BACKWARDS?!? Can anyone look into this and see why this is the case? This is something I am actually quite confused on. Anyways here is the new and fixed code....

package javafinalproject;
import java.util.*;
import java.text.DecimalFormat;

//============================================================================//
public class JavaFinalProject {
   
  public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
    	double grade[];
    	grade = new double[4];
    	int numStudent;
        int a = 1;
        
    	
    	StudentList student = new StudentList();
    
    	System.out.print("Enter the number of Students: ");
    	
    	numStudent = input.nextInt();
    	
    	for(int i = 1; i <= numStudent; i++){
    		
    	System.out.println("Enter the grade for Student " + i);
    	
    		for(int j = 0; j <= 3; j++)// Collects data for predefined number of students
                {
    		
    		System.out.println("Grade" + (j+1) + ":");
    		grade[j] = input.nextDouble();	
    		}student.add(grade[0], grade[1], grade[2], grade[3]); 
                
            }   
     	   student.printReport(a); //Prints a report of grades	
	}
    
//============================================================================//
    
  public static class Student {
            
            DecimalFormat fmt = new DecimalFormat("0.##");
       	    public double grade1;
	    public double grade2;
	    public double grade3;
	    public double grade4;
            public Student next;
            int i = 1;
            
	  
	    //Constructor 
	    public Student(double grade1, double grade2, double grade3, double grade4){
	        
	    	this.grade1 = grade1;
	        this.grade2 = grade2;
	        this.grade3 = grade3;
	        this.grade4 = grade4;
	    }
            
	    // Calculates grades and then returns the Student, Grades, and two calculated averages
	    public void printStatment(int i){
	    	
                double avg1 = (grade1 + grade2 + grade3 + grade4)/4;
                double avg2 = (0.2*grade1 + 0.3*grade2 + 0.3*grade3 + 0.2*grade4);
                
                //Prints the class grade report THIS IS TEST
                System.out.println("Student "  + i + "\t" + grade1 + "\t \t" + grade2 + "\t\t" +
                        grade3 + "\t\t" + grade4 + "\t\t" + fmt.format(avg1) + "\t\t" + fmt.format(avg2)); 
            }
    }

//============================================================================//

  public static class StudentList  {
   
   public Student firstStudent;
   private StudentNode list;
       
   public StudentList(){
       //sets list to null
       list = null;          
      }
          
   public void add (double grade1, double grade2, double grade3, double grade4){
       
      Student student =  new Student(grade1, grade2, grade3, grade4);
      
      student.next = firstStudent; 
      
      firstStudent = student;
      
      System.out.println("Student Grades added");
   }
   
      public void printReport(int i){
         
         Student student = firstStudent;
         
         System.out.println("Student[#]\t" + "Grade 1 \t" + "Grade 2 \t" + "Grade 3 \t" + "Grade 4 \t" 
                 + "Mean Average \t" + "Weighted Average \t" );
           
         while(student != null){
            student.printStatment(i);
            i++;
            student = student.next;  
         }       
           
      }
    
   }
//----------------------------------------------------------------------------//   
   //  An inner class that represents a node in the magazine list.
   //  The public variables are accessed by the MagazineList class.
   class StudentNode
   {
      public Student firstStudent;
      public StudentNode next;
  
      //  Sets up the node    
      public StudentNode (Student std)
      {
         firstStudent = std;
         next = null;
      }
   }	

}
//----------------------------------------------------------------------------//
//============================================================================//


Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Need help with Linked List Project. VERY confused.

Posted 05 May 2013 - 03:17 PM

I would do it differently.

The StudentList is a linked list of Student objects. Creating a StudentList should look something like this:

StudentList studentList = new StudentList();

The StudentList constructor should create an empty list with the first item in the list defined. For example, if your list has pointers to the firstStudent and the currentStudent, the first few lines of the StudentList class might look like:
    // class StudentList is a linked list of Student objects
    public static class StudentList
    {
        // two pointers to the first item and current item in the list
        // of Student objects
        public Student firstStudent;
        public Student currentStudent;

        // the default constructor that creates the first item in the list
        // and sets it to null
        public StudentList()
        {
            //sets the first item in the list to null
            firstStudent = null;

            // this line is optional but included for clarity
            currentStudent = null;
        }

        // . . . . etc.

The add() method either replaces the firstStudent item or appends a new Student object to the place in the list following the currentStudent:
        // method add() replaces the first item in the list if it is null,
        // otherwise adds a student to the list after the current item
        public void add ( Student student )
        {
            if ( firstStudent == null )
            {
                // replace the firstStudent with the new
                // student and update pointers
                firstStudent = student;
                firstStudent.next = null;
                currentStudent = firstStudent;
            }
            else
            {
                currentStudent.next = student;
                currentStudent = student;
                currentStudent.next = null;
            }

            System.out.println("Student Grades added");
        }

Note that the add() method accepts a Student object, not parameters to pass to the Student() constructor to create a new Student object. The call to add() could look like:

studentList.add( new Student( grade[0], grade[1], grade[2], grade[3] ) );

If you straighten this out, the results you expect will follow.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1