Question: 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
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:makemodelyearending 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 in the last lap, 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!
************************************************
package driver;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.*;
/**
*
* @author
*/
public class Driver {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException
{
Car car1 = new Car();
Scanner consoleScan = new Scanner(System.in);
System.out.println("Enter the file path with first Car details");
String fileName = consoleScan.nextLine();
File carInput = new File(fileName);
try {
Scanner carDetails = new Scanner(carInput);
car1.setMake(carDetails.next());
car1.setModel(carDetails.next());
car1.setYear(carDetails.nextInt());
car1.setSpeed(carDetails.nextInt());
System.out.println("Enter Second Car Details:");
System.out.println("Enter Car Make: ");
String make = consoleScan.next();
System.out.println("Enter Car Model ");
String model = consoleScan.next();
System.out.println("Enter Car year of Manufacturing: ");
int year = consoleScan.nextInt();
System.out.println("Enter initial speed of Car:");
int speed = consoleScan.nextInt();
Car car2 = new Car(year, model, make, speed);
System.out.println("!!!!!!Race is about to begin between the 2 cars!!!!!!!");
int car1MaxSpeed = 0;
int car2MaxSpeed = 0;
car1.setSpeed(0);
car2.setSpeed(0);
for (int i = 0; i < 5; i++)
{
car1.accelerate();
car2.accelerate();
car1.brake();
car2.brake();
System.out.println("==============Lap " + (i + 1) + "=======================");
System.out.println("Speed of First Car: " + car1.getSpeed());
System.out.println("Speed of Second Car: " + car2.getSpeed());
if (car1.getSpeed() > car1MaxSpeed)
{
car1MaxSpeed = car1.getSpeed();
}
if (car2.getSpeed() > car2MaxSpeed)
{
car2MaxSpeed = car2.getSpeed();
}
}
if (car1MaxSpeed > car2MaxSpeed)
{
System.out.println(" Fastest Car: First Car with speed: "+car1MaxSpeed);
} else
{
System.out.println(" Fastest Car: Second Car with speed: " + car2MaxSpeed);
}
if (car1.getSpeed() > car2.getSpeed())
{
System.out.println(" First Car won the Race: " + car1.toString());
} else
{
System.out.println(" Second Car won the Race: " + car2.toString());
}
carDetails.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
consoleScan.close();
}
}
package driver;
import java.util.Random;
/**
*
* @author
*/
public class Car
{
private int year;
private String model;
private String make;
private int speed;
public Car(int year, String model, String make, int speed)
{
this.year = year;
this.model = model;
this.make = make;
this.speed = speed;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public String getModel()
{
return model;
}
public void setModel(String model)
{
this.model = model;
}
public String getMake()
{
return make;
}
public void setMake(String make)
{
this.make = make;
}
public int getSpeed()
{
return speed;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
public void accelerate()
{
Random random = new Random();
int randomSpeed = 5 + random.nextInt(65);
speed += randomSpeed;
}
public void brake()
{
Random random = new Random();
int randomSpeed = 5 + random.nextInt(25);
if (speed > 25)
{
speed -= randomSpeed;
}
}
public String toString()
{
return " Make:" + make + " Model:" + model + " Year:" + year + " Ending speed after last race:" + speed;
}
}
need help completing this on netbeans and use IOException.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
