14 Replies - 378 Views - Last Post: 26 September 2011 - 05:45 PM Rate Topic: -----

#1 murkyo0ocrimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-September 11

Calling Methods in Class Problem

Posted 25 September 2011 - 03:04 PM

I'm having trouble calling one of my methods from my Student class. Class Student is derived from a class called Person, if that matters. The error is "cannot find symbol - method getAverage(Person)".

Here's where I use the method:

System.out.println("Average: "+getAverage(p1));



The method defined in class Student:

public double getAverage(Person p)
{
    scoreAverage=(scoreOne+scoreTwo)/2;
    return scoreAverage;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Calling Methods in Class Problem

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Calling Methods in Class Problem

Posted 25 September 2011 - 03:10 PM

From what you've told us (not much), since getAverage() is a method in the Student class, you'd NORMALLY call the method as a method of an instance of Student. In other words, if student was an instance of Student as in:

Student student = new Student();


You'd then call the method getAverage() using:

double average = student.getAverage();


Does that help? If not, then please show more code.
Was This Post Helpful? 0
  • +
  • -

#3 murkyo0ocrimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-September 11

Re: Calling Methods in Class Problem

Posted 25 September 2011 - 04:32 PM

I've tried that but I still get the same error ("cannot find symbol - method getGrade()"). I've made a few changes since then as well, but nothing worked. Here are my programs in full:
public class Student extends Person
{
    private int scoreOne;
    private int scoreTwo;
    private double scoreAverage;
    private char scoreGrade;
    
    public Student()  //default constructor
    {
        super();
        scoreOne=0;
        scoreTwo=0;
        scoreAverage=0;
        scoreGrade='Z';
    }
    
    public Student(String theName, int theId, int scr1, int scr2)
    {
        super(theName, theId);
        scoreOne=scr1;
        scoreTwo=scr2;
    }
    
    public static Student storeRecords()
    {
            Scanner keyboard = new Scanner(System.in);
            
            System.out.println("Please enter the student's name: " );
            String a = keyboard.next();
            System.out.println("Please enter "+a+"'s identification number: ");
            int b = keyboard.nextInt();
            System.out.println("Please enter "+a+"'s first score: ");
            int c = keyboard.nextInt();
            System.out.println("Please enter "+a+"'s second score: ");
            int d = keyboard.nextInt();

            System.out.println("You entered: ");
            Student temp = new Student(a, b, c, d);
            System.out.println(temp);
            return(temp);
    }
    
    public void getAverage(int score1, int score2)
    {
        scoreAverage=(score1+score2)/2;
        System.out.println(scoreAverage);
    }
    
    public void getAverage(Person p)
    {
        scoreAverage=(scoreOne+scoreTwo)/2;
        System.out.println(scoreAverage);
    }
    
    public void getGrade(double scoreAve)
    {
        if(scoreAve>=90)
        {
            scoreGrade='A';
            System.out.println(scoreGrade);
        }
        else if(scoreAve>=80)
        {
            scoreGrade='B';
            System.out.println(scoreGrade);
        }
        else if(scoreAve>=70)
        {
            scoreGrade='C';
            System.out.println(scoreGrade);
        }
        else if(scoreAve>=60)
        {
            scoreGrade='D';
            System.out.println(scoreGrade);
        }
        else
        {
            scoreGrade='E';
            System.out.println(scoreGrade);
        }
    }
    
    public void calcGrade(Person p)
    {
        getGrade(scoreAverage);
    }
    
    public void setGrade(char altGrade)
    {scoreGrade=altGrade;}
    
    public void setAve(double altAverage)
    {scoreAverage=altAverage;}
    
    public String toString()
    {return(super.toString()+"\nScore One: "+scoreOne+"\nScore Two: "+scoreTwo);}
}



And:

import java.util.Scanner;
public class TestStudent
{
    public static void main(String[] args)
    {       
       Person p1 = new Student("Bob", 1234, 10, 8);
       System.out.println(p1);
       System.out.println();
       
       System.out.println("Average: "+p1.getAverage());
        
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Calling Methods in Class Problem

Posted 25 September 2011 - 04:40 PM

Why do you pass a Person as parameter to this method ?

public void calcGrade(Person p)

actually you do not even use Person p in the method body

And the method is void... so it will return nothing to print

This post has been edited by pbl: 25 September 2011 - 05:01 PM

Was This Post Helpful? 0
  • +
  • -

#5 murkyo0ocrimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-September 11

Re: Calling Methods in Class Problem

Posted 25 September 2011 - 04:59 PM

I didn't realize that, thanks. I deleted the method calcGrade and changed the parameters on getGrade as well as part of the body in getGrade to the following, but I'm still receiving the same error. Everything else is the same as in my previous post.

    public void getGrade()
    {
        if(scoreAverage>=90)
        {
            scoreGrade='A';
            System.out.println(scoreGrade);
        }
        else if(scoreAverage>=80)
        {
            scoreGrade='B';
            System.out.println(scoreGrade);
        }
        else if(scoreAverage>=70)
        {
            scoreGrade='C';
            System.out.println(scoreGrade);
        }
        else if(scoreAverage>=60)
        {
            scoreGrade='D';
            System.out.println(scoreGrade);
        }
        else
        {
            scoreGrade='E';
            System.out.println(scoreGrade);
        }
    }
    


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Calling Methods in Class Problem

Posted 25 September 2011 - 05:02 PM

getGrade() is void. It returns nothing to print
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Calling Methods in Class Problem

Posted 25 September 2011 - 05:05 PM

Now your error message makes sense. You no longer have a getGrade() method with the signature getGrade( double ), so the compiler is telling you that it can't find it for the call in calcGrade(). You need to edit calcGrade() to correspond to the other changes you've made.

Also, why do you call getGrade() in the method calcGrade()? What is that call supposed to do?

This post has been edited by GregBrannon: 25 September 2011 - 05:05 PM

Was This Post Helpful? 0
  • +
  • -

#8 murkyo0ocrimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-September 11

Re: Calling Methods in Class Problem

Posted 25 September 2011 - 05:21 PM

Good point, GregBrannon. I'm not sure what I was attempting with calcGrade anymore. I deleted the method calcGrade and changed getGrade to the following, but I'm still getting the error.

public char getGrade()
    {
        if(scoreAverage>=90)
        {
            scoreGrade='A';
            return scoreGrade;
        }
        else if(scoreAverage>=80)
        {
            scoreGrade='B';
            return scoreGrade;
        }
        else if(scoreAverage>=70)
        {
            scoreGrade='C';
            return scoreGrade;
        }
        else if(scoreAverage>=60)
        {
            scoreGrade='D';
            return scoreGrade;
        }
        else
        {
            scoreGrade='E';
            return scoreGrade;
        }
    }


Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Calling Methods in Class Problem

Posted 25 September 2011 - 05:27 PM

Please be more specific when you say you're "still getting the error." Show us your latest code and the error message exactly as it appears, cut and paste. There's no way we can help with the info provided in your latest post.
Was This Post Helpful? 0
  • +
  • -

#10 murkyo0ocrimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-September 11

Re: Calling Methods in Class Problem

Posted 25 September 2011 - 05:34 PM

The error is "cannot find symbol - method getGrade()".
Was This Post Helpful? 0
  • +
  • -

#11 murkyo0ocrimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-September 11

Re: Calling Methods in Class Problem

Posted 26 September 2011 - 11:22 AM

I'm trying to create a program that calls a class called Student in the main program and I'm getting a method call error that I can't figure out, specifically: cannot find symbol - method getGrade. getGrade is one of the methods in Student, which is shown below. (getGrade is at line 56)

import java.util.Scanner;
public class Student extends Person
{
    private int scoreOne;
    private int scoreTwo;
    private double scoreAverage;
    private char scoreGrade;
    
    public Student()  //default constructor
    {
        super();
        scoreOne=0;
        scoreTwo=0;
        scoreAverage=0;
        scoreGrade='Z';
    }
    
    public Student(String theName, int theId, int scr1, int scr2)
    {
        super(theName, theId);
        scoreOne=scr1;
        scoreTwo=scr2;
    }
    
    public static Student storeRecords()
    {
            Scanner keyboard = new Scanner(System.in);
            
            System.out.println("Please enter the student's name: " );
            String a = keyboard.next();
            System.out.println("Please enter "+a+"'s identification number: ");
            int b = keyboard.nextInt();
            System.out.println("Please enter "+a+"'s first score: ");
            int c = keyboard.nextInt();
            System.out.println("Please enter "+a+"'s second score: ");
            int d = keyboard.nextInt();

            System.out.println("You entered: ");
            Student temp = new Student(a, b, c, d);
            System.out.println(temp);
            return(temp);
    }
    
    public void getAverage(int score1, int score2)
    {
        scoreAverage=(score1+score2)/2;
        System.out.println(scoreAverage);
    }
    
    public void getAverage(Person p)
    {
        scoreAverage=(scoreOne+scoreTwo)/2;
        System.out.println(scoreAverage);
    }
    
    public char getGrade()
    {
        if(scoreAverage>=90)
        {
            scoreGrade='A';
            return scoreGrade;
        }
        else if(scoreAverage>=80)
        {
            scoreGrade='B';
            return scoreGrade;
        }
        else if(scoreAverage>=70)
        {
            scoreGrade='C';
            return scoreGrade;
        }
        else if(scoreAverage>=60)
        {
            scoreGrade='D';
            return scoreGrade;
        }
        else
        {
            scoreGrade='E';
            return scoreGrade;
        }
    }
    
    public void setGrade(char altGrade)
    {scoreGrade=altGrade;}
    
    public void setAve(double altAverage)
    {scoreAverage=altAverage;}
    
    public String toString()
    {return(super.toString()+"\nScore One: "+scoreOne+"\nScore Two: "+scoreTwo);}
    
}



And here's how I call getGrade in my main program:

Person p1 = new Student("Bob", 1234, 10, 8);
       System.out.println(p1);
       System.out.println();
       
       System.out.println(p1.getGrade());


Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9382
  • View blog
  • Posts: 35,228
  • Joined: 12-June 08

Re: Calling Methods in Class Problem

Posted 26 September 2011 - 11:26 AM

Please refrain from opening duplicate threads. Thank you.
Was This Post Helpful? 0
  • +
  • -

#13 murkyo0ocrimson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-September 11

Re: Calling Methods in Class Problem

Posted 26 September 2011 - 05:40 PM

X

This post has been edited by murkyo0ocrimson: 26 September 2011 - 05:50 PM

Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9382
  • View blog
  • Posts: 35,228
  • Joined: 12-June 08

Re: Calling Methods in Class Problem

Posted 26 September 2011 - 05:43 PM

Seriously, stop opening duplicate topics. You already have a thread for this problem. This is your warning.

(Merged threads)
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Calling Methods in Class Problem

Posted 26 September 2011 - 05:45 PM

Since the getGrade() method is defined in Student and your variable is of type Person, you cannot invoke getGrade() without casting the variable p1.

When you declare Person p1, all the compiler knows is that the Object in memory has to be a Person. So say you have a class Employee that also derives from Person. Person can now point to either a Person, Student or Employee. If Employee doesn't have a getGrade() method, then there would be a problem if you invoked p1.getGrade(). On top of that, Person does not have a getGrade() method. So you can only invoke methods on variables based on their data types.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1