Why Does My Java Program Say Cannot Find Method?

  • Comp Sci
  • Thread starter major_maths
  • Start date
  • Tags
    Java
In summary, the conversation is about creating a program that has a class named Student that is derived from the class Person. The main program, TestStudent, is used to test everything and there is an issue with calling methods correctly. The error "cannot find method" is encountered when trying to call a method in the main program. The conversation also includes discussions on the Person class and the Student class, as well as the TestStudent class. There is a question about why the Student constructor is being called to create a Person instance when the Person class does not know about the Student class.
  • #1
major_maths
30
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
  • #2
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.
 

Related to Why Does My Java Program Say Cannot Find Method?

1. What is a "Java calling methods problem"?

A "Java calling methods problem" refers to an issue that arises when a method in Java is not called or executed properly. This can result in errors or unexpected behavior in a program.

2. How can I troubleshoot a Java calling methods problem?

To troubleshoot a Java calling methods problem, you can start by checking the syntax of the method call to ensure it matches the method definition. You can also use a debugger to step through the code and see where the method is not being called correctly. Additionally, reviewing the logic of the method and any parameters being passed can help identify the issue.

3. What are some common causes of Java calling methods problems?

There are several potential causes of Java calling methods problems. These can include typos or syntax errors in the method call, incorrect or missing parameters, using the wrong method name, or calling a method from the wrong class.

4. Can a Java calling methods problem be caused by an issue in the method itself?

Yes, a Java calling methods problem can also be caused by an issue in the method itself. This can include incorrect logic, missing or incorrect return statements, or not handling exceptions properly.

5. How can I avoid Java calling methods problems in my code?

To avoid Java calling methods problems, it is important to carefully review and test your code before running it. This includes checking the syntax of method calls, properly defining and using parameters, and thoroughly testing the logic of the method. Using a debugger or other troubleshooting tools can also help catch any potential issues before they become larger problems.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Back
Top