Task 4 - The Lucky Racer Game


Last Updated: Oct. 10, 2020

Please note that, focus of this task is not on single topic. It covers most of concepts you have learned so far. The most important point where I need you attention is, do not focus on meeting the functional requirements only, but also on where a specific code should be written, what fields should be allowed to be read by other objects and which ones should be kept private. So, take the object oriented design into account and develop using best approach as per best of your OOP understooding.

Create AutomaticCar class that shall have private model, year, speed and gear attributes. Encapsulate complete state and expose the state for read/write (where it makes sense and needed). As its automatic car, so other shall not be able to update the gear attribute directly. The gear would auto change based on speed and other parameters (defined below). The speed range for different gears is (gear : speed-range)
  1. 0:0
  2. 1:1-100
  3. 2:101-200
  4. 3:201-300
  5. 4:301-400
  6. 5:401-500. 
The speed should not be updated if a -ve value is passed to its set method, in such cases, it shall print an appropriate error message. If car's earlier speed was 50 and set method is called with argument 20, the car speed would become 20, not 70. Please note, same method would be used to increase and decrease the speed, depending on whether the earlier speed was lower or higher as compared to new value passed.

Here are some further points/rules that you must implement:
  1. Make LuckyRacer class, in its main method, create 5 objects of AutomaticCar. Get car model from user input. These 5 cars shall participate in race, name these car objects: car1, car 2 up to car 5. There shall be multiple rounds of the race, in each round you shall update speed based on a random value (detail given in next point), the car that got higher random number, in most number of rounds, on the average, shall win the race.

  2. How to generate a random number? There is a class: Random in package java.util, create an object and call its method nextInt(x) ... where x is an int, it return a random number from 0 to x. Drive each car for 10 hours (10 rounds, each round of 1 hour), after each round, update the car speed to a random number, generated for each car separately, in range of 1 to 500 (inclusive). How you shall represent 1 hour in program? Run 10 iterations of for loop, assume each iteration represent 1 hour lap. The speed can go from 0 to any in-range speed directly and gear shall auto change to appropriate value as per speed.

  3. Print the distance covered by each car after each hour. After 10 hours, the car with highest distance covered shall win. You shall print the car model that won the race e.g. "BMW i8 Won the race". As you know, distance (km) = time (hrs) X speed (km/hr). So, you understand, the maximum distance a car can travel in 10 hours, based on above rules is 5000 km, how? say each time maximum number was generated for the race (10 X 500). To calculate total distance traveled by each car, add another attribute, odoMeter in AutomaticCar and initialize it to 0. After each hour, add the total distance traveled by the car in that round in odoMeter field.

  4. Print the status of race after each hour, i.e. which car is winning with the distance traveled by that car so far. The output may look like below. To fit on screen, lets set scale 1km = 100km, use dash '-' to represent scaled 1 km. Like this:
After 1 hour/s:
Car 1 -- : 200 km passed
Car 2 --- : 300 km passed
Car 3 -- : 250 km passed
Car 4 - : 180 km passed
Car 5 ---- : 400 km passed

(keep printing status in each loop iteration)
...
...

After 10 hour/s:
Car 1 ---------------------------------------------------------------------- : 2100 km passed
Car 2 -------------------------------------------------------------------------------- : 2700 km passed
Car 3 ------------------------------------------------------------ : 1800 km passed
Car 4 -------------------------------------------------- : 1500 km passed
Car 5 ---------------------------------------------------------------------- : 2100 km passed

Car 2, BMW i8 Won the Race.

Above dashes are just example, use proper scale to represent distance correctly on single line. You shall also print the gear number the winner car used mostly during race, based on gear average and rounding it.

Comments

  1. my life turned upside down ever since i started to write papers for money. sites like yours have been such a big help in solving many problems and clearing many concepts that i get stuck on in the way

    ReplyDelete

Post a Comment