Share
Explore BrainMass

Java Class Car and CarTest

Design a Java class named Car with the following fields:
- yearModel,
- make: references a string,
- speed: holds the car's current speed.

In addition, it should also have the following constructor and other methods:
- Constructor: should assign 0 to the speed field, accept the year model and make as arguments which are assigned to the object's yearModel and make fields respectively.
- Accessors: methods to get the values stored in yearModel, make and speed fields.
- accelerate: method should add 5 to the speed field each time it is called.
- brake: should subtract 5 from the speed field when called.

Also design a test program that creates a Car object, then calls the accelerate method five times, and after each call gets the current speed of the car and displays it. Then it calls the brake method five times, and after each call gets the current speed and displays it.

Solution Preview

Attached 585315.zip contains two files -

Car.java : implements class Car, and
CarTest.java : implements test program to test class Car.

Java and Java compiler (javac) version 1.7.0_45 were used during development and testing of this solution.

Here is how you can compile and execute the CarTest ...

Solution Summary

Solution consists of two files: Car.java that implements class Car, and CarTest.java that implements a test program to test this Car class.

Java and Java compiler (javac) version 1.7.0_45 were used during development and testing of this solution.

$2.19