Object Oriented Programming - Assignment 1

Create AutomaticCar class that shall have private model, year, speed and gear attributes. Encapsulate complete state and expose the state for read/write. Do not give write access to gear attribute because the geat shall not directly updated by others, gear would auto change based on speed and other attributes. 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 set method speed attribute shall show error message of -ve value is passed, for >0, set the speed and gear appropriately. 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 car object would race, name these car objects: car1, car2 up to car5. There shall be multiple rounds of 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 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 traveled by each car after each hour. After 10 hours, the car with highest distance traveled 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 average gear number the winner car used during race.

How to upload: 
Zip both java files i.e. AutomaticCar.java and LuckyRacer.java and name the zip file to <Section-StudentName-RegistrationNo>.zip (e.g. C-AliZia-BCS-FA16-170.zip) and upload the zip file at Google Class Room. Please note, the zip file should not contain a folder but only 2 java source files, nothing else.

Total Marks: 10


  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