So the problem comes in, in the last method called printComparisonResults. I just have no clue what to do?? Thanks for your time!


My Class:

//StudentRecord
 //Nick Elliott 02/01/10
 
 import java.util.Scanner;
 import java.io.*;


 
 // A StudentRecord contains a student's first name and their gpa
 public class StudentRecord
 {
 	private String name;
	private double gpa;
	
	//initialize record given to values
	public StudentRecord(String theName, double theGpa)
	{
		this.setName(theName);
		this.setGpa(theGpa);
	}
	// initialize the empty record
	public StudentRecord( )
	{
		this("",0);
	}
	// initialize record to values given in string
	public StudentRecord(String recordValues)
	{
		Scanner scanner = new Scanner(recordValues);
		this.setName(scanner.next());
		this.setGpa(scanner.nextDouble());
	}
	// name is set to theName
	public void setName(String theName)
	{
		this.name = theName;
	}
	// gpa is set to theGpa
	public void setGpa(double theGpa)
	{
		if(theGpa < 0.0 || theGpa > 4.0)
		{
			throw new RuntimeException(theGpa + " is out of the range, 0.0 - 4.0");
		}
		
		this.gpa = theGpa;
	}
	// returns name
	public String getName( )
	{
		return this.name;
	}
	// returns gpa
	public double getGpa( )
	{
		return this.gpa;
	}
	//returns string representation of record
	public String toString( )
	{
		return this.name + " " + this.gpa;
	}
}

Here is my application:

// FindAverageGpa
// Nick Elliott 2/2/10

import java.io.*;
import java.util.Scanner;

public class FindAverageGpa
{
	// read in a list of StudentRecords
	// print out the average gpa and whether each student's gpa is >= or <average
	public static void main(String[]args)throws IOException
	{
		final int MAX_SIZE = 10;
		StudentRecord[] student = new StudentRecord[ MAX_SIZE ];
		
		System.out.println(" Type in a list of students names and their Gpa's"
		+ " \nPress enter at blan line to stop");

		int size = readRecords( student );
		
		double averageGpa = calculateAverageGpa( student, size);
		
		System.out.println(" the average gpa is " + averageGpa);

		printComparisonResults( student, size, averageGpa);

}
	// interactively read in any number of StudentRecords (name gpa) into the student array
	// return the number of records read and stored
	public static int readRecords( StudentRecord[]student) throws IOException
	{
		Scanner scanner = new Scanner(System.in);

		String line = null;
		int count = 0;
		while(true)
		{
			line = scanner.nextLine();
			
			if(line.equals(""))
				break;
			
			data[count] = new StudentRecord (line);
			
			count++;
		}
		return count; // number of data elements stored
	}

	// returns the average gpa of the records stored in student[0...size-]
	public static double calculateAverageGpa(StudentRecord[]student, int size)
	{
		double sum = 0.0;
		int count = 0;

		while(count < size)
		{
			sum = sum + student[count].getGpa();
			count++;

		}
		double averageGpa = sum/size;
		return averageGpa;
	}
	
	// print out each StudentRecord and say whether that Student's gpa is >= or < averageGpa
	public static void printComparisonReults(StudentRecord[]student, int size, double averageGpa)
	{
		if(StudentRecord[theGpa] >= averageGpa) //this line has a problem
		{
			System.out.println(" The student record is greater than or equal to the average gpa.");
		}
		else
		{
			if(StudentRecord[theGpa] < averageGpa)//this line has a problem
			{
				System.out.println(" The student record is less than the average gpa.");
			}
		}
	}
}
// print out each StudentRecord and say whether that Student's gpa is >= or < averageGpa
	public static void printComparisonReults(StudentRecord[]student, int size, double averageGpa)
	{
		if(StudentRecord[theGpa] >= averageGpa) //this line has a problem
		{
			System.out.println(" The student record is greater than or equal to the average gpa.");
		}
		else
		{
			if(StudentRecord[theGpa] < averageGpa)//this line has a problem
			{
				System.out.println(" The student record is less than the average gpa.");
			}
		}
	}

First, the variable theGpa does not exist within this method. Second, from the comments, it appears that you want to iterate through each of the student records. Try wrapping a for loop around your if statements and use the loop variable in lieu of theGpa. Lastly, StudentRecord is not an array of doubles, it's not even an array. It's a type. Your array is student of type StudentRecord.

Keep at it.

Edited 6 Years Ago by apegram: n/a

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