Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java program: I only need help in part two. This should be the output. Part1 You will create two classes Car and RaceLane.

Write a Java program: I only need help in part two. This should be the output.image text in transcribedimage text in transcribed

Part1

You will create two classes Car and RaceLane. Your main method will go inRaceLane1.

1.Define a classCar. All of the below properties/methods should be non static.

a.A Car should have five private properties: a String for the model of the car (e.g.Toyota Camry), an int location, an int currentSpeed, a boolean movingForward, and an int maxSpeed. The idea is that the location data member stores where on a number line the Car is.Make sure the number entered is 0.

b.A default constructor.

c.A constructor that takes as input three things: A String for the model, an int maxSpeed, and an int for an initial location. It should set the model of the Car, the max speed, and initial location appropriately based on the input. The constructor should make it so that the movingForward property should always be initially set to be true. The currentSpeed should always be initially set to be 0.

d.Write three methods called getModel(), getDirection(), and get Location() that let you retrieve the model, movingForward, and location properties respectively.

e. Write a method go() which sets the currentSpeed of the Car object to be the objects maxSpeed

f. Write a method stop() which sets the currentSpeed of the Car object to be 0

g.Write a method turnAround() which changes the boolean variable movingForward to be the opposite of what it was before. That is, if it was true before, it should be false now. If it was false before, it should now be true.

h.Write a method move() which does the following:

i. If movingForward is true, then it adds the currentSpeed to the location property.

ii.If movingForward is false, it subtracts the currentSpeed from the location property.

i. A method toString() which return a string representation of a Car object in the form of the model of the Car, its current speed, its direction and its location.

For example, it might return:

Toyota Camry: Located at 10, facing forward and moving at 5 speed.

or

Toyota Camry: Located at 3, facing backwards, not moving

2.Once you have defined the class, create a new class

RaceLane1 and add a main method in which you do the following:

a. First create a Scanner object and use it to read (one at a time) 3 values from the user: A String, an initial location, and a maxSpeed. Create a Car with these 3 properties. Recall that you can use the method nextLine() in the Scanner class to read a String.

b. Next, read 3 more values from the user and initialize a second Car object based on that.

c. Call the toString() methods on each Car to check their states.

d. Determine which car is to the left of the other car based on their location (small numbers mean left and large numbers mean right) Turn the car on the right around (using the turnAround()) method, so that it is facing the car on the left.

e.Call the toString() methods on each Car to check their states.

f. Use the go() method on each car to set their states to that they are moving.

g. Create a loop. At each step of your loop, you should call the move() method on each car. Then call the toString() methods to check their states. Your loop should stop when the Cars collide: That is, when their positions have switched so that the car that was initially on the right is now on the left or they are both in the same location

Your method should then print Boom!!

Part 2

Lets race

Add the following to the Car class:

a.Create a new parametrized constructor for the car class that takes only two parameters: the model of the car and its maximum speed. The rest of the fields should be initialized

to: location =0, facing forward, and current speed =0

b. Add a new methodaccelerate()to the Car class. When this method is invoked, the speed of the car is incremented by 1. If the car is already going at its maximum speed,

then accelerate should not do anything.

c. Add a new method brake() to the Car class. When this method is invoked, the speed of the car is decremented by 1. If the car is stopped, then brake should not do anything.

Once you have updated the Car class, create a new class RaceLane2 and add a main method in which you do the following:

a.Create an array of a user given number of cars. All cars should start from the same position. All cars should be facing the same direction. Different cars have different speeds. The maximum speed of a car ranges from 2 to 7.

b.When the race starts, all cars keeps accelerating to reach their maximum speed.

c.The length of the race track is 100 units. The user specifies how many laps the race consists of.

d.The race track has only 2 lanes. A crash happens when you have three cars at the same location. As long as all cars are still in the first lap (first 100 units), no crashes may

happen. Crash detection starts as soon as a car enters the second lap .When a crash is detected, the three cars should be removed from the race and all cars must stop. Then they start

accelerating again.

e.You need to develop a loop. All cars keeps racing until three cars finish the race or all cars have crashed.

f. Your program should display the status of each car in every loop iteration.

g. The three winning cars should be displayed at the end.

h. Hint : it may be useful to add a Boolean field crashed to keep track of crashed cars.

i. If all cars got crashed, your program should indicate that.

Feel free to add fields and/or methods as you see fit to both classes.

C Chogg Shudy | Guided Saluticn x Purpose: The purpose of this : https://moo dle concordia carr oodlo pluginfile.php/2842 119/mod label intro C24BAS 2017 80% Search Toyota Please enter the max speed of the car 0 please en te r .. nasc sp@ed of e ear i Please enter the nodel of car 2 Benzo ot the c Toyota Loc:ated t: 1, facing forvard end movingt 1 8peed Honda Located ati 1, tacing torward and Loving at 1 speed Ben=o Located at: 1, facing forward and moving at 1 5peed Toyota Located at! 3, facing forward and oving at speed Honda Located at ! 3, facing forvard and oving at 2 speed Benzo Located at ! 3 facing forward and oving at speed Toyota Loeated at: tacing torvard and noving at 3 apeed Honda Loeated at: 6. faeing torvard and moving at 3 speed Benzo Located dt: E, Ede ing Eorazd and moving at 3 speed Honda Located at ; 10, facing forward and novingt 4 speed Ben o Located at ; 10, f.cing forvard and novingt 4 8peed Toyota Located at: 15, tacing torvard and wovin at 3 speed Honda Located ati 15, tacing torard and noring at 5 speed Benzo Located at: 15, tacing torard and noving at 5 speed Toyota Located at: 20. Eacing forvard and noving at 5 apeed Honda Loeated at: 21. tacing torvard and oving at6 speed Benzo Loeated at: 21, tacing torvard and aoving at apeed Toyota Loc:ated at:s, fae ing forvard and moving at S spe-d Honda Located at: , facing forvard and noving at 6 speed Beno Located at: 28, tac: 1ng totvard and novingt 7 8peed Toyota Located at: 30, tacing torvard and oving at 5 speed Honda Located at: 33, tacing tovard and noring at 6 speed Ben=o Located ac: 35, facing forward and oving at 7 5peed Toyota Located at: 35, tacing torvard and ovin at s speed Honda Located at: 39, tacing torrard and noving at 6 speed 1:12 PM 27/11/2017 Type here to search ^ ENG 1 C Chogg Shudy | Guided Saluticn x Purpose: The purpose of this : https://moo dle concordia carr oodlo pluginfile.php/2842 119/mod label intro C24BAS 2017 80% Search Toyota Please enter the max speed of the car 0 please en te r .. nasc sp@ed of e ear i Please enter the nodel of car 2 Benzo ot the c Toyota Loc:ated t: 1, facing forvard end movingt 1 8peed Honda Located ati 1, tacing torward and Loving at 1 speed Ben=o Located at: 1, facing forward and moving at 1 5peed Toyota Located at! 3, facing forward and oving at speed Honda Located at ! 3, facing forvard and oving at 2 speed Benzo Located at ! 3 facing forward and oving at speed Toyota Loeated at: tacing torvard and noving at 3 apeed Honda Loeated at: 6. faeing torvard and moving at 3 speed Benzo Located dt: E, Ede ing Eorazd and moving at 3 speed Honda Located at ; 10, facing forward and novingt 4 speed Ben o Located at ; 10, f.cing forvard and novingt 4 8peed Toyota Located at: 15, tacing torvard and wovin at 3 speed Honda Located ati 15, tacing torard and noring at 5 speed Benzo Located at: 15, tacing torard and noving at 5 speed Toyota Located at: 20. Eacing forvard and noving at 5 apeed Honda Loeated at: 21. tacing torvard and oving at6 speed Benzo Loeated at: 21, tacing torvard and aoving at apeed Toyota Loc:ated at:s, fae ing forvard and moving at S spe-d Honda Located at: , facing forvard and noving at 6 speed Beno Located at: 28, tac: 1ng totvard and novingt 7 8peed Toyota Located at: 30, tacing torvard and oving at 5 speed Honda Located at: 33, tacing tovard and noring at 6 speed Ben=o Located ac: 35, facing forward and oving at 7 5peed Toyota Located at: 35, tacing torvard and ovin at s speed Honda Located at: 39, tacing torrard and noving at 6 speed 1:12 PM 27/11/2017 Type here to search ^ ENG 1

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

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago