Why Does My Java Program Say Cannot Find Method?

  • Context: Comp Sci 
  • Thread starter Thread starter major_maths
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

The forum discussion addresses a common error encountered in Java programming: "cannot find method." The issue arises when attempting to call methods on a superclass instance instead of a subclass instance. Specifically, the user incorrectly creates a Person object using the Student constructor, leading to method invocation failures. The solution is to instantiate a Student object directly, ensuring that subclass-specific methods are accessible.

PREREQUISITES
  • Understanding of Java inheritance and polymorphism
  • Familiarity with Java class constructors and method overloading
  • Knowledge of Java's access modifiers (private, public)
  • Basic proficiency in using Java's Scanner class for input
NEXT STEPS
  • Review Java inheritance principles and best practices
  • Learn about method overriding and overloading in Java
  • Explore Java's access control mechanisms and their implications
  • Investigate debugging techniques for common Java runtime errors
USEFUL FOR

Java developers, computer science students, and anyone troubleshooting object-oriented programming issues 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 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K