Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with this codding assignment. I'm using java netbeans. Please leave comments explaining what you did, thank you, and on how to do input.

Please help with this codding assignment. I'm using java netbeans. Please leave comments explaining what you did, thank you, and on how to do input.

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; //default constructor public Car() { } 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(66) + 5; speed += myRand1; //method: acceleration } public void brake() { Random rand2 = new Random(); int myRand2 = rand2.nextInt(66) + 5; speed -= myRand2; if (speed < 0) { speed = 0; } } public String toString() { return "Make: " + make + "Model: " + model + "Year: " + year + "Speed: " + speed; } // 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, fileName; int year, speed; Scanner key = new Scanner(System.in); System.out.println("Enter file containing data of previous winning car: "); fileName = key.nextLine().trim(); Car car1 = new Car(); 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 ex) { System.out.println(ex.getMessage()); } System.out.println("Enter model of Car2: "); model = key.nextLine().trim(); System.out.println("Enter make of Car2: "); make = key.nextLine().trim(); System.out.println("Enter year of Car2: "); year = key.nextInt(); System.out.println("Enter speed of Car2: "); speed = key.nextInt(); Car car2 = new Car(model, make, year, speed); System.out.println("The two cars are racing "); System.out.println(car1); System.out.println(car2); System.out.println("Ladies and gentlemen, the race is about to begin "); car1.setSpeed(0); car2.setSpeed(0); int maxCar1Speed = 0, maxCar2Speed = 0; for (int lap = 1; lap <= 5; lap++) //loop 5 time for 5 laps { car1.accelerate(); car2.accelerate(); 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; if (car1.getSpeed() > maxCar1Speed) { System.out.println("Car1 was faster"); winner = car1; } else { System.out.println("Car2 was faster"); winner = car2; } 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 ex) { System.out.println(ex.getMessage()); } } }

This is the output, please explain why it's saying 'null' and how I can fix it, thank you.

run: Enter file containing data of previous winning car: car.txt car.txt (The system cannot find the file specified) Enter model of Car2: Toyota Enter make of Car2: Camry Enter year of Car2: 2016 Enter speed of Car2: 10 The two cars are racing Make: nullModel: nullYear: 0Speed: 0 Make: CamryModel: ToyotaYear: 2016Speed: 10 Ladies and gentlemen, the race is about to begin

Speeds: car1 = 22 car2 = 28 Speeds: car1 = 0 car2 = 0 Speeds: car1 = 68 car2 = 25 Speeds: car1 = 10 car2 = 0 Speeds: car1 = 65 car2 = 66 Speeds: car1 = 58 car2 = 2 Speeds: car1 = 119 car2 = 53 Speeds: car1 = 103 car2 = 48 Speeds: car1 = 155 car2 = 66 Speeds: car1 = 124 car2 = 17 Car2 was faster BUILD SUCCESSFUL (total time: 35 seconds)

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

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago