I have 2 classes. 1 class sets & gets a students' name, 3 test scores for a student & an average for the 3 scores. The other class has a main method to set and get this information. I'm getting an error from my IDE for the 2nd class only.
public class Student5th
{ /**
Instance variables
Each student object will have a name and three test scores
*/
private String name; //Student name
private int test1; //Score on test 1
private int test2; //Score on test 2
private int test3; //Score on test 3
/**
Set a student's name
Preconditions -- nm is not empty
Postconditions -- name has been set to name
*/
public void setName (String nm)
{
name = nm;
}
/** Get a student's name
Preconditions -- none
Postconditions -- returns the name
*/
public String getName ()
{
return name;
}
/** Set the score on the indicated test
Preconditions -- 1 <= i <= 3
-- 0 <= score <= 100
Postconditions -- test i has been set to score
*/
public void setScore (int i, int score)
{
if (i == 1) test1 = score;
else if (i == 2) test2 = score;
else test3 = score;
}
/** Get the score on the indicated test
* Preconditions -- none
* Postconditions -- returns the score on test I
* */
public int getScore (int i)
{
if (i == 1) return test1;
else if (i == 2) return test2;
else return test3;
}
/** Compute and return a student's average
* Preconditions -- none
* Postconditions -- returns the average of the test scores
* */
public int getAverage() {
int average;
average = (int) Math.round((test1 + test2 + test3) / 3.0);
return average;
}
}
My 2nd class with the main method...
import java.util.*;
public class TestStudent
{
public static void main (String[] args)
{
Scanner console = new Scanner(System.in);
private Student **student1**;
private Student **student2**;
String s;
int t;
student1 = new Student();
student2 = new Student();
s = console.next();
student1.setName (s);
t = console.nextInt();
student1.setScore (1, t);
student1.setScore (2, console.nextInt());
student1.setScore (3, console.nextInt());
student2.setName (**keyboard**.readLine());
student2.setScore (1, console.nextInt());
student2.setScore (2, console.nextInt());
student2.setScore (3, console.nextInt());
}
}
I've bolded (well, put double asterisks around) the parts which are giving me errors. I'm close to getting this program to work, but I don't know why my IDE is giving me problems for student1 & student2 in the 2nd class, as well as giving me a problem for (keyboard.readLine()); for student2.setName in the 2nd class?