FAQ: Object-Oriented Java - Using Methods: II


#1

This community-built FAQ covers the “Using Methods: II” exercise from the lesson “Object-Oriented Java”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Using Methods: II

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

Ok… I don’t understand how did the method “getAge” return the value 10?


#3

I’m going through this class myself, so I’ll give it my best shot.
I’m guessing you entered 10 on line 18:
18 Dog spike = new Dog(10);

On line 6 you defined the int age as having the same value as dogsAge. When you called the getAge method on the spike object, it returned age (which equals the value entered in your parameter dogsAge, 10).

Part of the confusion I think comes from the similar names.
Functionally, age means this dog is X years old (because it’s not defined yet)
and dogsAge for spike is 10 years old.
So for Spike, X=10, but for another dog, X=3

Hope that helps


#4

class Car {

int modelYear;

public Car(int year) {

    modelYear = year;

}

public void startEngine() {

    System.out.println("Vroom!");

}

public void drive(int distanceInMiles) {

    System.out.println("Your car drove " + distanceInMiles + " miles!");

}

public int numberOfTires() {

    return 4;

}

public static void main(String[] args){

    Car myFastCar = new Car(2007)
    myFastCar.startEngine();
    myFastCar.drive(1628);

    int tires = myFastCar.numberOfTires();
    System.out.println(tires);

}

}

=============================================
Okay I see what’s going on here but I am curious to why couldn’t we just call “numberOfTires” directly on “myFastCar” without setting that result to a variable then printing out the variable?
like so… myFastCar.numberOfTires().
Since the number of tires is static to 4, should that just return 4? Just seem a bit redundant to me-- it would be like setting skymiles = myFastCar.drive(1628) then printing out “skymiles”;

:confused: