Working on some basic code and I dont get why Test 1 and Test 2 wont resolve when I call the getAverage function in the main method....here are my classes, can someone please tell me what the problem is? Or point me in the right direction?
Code:
import java.util.Scanner;
public class Student
{
Scanner input = new Scanner(System.in);
// declare instance data
private String studentName;
private int test1;
private int test2;
//constructor
public Student(String sName)
{
sName = studentName; // add body of constructor
}
// inputGrades: prompt for and read in student's grades for test1 and test2.
// Use name in prompts, e.g., "Enter's Joe's score for test1".
public void inputGrades()
{
System.out.println("Enter "+studentName+"'s score for test #1");
test1 = input.nextInt();
while(test1<0 || test1>100)
{
System.out.println("Invalid number. Enter "+studentName+"'s score for test #1");
}
System.out.println("Enter "+studentName+"'s score for test #2");
test2 = input.nextInt();
while(test2<0 || test2>100)
{
System.out.println("Invalid number. Enter "+studentName+"'s score for test #2");
}
// add body of inputGrades
}
// getAverage: compute and return the student's test average as a double
public double getAverage(int test1, int test2) // add header for getAverage
{
double average = (test1+test2)/2.0; // add body of getAverage
return average;
}
// printName: print the student's name
public void printName() // add header for printName
{
System.out.print(studentName);// add body of printName
}
Code:
import java.util.*;
public class Grades
{
public static void main(String[] args)
{
Student student1 = new Student("Mary");
Student student2 = new Student("Mike");//create student2, "Mike"
student1.inputGrades();//input grades for Mary
System.out.print("The average for Mary is " +student1.getAverage(test1, test2)); //print average for Mary
System.out.println();
//student2.inputGrades();//input grades for Mike
System.out.print("The average for Mike is " +student2.inputGrades().getAverage(test1, test2)); //print average for Mike
}
}
Comment