1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Calling Methods Problem

  1. Sep 26, 2011 #1
    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 (Text):

    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 (Text):


    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 (Text):


    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());        
        }
    }
     
     
  2. jcsd
  3. Sep 26, 2011 #2

    Mark44

    Staff: Mentor

    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 (Text):
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java Calling Methods Problem
  1. Java object method (Replies: 23)

Loading...