0

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?

4
  • 1
    Can't see the bolded parts. Can you share the compilation error that you are getting? Commented Apr 8, 2014 at 4:46
  • what error you are getting can u paste it here? Commented Apr 8, 2014 at 4:56
  • On top of Christian's answer, change your student2 line to this: student2.setName(console.nextLine()); Commented Apr 8, 2014 at 4:56
  • and what is mean of using Modifier for the same method?? just Remove Private Modifier form the main method i mean that object that you are using Student object. Commented Apr 8, 2014 at 4:57

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

13 Comments

Yes! Also, I needed to change keyboard to console. I can't believe my "Lecturer" said I had to make those private. Unbelievable. Thank you.
Maybe he referred to make them private somewhere else.
He could have meant outside of main, but still inside your TestStudent class.
@LisaSmith well my "Lecturer" use to teach me Hello World only all time. hehehe. :P
Basically, you're saying that if I make private variables in my class with the main method, I can't call methods on them from the other class? Correct?
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.