Reputation: 25
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?
Upvotes: 0
Views: 103
Reputation: 79808
A few things going on here.
If your lecturer specified that student1
and student2
must be private
, then he intended them to fields of the class. That means you have to declare them outside of the method. Move these two declarations up, to before the line that says public static void main ...
.
Also, your class is called Student5th
, but you're using it within TestStudent
as if it were just Student
. The class name used in the variable declarations has to match the actual name of the class.
Where you wrote keyboard.readLine()
towards the end, you actually meant to write ccnsole.nextLine()
. You shouldn't be trying to read the same input stream with two different objects at the same time.
If you do change keyboard.readLine()
to console.nextLine()
, then you'll need an extra call to console.nextLine()
immediately before it, to consume the newline character at the end of the line that has the first student's third score.
Upvotes: 0
Reputation: 34146
You shouldn't specify an access level modifier (like private
or public
) inside a method:
Student student1; // delete 'private'
Student student2; // delete 'private'
Why? Because if you declare a variable inside a method, it should only be visible inside that specific method. It doesn't make sense to declare it as private
, public
or protected
.
You could take a look to this article about Information hiding.
Upvotes: 3