[Solved] COMP248 Assignment 5-Car and RaceLane

$25

File Name: COMP248_Assignment_5-Car_and_RaceLane.zip
File Size: 348.54 KB

SKU: [Solved] COMP248 Assignment 5-Car and RaceLane Category: Tag:
5/5 - (1 vote)

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.
    1. 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 Make sure the number entered is 0.
  1. A default constructor.
  1. 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.
  1. Write three methods called getModel(), getDirection(), and getLocation() that let you retrieve the model, movingForward, and location properties respectively.
  1. Write a method go() which sets the currentSpeed of the Car object to be the objects maxSpeed.
  1. Write a method stop() which sets the currentSpeed of the Car object to be 0.
  1. 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.
  1. Write a method move() which does the following:
  2. If movingForward is true, then it adds the currentSpeed to the location property.
  3. If movingForward is false, it subtracts the currentSpeed from the location property.
  1. 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

  1. Once you have defined the class, create a new class RaceLane1 and add a main method in which you do the following:
  1. 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.
  1. Next, read 3 more values from the user and initialize a second Car object based on that.
  1. Call the toString() methods on each Car to check their states.
  1. 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.
  1. Call the toString() methods on each Car to check their states.
  1. Use the go() method on each car to set their states to that they are moving.
  1. 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

Lets race J

Add the following to the Car class:

  1. 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

  1. Add a new method accelerate() to the Car 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.
  1. Add a new method brake() to the Car 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:

  1. 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.
  1. When the race starts, all cars keeps accelerating to reach their maximum speed.
  1. The length of the race track is 100 units. The user specifies how many laps the race consists of.
  1. 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.
  1. You need to develop a loop. All cars keeps racing until three cars finish the race or all cars have crashed.
  1. Your program should display the status of each car in every loop iteration.
  1. The three winning cars should be displayed at the end.
  1. Hint: it may be useful to add a Boolean field crashed to keep track of crashed cars.
  1. 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

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] COMP248 Assignment 5-Car and RaceLane
$25