Question: Please help with this codding assignment. I'm using java netbeans. Please leave comments explaining what you did, thank you. Write a class named Car that
Please help with this codding assignment. I'm using java netbeans. Please leave comments explaining what you did, thank you.
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:
make
model
year
ending speed of 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.
Display an announcement that a race is about to begin between the 2 cars. Use your creativity regarding all messages displayed.
Note: Reset all cars' starting speed to 0 before the race begins.
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 variables to hold the fastest speed of each car.)
At the end of the loop, display the fastest speed that each car reached.
Decide which car achieved the fastest speed, 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 of last race
Make sure to close the file!
This is what I have so far
DOMAIN
package drivingsimulation;
import java.util.Random;
/** * * @author morty */ public class Car { private String model, make; private int year, speed; //constructor Car(String amodel, String amake, int ayear, int aspeed) { model = amodel; make = amake; year = ayear; speed = aspeed; //default constructor; } public String getModel() { return model; }
public void setModel(String amodel) { model = amodel; }
public String getMake() { return make; }
public void setMake(String amake) { make = amake; }
public int getYear() { return year; }
public void setYear(int ayear) { year = ayear; }
public int getSpeed() { return speed; }
public void setSpeed(int aspeed) { speed = aspeed; } public void accelarete() { Random rand1 = new Random(); int myRand1 = rand1.nextInt(66) + 5; speed += myRand1; //method: acceleration } public void brake() { Random rand2 = new Random(); int myRand2 = rand2.nextInt(66) + 5; speed -= myRand2; } // end of Car }
DRIVER
package drivingsimulation; import java.util.Scanner; import java.util.Random; import java.text.DecimalFormat; import java.io.IOException; import java.io.*;
/** * * @author morty */ public class DrivingSimulation {
/** * @param args the command line arguments */ public static void main(String[] args) throws IOException { String model, make; int year, speed; int carSpeed1, carSpeed2; //strore car speed File aFile = new File("output.txt"); Scanner inFile = new Scanner(aFile); DrivingSimulation car1 = new DrivingSimulation();
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
