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.

Define a "fastestSpeedCar1" and a "fastestSpeedCar2" and initialize them to 0.

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 accessormethod 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: compare the current speed of car1 against fastestSpeedCar1, and if it is faster, move the current speed of car1 to fastestSpeedCar1. Similarly, compare the current speed of car2 against fastestSpeedCar2, and if it is faster, move the current speed of car2 to fastestSpeedCar2. )

At the end of the loop, display the fastest speed that each car reached, like each car's "personal best." Display fastestSpeedCar1 and fastestSpeedCar2.

For the last lap, 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!

Could you please fix the error and complete the rest of it basically. Thank you.

Car Class:

public class Car {

/** * @param args the command line arguments */ // TODO code application logic here private int year; private String model; private String make; private int speed;

Car(int year,String model,String make,int speed){ this.year=year; this.model=model; this.make=make; this.speed=speed; } public Car(){ this.year=0; this.model=" "; this.make=" "; this.speed=0; } public int getYear(){ return year; } public String getModel(){ return model; } public String getMake(){ return make; } public int getSpeed(){ return speed; } public void setYear(int year){ this.year=year; } public void setModel(String model){ this.model=model; } public void setMake(String make){ this.make=make; } public void setSpeed(int speed){ this.speed=speed; } public void accelerate(){ Random rand = new Random(); int acceleration= rand.nextInt((70 - 5) + 1) + 5; speed=speed+acceleration; } public void brake(){ Random rand = new Random(); int subtraction= rand.nextInt((30 - 5) + 1) + 5; speed=speed-subtraction; }

public String toString()

{

return "Year:"+ year +" Model:"+model+" Make:"+make+" Speed:"+speed; } }

Driver Class:

public class DrivingSimulation { public static void main(String args[]){ int year; String model; String make; int speed; Scanner keyboard=new Scanner(System.in); System.out.println("Enter file name:");

String fileName = keyboard.nextLine(); try { Car c1 = new Car(); File myFile = new File(fileName); Scanner scan = new Scanner(myFile); make = keyboard.nextLine(); model = keyboard.nextLine(); year = Integer.parseInt(keyboard.nextLine()); speed = Integer.parseInt(keyboard.nextLine()); c1.setMake(make); c1.setModel(model); c1.setSpeed(speed); c1.setYear(year);

keyboard.close(); } catch(Exception e) { e.printStackTrace(); } System.out.println("Enter the year of the second Car: "); year= keyboard.nextInt(); System.out.println("Enter the model of the second Car: "); model= keyboard.next(); System.out.println("Enter the make of the second Car: "); make= keyboard.next(); System.out.println("Enter the speed of the second Car: "); speed= keyboard.nextInt(); Car c1 = new Car(); Car c2 = new Car(year, model, make, speed); System.out.println("Car 1 details:"); System.out.println(c1.toString()); System.out.println("Car 2 details:"); System.out.println(c2.toString()); int fastestSpeedCar1=0; int fastestSpeedCar2=0; for(int i =0;i<5;i++){ System.out.println("Lap "+(i++)); c1.accelerate(); c2.accelerate(); System.out.println("car 1 speed after acceleration:" + c1.getSpeed()); System.out.println("Car 2 speed after acceleration:" + c2.getSpeed()); c1.brake(); c2.brake(); System.out.println("car 1 speed after brake:"+c1.getSpeed()); System.out.println("Car 2 speed after brake:"+c2.getSpeed()); System.out.println("========================================="); if(fastestSpeedCar1 < c1.getSpeed()) fastestSpeedCar1 = c1.getSpeed(); if(fastestSpeedCar2 < c2.getSpeed()) fastestSpeedCar2 = c2.getSpeed();

} System.out.println("Car1's personal best:"+fastestSpeedCar1); System.out.println("Car2's personal best:"+fastestSpeedCar2); String wonRace = ""; if(c1.getSpeed()>c2.getSpeed()){ System.out.println("Car 1 won the race"); System.out.println(c1.toString()); wonRace= c1.getMake()+" "+c1.getModel()+" "+c1.getYear()+" "+c1.getSpeed(); } else if(c2.getSpeed()>c1.getSpeed()){ System.out.println("Car 2 won the race"); System.out.println(c2.toString()); wonRace= c2.getMake()+" "+c2.getModel()+" "+c2.getYear()+" "+c2.getSpeed(); } } }

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_2

Step: 3

blur-text-image_3

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

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions

Question

What command attaches a shape to a window?

Answered: 1 week ago