Comp Sci Why Does My Java Program Say Cannot Find Method?

  • Thread starter Thread starter major_maths
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The error "cannot find method" arises because the code attempts to call methods on a Person reference that are not defined in the Person class. The TestStudent class should instantiate a Student object directly instead of a Person object to access Student-specific methods. Additionally, passing a Person type parameter to the Student's getAverage method is inappropriate since it expects scores from the Student class. Correcting these issues will resolve the method call errors. Properly using class instances is crucial for method accessibility in Java.
major_maths
Messages
30
Reaction score
0
I'm trying to create a program that derives a class named Student from the class Person, and then I'll test everything in the main program in TestStudent. What I'm having trouble with at the moment, is calling the methods correctly. Whenever I try to call a method in the main program, I get this error: "cannot find method - method [name of method]".

Class Person:
Code:
public class Person
{
    private String name;
    private int id;
    
    public Person()
    {
        name="AAAA";
        id=0000;
    }
    
    public Person(String newName, int newId)
    {
        name=newName;
        id=newId;
    }
    
    public String getName()
    {return name;}
    
    public int getId()
    {return id;}
    
   public Person(Person otherPerson)
    {
        name=otherPerson.name;
        id=otherPerson.id;
    }
    
    public void setName(String name)
    {this.name=name;}
    
    public static void setName(Person p, String newName)
    {p.name=newName;}
    
    public void setId(int id)
    {this.id=id;}
    
    public static void setId(Person p, int newId)
    {p.id=newId;}

    public boolean equals(Object otherObject)
    {
        Person p = (Person)otherObject;
        return (name.equals(p.name)&&id==p.id);
    }
    
    public boolean equals(Person p)
    {return (name.equals(p.name)&&id==p.id);}
    
    public String toString()
    {return ("Name: "+name+"\nId: "+id);}
}

The Student class:
Code:
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 finally, the TestStudent class:
Code:
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(p1.getGrade());        
    }
}
 
Physics news on Phys.org
Why are you calling the Student constructor to create a Person instance? The Person class doesn't know anything about the Student class.

Apparently you want to create a Student object, so you should use a Student instance to store it in.

Code:
Student s1 = new Student("Bob", 1234, 10, 8);

There are probably other things wrong with your code, such as where you pass a parameter of type Person to the getAverage method on Student.
 

Similar threads

Replies
1
Views
2K
Replies
12
Views
2K
Replies
2
Views
1K
Replies
7
Views
3K
Replies
5
Views
3K
Replies
1
Views
2K
Replies
2
Views
4K
Back
Top