Learn

Fantastic! Similar to constructors, you can customize methods to accept parameters.

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("Miles driven: " + distanceInMiles); } public static void main(String[] args){ Car myFastCar = new Car(2007); myFastCar.startEngine(); myFastCar.drive(1628); }

In the example above, we create a drive method that accepts an int parameter called distanceInMiles. In the main method, we call the drive method on the myFastCar object and provide an int parameter of 1628.

Calling the drive method on myFastCar will result in printing Miles driven: 1628 to the console.

Instructions

1.

In between the bark and main methods, add a method called run to the Dog class by typing:

public void run() { }
2.

Modify the run method so that it accepts an int parameter called feet.

3.

Inside of the run method, type:

System.out.println("Your dog ran " + feet + " feet!");
4.

Inside of the main method, call the run method on the spike object. You can pass in an int parameter of your choice.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?