Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with this codding assignment. I\'m using java netbeans. Please leave

ID: 3855218 • Letter: P

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 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();

Explanation / Answer

Here is the complete code for the question. When ever the car's speed becomes -ve after brake, its being set to 0. (this happens when the brake value is more than current speed. Example car speed is 12 and brake is 25, then speed becomes -ve. In such case , speed is set to 0, since -ve speed doesnot make sense).

Please post a comment in case of any issues and rate the answer if it helped. Thank you.

Car.java

package drivingsimulation;

import java.util.Random;

/**

*

* @author morty

*/

public class Car

{

private String model, make;

private int year, speed;

  

//default constructor

public Car()

{

      

}

// parameterized constructor

  

public Car(String amodel, String amake, int ayear, int aspeed)

{

   model = amodel;

   make = amake;

   year = ayear;

   speed = aspeed;

  

}

  

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 accelerate()

{

Random rand1 = new Random();

int myRand1 = rand1.nextInt(70 - 5 + 1) + 5;

speed += myRand1;

//method: acceleration

}

public void brake()

{

Random rand2 = new Random();

int myRand2 = rand2.nextInt(50 - 5 + 1) + 5;

speed -= myRand2;

if(speed < 0) //if speed goes -ve

   speed = 0;

}

  

public String toString()

{

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

}

// end of Car

}

DrivingSimulation.java

package drivingsimulation;

import java.util.Scanner;

import java.io.*;

/**

*

* @author morty

*/

public class DrivingSimulation {

  

  

/**

   * @param args the command line arguments

   */

public static void main(String[] args)

{

   String model, make;

   int year, speed;

   String filename;

   Scanner keybd = new Scanner(System.in);

   System.out.println("Enter filename containing data of previous winning car: ");

   filename = keybd.nextLine().trim();

   Car car1 = new Car(); //use default constructor

   //load car1 details from input file

   try {

          Scanner inFile = new Scanner(new File(filename));

          car1.setMake(inFile.nextLine().trim());

          car1.setModel(inFile.nextLine().trim());

          car1.setYear(inFile.nextInt());

          car1.setSpeed(inFile.nextInt());

          inFile.close();

          System.out.println("Loaded car1 from file: " + car1);

       } catch (FileNotFoundException e) {

           System.out.println(e.getMessage());

          

       }

   //prompt user for car2 details

   System.out.print(" Enter details for car2 : ");

   System.out.print(" Make: ");

   make = keybd.nextLine().trim();

   System.out.print("Model: ");

   model = keybd.nextLine().trim();

   System.out.print("Year: ");

   year = keybd.nextInt();

   System.out.print("Speed: ");

   speed = keybd.nextInt();

   //use parameterised constructor

   Car car2 = new Car(model, make, year, speed);

   System.out.println(" The 2 cars racing are ...");

   System.out.println(car1);

   System.out.println(car2);

   System.out.println(" The races is about to begin.... ");

   //set speed to zero for both the cars

   car1.setSpeed(0);

   car2.setSpeed(0);

   int maxCar1Speed = 0, maxCar2Speed = 0;

   //loop for 5 tracks

   for(int lap = 1; lap <= 5; lap++)

   {

       car1.accelerate();

       car2.accelerate();

     

       //store the fasted speed for each car

       if(car1.getSpeed() > maxCar1Speed)

           maxCar1Speed = car1.getSpeed();

       if(car2.getSpeed() > maxCar2Speed)

           maxCar2Speed = car2.getSpeed();

       System.out.println("Speeds: car1 = " + car1.getSpeed() + " car2 = " +car2.getSpeed());

       car1.brake();

       car2.brake();

       System.out.println("Speeds: car1 = " + car1.getSpeed() + " car2 = " +car2.getSpeed());

   }

   Car winner;

   //display the car with fastest speed

   if(maxCar1Speed > maxCar2Speed)

   {

       System.out.println("Car1 had the fastest speed of " + maxCar1Speed +" " + car1);

       winner = car1;

   }

   else

   {

       System.out.println("Car2 had the fastest speed of " + maxCar2Speed +" " + car2);

       winner = car2;

   }

   //store data of winning car back into same file

   try {

       PrintWriter outfile = new PrintWriter(new File(filename));

       outfile.write(winner.getMake() + " ");

       outfile.write(winner.getModel() + " ");

       outfile.write(winner.getYear() + " ");

       outfile.write(winner.getSpeed() +" ");

       outfile.close();

       } catch (FileNotFoundException e) {

           System.out.println(e.getMessage());

       }

}

}

input file : car.txt

Ford

Ford Taurus

2016

100

output

Enter filename containing data of previous winning car:

car.txt

Loaded car1 from file: Make: Toyota Model: Toyota Etios Year: 2016 Speed: 27

Enter details for car2 :

Make: Ford

Model: Ford Taurus

Year: 2016

Speed: 35

The 2 cars racing are ...

Make: Toyota Model: Toyota Etios Year: 2016 Speed: 27

Make: Ford Model: Ford Taurus Year: 2016 Speed: 35

The races is about to begin....

Speeds: car1 = 12 car2 = 13

Speeds: car1 = 0 car2 = 0

Speeds: car1 = 23 car2 = 64

Speeds: car1 = 0 car2 = 24

Speeds: car1 = 34 car2 = 57

Speeds: car1 = 25 car2 = 47

Speeds: car1 = 46 car2 = 112

Speeds: car1 = 24 car2 = 87

Speeds: car1 = 69 car2 = 118

Speeds: car1 = 21 car2 = 100

Car2 had the fastest speed of 118

Make: Ford Model: Ford Taurus Year: 2016 Speed: 100