Question: Create a new program named Car.java. The goal is to implement a Car class so that it conforms to the UML below: Create three instance

Create a new program named Car.java.

The goal is to implement a Car class so that it conforms to the UML below:

Create a new program named Car.java. The goal is to implement a

Create three instance variables within the class: an int that holds the car's model year, a String that holds the car's make, an int that holds the car's speed. These should be listed as private.

Create a constructor that accepts the model year and make and sets the speed to 0. Note that either you should make the constructor's parameters a different name than the instance variables or use the "this" qualifier when placing the parameter's value into the instance variable.

Code the toString method. This method should take no parameters and should return a string generated using the String.format method: String.format("A %d %s that is going %.1f mph", year, make, speed)

Compile your Car class and correct any errors.

Part 3 Test your Car.

Create a CarTester class (CarTester.java) which contains a main method.

Create two Car objects of your choice. (Hint: you need to call the constructor with the values you want for your "dream cars").

Display the Car objects so that you know that your constructor is working correctly by calling the toString method and printing that result. (Hint, you will need to call the method with the Car objects that you created)

Here is a sample of how to create/instantiate car objects and test them. The contents of main should look something like the following.

Car car1; Car car2; car1 = new Car("Ford", 1997); car2 = new Car("Toyota", 2014); System.out.println(car1.toString()); System.out.println(car2.toString());

Part 4 Refinement:

Edit Car.java.

Create accessor methods that let you access each individual element of the Car. They should be named with "get" followed by the name of the attribute. So one of your accessor methods would be getSpeed(). They should take no parameters and should return the value of that attribute.

Create two mutator methods, accelerate() and brake(). These methods are void methods and take no parameters. When called, the accelerate() method should increase the speed variable by 5 miles per hour. The brake() method should decrease the speed variable by 5 miles per hour.

Part 5 Testing the refinement:

Edit CarTester.java.

For the first car, call the accelerate() method 5 times in a row and call the getSpeed() method in a print statement (with an appropriate label) for each increase.

Do the same for the second car but accelerate only 3 times in a row also calling getSpeed() in a print statement for each acceleration.

For the first car, call brake 5 times in a row calling getSpeed() with each pass.

Print the result of a call to the toString() method for each of the cars. The first car should be at zero and the second at 15 mph.

Part 6 - Just for fun:

It is possible to pass one object into the method for another object. For example, the String method equals is called by a String object and is passed another String object: answer.equals("yes") for example.

Add a mutator method to your Car class called crash that takes a Car object as a parameter and returns nothing. It should display the message "CRASH" and then set the speed of both the calling object and the called object to 0. (See Chapter 9.2 for examples of passing an object into a method and 9.8 for examples of referencing two objects' fields (instance variables)).

In your tester, call accelerate for the first car until it is the speed of the second. Call the first car's crash() method passing in the second car.

Display both of the cars after the crash() using the toString() method.

Car -make: String -year: int -speed: double +Car (n: String, y:int) +toString): String +getMake ) String +getSpeed ): double +getYear): int taccelerate ): void +brake (): void

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!