tags:

views:

93

answers:

6

Correct the following erroneous definition of method getAge()

public void getAge() 
{
   return age;
}

Paste your answer in here:

public int getAge(int age) 
{ 
return age; 
}

Mark: 0 out of 1

Comments:

* Test 1 (0.0 out of 1)

      Person.java:15: getAge(int) in Person cannot be applied to ()
              String s = (p.getAge() == 16 ? "getAge() Correct" : "getAge() not Correct");
                           ^
      1 error
      The output should have been:
          getAge() Correct

      This is what was actually produced:
          Exception in thread "main" java.lang.NoClassDefFoundError: Person

instead of (int age) does it want me to replace it with a String?

+2  A: 

No it wants you to give it the correct return type!

public int getAge() 
{
   return age;
}
ennuikiller
He had the return type right, just the wrong arguments.
Tim
what arguments? There's no arguments in this accessor! It's the wrong return type (void) that was specified!!
ennuikiller
He got the answer half right. He corrected the return type from void to int, but neglected to remove the "int age" argument to the accessor method.
Jim Ferrans
A: 

No, it doesnt want the parameter. You return a value of a property of the object (p in this case).

Trimack
+4  A: 

I believe the correct code you're looking for is:

public int getAge() 
{ 
  return age; 
}

basically saying that getAge() requires no parameters (where your mistake was, you had one integer parameter being passed in) and it will return a value of type int.

tschaible
A: 

I think the problem is that it's trying to apply your getAge(int) function to a call to getAge(). Go back and think again about what getAge is trying to do, and what information (arguments) it should need to do it. Then rewrite your function definition.

Tim
A: 

Homework? Tsk...tsk...tsk.

inked
+1  A: 

Just use the java compiler in command line (or a decent IDE) to understand what is going on. (You seem to be just pasting code in your school's homework test harness and getting cryptic errors.)

What does this mean?

  This is what was actually produced:
      Exception in thread "main" java.lang.NoClassDefFoundError: Person

It means that the homework test harness tried compiling your Person class and it couldn't (see below) and then the test harness ran and couldn't find a Person class (because it didn't compile).

Here is the problem:

public void getAge() 
{
   return age;
}

A method returning void can't return a value. (If you had used javac, the compiler would have told you that.)

So age is an int and you need to return it:

public int getAge() 
{
   return age;
}