Starting from:

$25

COMP248- Project 5 Solved

Purpose: 
 
The purpose of this assignment is to practice defining new types/classes and then using them in your program. 
CEAB/CIPS Attributes:            Design/Problem analysis/Communication Skills



 

General Guidelines When Writing Programs: 

-   Please refer to the handout of Assignment #1.

Part 1 
You will create two classes Car and RaceLane. Your main method will go in RaceLane1.

1.     Define a class Car. 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 getLocation() 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 object’s 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!!”

 

 

Here is a sample output to illustrate the expected behavior of your program.  Note: user input is surrounded by a black rectangle and is in purple.



             

Part 2 

Let’s race J 

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

Here are2 sample outputs to illustrate the expected behavior of your program.  Note: user input is surrounded by a black rectangle and is in purple.

 

 

Sample run 1: Three cars racing, two laps, different speeds no crash

 

 



….

 

 

 

Sample run 2: 4 cars racing, two laps, a crash between three cars… 

 





 





          

More products