Task 4 - OOP Assignment 1

Write a class AutomaticCar that has private model, year, speed and gear attributes. Define public  get methods for all private attributes and set method for model, year and speed. The gear would not be directly updated by other objects (mean by client code). In AutomaticCar, gear would auto change based on speed, other attributes. The speed range for different gears is, gear:speed-range 0:0, 1:1-100, 2:101-200, 3:201-300, 4:301-400, 5:401-500. Define setSpeed(int speed), if -ve value is passed for speed, show error message. For >0, set the speed and gear appropriately. If car's earlier speed was 50 and setSpeed method is called with argument 20, the car speed would become 20, not 70. So notice, 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 is the interesting part:
1. In setSpeed method, update speed and gear value as per gear:speed-range rules above.

2. Make LuckyRacer class, in main method, create 5 objects of AutomaticCar. Get car model from user input. These 5 cars would race, name these car objects: car1, car2 up to car5. There would be multiple round in the race, in each round you would update speed based on a random value (detail given in next point), the car that got higher random number in most rounds on the average, would win the race. So we can call that car's driver, lucky racer.

How to generate a random number: There is a class: Random in packaged java.util, create an object and call its method nextInt(x) ... where x is an int, it would generate a random number from 0-x. Use it as per your requirements.

3. Let the each car run for 10 hours (10 rounds, each round of 1 hour) and after each hour or round update the car speed to a random number, generated for each car separately, in range 1 to 500 (inclusive). How you would represent 1 hour? Run a loop for 10 times, assume each iteration represent 1 hour lap. The speed can go from 0 to any in-range speed directly, and gear should be automatically changed to appropriate value.

4. Print the distance traveled by each car after each hour. After 10 hours, the car with highest distance traveled would win. You would 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 distance traveled, add another attribute, odoMeter in AutomaticCar and initialize it to 0. After each hour, add the total distance traveled by the car in that hour in odoMeter attribute. 

5. 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 something 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 for example ... use proper scale to represent distance correctly on single line.

Total Marks 10

Update 1: Also print the average gear number winner car used during race.


  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


Post a Comment