Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a class named Car that has the following fields (attributes): year (an int which holds the car's year) model (a String with holds the

Write a class named Car that has the following fields (attributes): year (an int which holds the car's year) model (a String with holds the car's model) make (a String which holds the make of the car) speed (an int which holds the car's initial speed) The Car class should have the following constructors and other methods: Constructor - Accepts the car's year, model, make, and speed as arguments. Default Constructor - Does not accept any input parameters, and uses data type defaults. Accessors (getters) for the object's attributes and Modifiers (setters). Methods: accelerate - each time it is called, it should add a random number between 5 and 70, to the speed field brake - each time it is called, it should subtract a random number between 5 and 30, to the speed field Create an empty data file called race.txt and add 1 record in it with all the data needed. Write a Driver class, the DrivingSimulation class, which does the following: Create a Car object #1 using the DEFAULT constructor. Then use the setters to initialize it's values, after reading a file that contains all the values: Prompt the user the name of the file that contains the data of the previous winning car. Read the file that contains the following data: make model year ending speed after last race Use the setters of Car1 object, to change the values from the default to the values read from the file. Remember to close the file! Prompt the user for the year, model, make, and initial speed of car #2. Create a Car object #2 using the non-default constructor. Display the information for each car. Note: Reset starting speed of each car to 0 before the race begins. Display an announcement that a race is about to begin between the 2 cars. Use your creativity regarding all messages displayed. Create a loop that will simulate racing around a track 5 times. Within the loop, do the following: Call the accelerate for each of the car objects, and after each call, use the accessor method to display the current speed of the car Call the brake for each of the car objects, and after each call, use the accessor method to display the current speed of the car Compare the speed for each car, and store the fastest speed for each car (hint: use 2 local variables in the driver to hold the fastest speed of each car.) At the end of the loop, display the fastest speed that each car reached. Compare the final speed of each car, and decide which car was the fastest (compare the final speed of car1 with the final speed of car2), and display all its data (hint: toString) Write over the file you read at the beginning of the program, with all the data about the winning car: make model year ending speed after last race Make sure to close the file!ppp

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions