COSC1437Lab1-RaceTrack.docx

COSC 1437

Lab 3: Understanding OOPs

Your task is to take the race track program presented in class and add several parts to it. This is a demonstration of the techniques from chapter 3 and chapter 6. You will be creating a class based on the examples given, as well as modifying their access levels (chapter 3). You will also work with the relationship between car object and the engine object (chapter 6).

· Creating new objects - Add a new class called Motorcycle. It should extend vehicle which will allow it to be a part of the array with other vehicles. Motorcycles have great speed (the horsepower you should set for your motorcycle’s engine should be 95) but are dangerous and will have a chance to crash. The vehicle class has a blank (abstract) Go() method. To extend Vehicle you must give a definition for Go. Do so like Car, and Truck but add a conditional statement that checks for the motorcycles chance to crash which is 2% per loop do this by checking a random number (math.random() < .02f) and creating another field in motorcycle called “hasCrashed” to store the result. If the motorcycle crashes then it should not progress in the race any further. The motorcycle is allowed to participate in future races in the season however (so hasCrashed should be reset for each new race).

· There should be 25 races in a season. Add a loop to make 25 races happen. At the end of the season output the win totals of each vehicle as well as the vehicle with the most wins.

· Encapsulation and access modifiers (ownership of data) - Next we need to redefine the way passengers are kept track of in all vehicles. First passengers, Vin, and RaceProgress should be a private variable which can only be accessed by appropriate accessor and mutator methods (which you will have to add). VIN should be made a final variable since there is never any reason to change it (hint do we need a mutator method for this field?). Finally, we should add a new final variable for the maxOccupancy of the vehicle (do not add it to the subclasses). The max occupancy should be checked in the mutator method for passengers. The max occupancy should be defined as:

· Car = 5

· Truck = 3

· Motorcycle = 1

If more passengers than are allowed attempt to pile into a vehicle the program should indicated this to the user and continue with the max passenger amount. This can be accomplished by using the super call in the subclass’s (car, truck, motorcycle) constructors to pass in their specific value. There are so rules here that I am forcing you to learn when it comes to creating final variables (specifically that the maxOccupancy must be set by the working constructor of Vehicle). If you run into problems, first try to make simple examples to try and learn these basics, and if you continue to run into errors reach out to your instructor for help. Note: you do not need to make fields/variables in other classes private (like engine for example). I am only going to look at Vehicle when grading this step and figure if you can do it correctly for one class then you should be able to do it for others.

· Aggregation vs Composition - Currently the engine variable is an aggregation relationship to the vehicle class – your job is to convert the relationship to a composition one. You have 2 options to complete this that you will demonstrate: With the car class create copy of the engine within the constructor (you may write a copy constructor for the Engine class to accomplish this) and store that appropriately. The vehicle class already takes an Engine into its constructor. You will need to modify this so that it creates a deep copy (again, using the copy constructor) and stores that instead of the direct reference to the passed in Engine. With the truck class change the constructor so that an int for the horsepower is passed into the truck constructor instead of the entire engine (this is the only data needed to create an engine) use this to create the engine within the vehicle (truck can pass the int up into vehicle if you add a constructor for this in vehicle). Since truck/Vehicle creates its own engine within the object it is safer code than creating it in main or who-knows-where. The motorcycle can be left however you choose and doesn’t have to have its Engine modified.