Fryon Alexandra Fryon Alexandra - 4 months ago 22
Java Question

This java tutorial is WRONG?

I am trying to learn objects and classes in java and an online tutorial is giving me code which I cant compile/run on eclipse.

When I compile the program after saving the class it gives me the error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

The method setAge(int) is undefined for the type Puppy

The method getAge() is undefined for the type Puppy
at Puppy.main(Puppy.java:23)


public class Puppy{

int puppyAge;

public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
public setAge( int age ){
puppyAge = age;
}

public getAge( ){
System.out.println("Puppy's age is :" + puppyAge );
return puppyAge;
}
public static void main(String []args){
/* Object creation */
Puppy myPuppy = new Puppy( "tommy" );

/* Call class method to set puppy's age */
myPuppy.setAge( 2 );

/* Call another class method to get puppy's age */
myPuppy.getAge( );

/* You can access instance variable as follows as well */
System.out.println("Variable Value :" + myPuppy.puppyAge );
}
}


http://www.tutorialspoint.com/java/java_object_classes.htm

(the code is in the middle of the page)

It is a constructor class program. There should be no return type.

Answer

Why it does not work?

You have to add return types to getAge() and setAge().

Java Language Specification notes that method declaration should follow this format:

MethodModifiers(optional) TypeParameters(optional) Result MethodDeclarator Throws(optional)

Relax, lets look at this slowly:

  • MethodModifiers(optional) - You can mark each method as private/public/protected. If you omit this parameter method is package. There are few more modifiers, but these are most interesting for You now
  • TypeParameters(optional) - It is about generic methods. You can safely skip this one now
  • Result - This one is important. Every method must explicitly say what it returns. This was missing in Your example.
  • MethodDeclarator - name of Your method.
  • Throws(optional) - if Your method will throw exceptions You have to write it here. For now You can safely skip it.

enter image description here

Just show me the working code!

public class Puppy{

    int puppyAge;

    public Puppy(String name){
        // This constructor has one parameter, name.
        System.out.println("Passed Name is :" + name );
    }
    public void setAge( int age ){
        puppyAge = age;
    }

    public int getAge( ){
        System.out.println("Puppy's age is :" + puppyAge );
        return puppyAge;
    }
    public static void main(String []args){
      /* Object creation */
        Puppy myPuppy = new Puppy( "tommy" );

      /* Call class method to set puppy's age */
        myPuppy.setAge( 2 );

      /* Call another class method to get puppy's age */
        myPuppy.getAge( );

      /* You can access instance variable as follows as well */
        System.out.println("Variable Value :" + myPuppy.puppyAge );
    }
}
Comments