1

Does anyone know why I'm not getting the actual integers for out put... I get WorkOut@1261281792. I know it is something small but can't figure it out. here are my 3 classes:

public class WorkOut1 {

    public static void main(String [] args) {      

        Exercise tuesWorkout = new Exercise (10, 50);

        PullUps tuesPullUps = new PullUps (10, 50);

        System.out.println("This Tuesday our workout will consist of this many minutes and reps:");
        System.out.println(tuesWorkout);
    }
} 

PullUps Class

public class PullUps extends Exercise {

    private static int barHeight;

    public PullUps (int min, int reps) {
        super(min, reps);
        barHeight = 6;
    }
}

Exercise class

public class Exercise {

    private static int min;
    private static int reps;

    //Constructor
    public Exercise(int min,int reps) {
        this.min = min;
        this.reps = reps;
    }// end constructor

    public static int howManyMin() {
        return min;
    }//end method howmanyMin()

     public static int howManyReps() {
        return reps;
    }//end method howmanyReps()

}//end class
2
  • That is what you are supposed to get. You are asking to "print" out the actual instance of the Workout1 class. Which is what you get. Commented Aug 24, 2015 at 20:52
  • If you want to print out, for example, reps, you want to do something along the lines of System.out.println(tuesWorkout.howManyReps()). Commented Aug 24, 2015 at 20:53

2 Answers 2

4

When you call an object to be printed, the object's toString() method is called, which in this case it will inherit from the Object class (this is a Java class from which all classes inherit from) since you haven't defined a toString() method in the Exercise class. So you are getting the Object name + a hash representation of the object. One fix will be to create a toString method in the object to be printed (in this case Exercise) that returns a string of what you want printed.

public String toString() {
    return(min + ", " + reps);
}

The other option is to print the result of the accessor function for the property you want printed in your main class.

System.out.println(tuesWorkout.howManyMin() + ", " + tuesWorkout.howManyReps());
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Adrian... this is a great help.
2

You do not want to print the actual tuesWorkout object itself.

You want to to do:

System.out.println(tuesWorkout.howManyReps());

Which gives you the number of reps.

Then you have to call your getter for min to find out how many min the workout is.

By just calling System.out.println(tuesWorkout) it is like printing the actual Object itself, which has no real meaning to us.

2 Comments

Additionally, if the goal is to be able to print a custom format for your classes you may want to look into overriding their public String toString() method.
Ahh yes. An even better idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.