Class Challenge: Car

  1. In Eclipse, create a new package in Java4Kids which is com.java4kids.car and a new Java class Car with the following methods:

public void start()

public void stop()

public int drive(int howlong)

The method drive has one argument – the driving time. The method has to return the total distance driven by the car for the specified time. Use the following formula to calculate the distance: distance = howlong*60;. Use the statement return distance as the last line in the method drive.

  1. Run this program in Eclipse.
  2. Write another class CarOwner with the method main that creates an instance of the object Car and call its methods. The result of each method call has to be printed using System.out.println().
  3. Create a subclass of Car named SuperCar and override the method drive Now use the following formula to calculate the distance: distance = howlong*180;
  1. Create an instance of the class SuperCar in the main method of CarOwner. Re-run the program CarOwner to see that it calls the overridden method drive.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s