programming
/**
*
This is the program driver (where the program starts)
* It is in charge of creating the race and it’s participants and telling them to “go” in the race.
* @author vjl8401
*/
public class RaceTrack //driver
{
public final static int raceDuration = 1000; //store the length of the race (can be accessed anywhere in code)
public static void main(String arg[])
{
//Instantiating my object instances
Engine engine = new Engine(67);
Vehicle c1 = new Car(1,engine,2); //this one I’m storing as the base class (vehicle)
Car c2 = new Car(2,new Engine(94),4); //this one I store as the sub class (car). There are differences but they do not come into play here
Truck t1 = new Truck(3,new Engine(87),2,250);
//This is an array
Vehicle[] allVehicles = new Vehicle[3];
//placing the vehicles into an array
allVehicles[0] = c1; //polymorphism (“isa”)
allVehicles[1] = c2; //remember just because I store these as vehicles doesn’t mean that the
allVehicles[2] = t1; //methods for them has changed. Each still stores its own go method.
//infinite loop (well without the base case it is)
while (true) //this will run until a race participant crosses the finish line (passes raceDuration)
{
int max=0;
//tell the cars to “go” one by one
for (int i=0; i //3 times { Vehicle v = allVehicles[i]; v.Go();//polymorphism System.out.println(v); max = Math.max(max,v.RaceProgress);
} System.out.println(); //check to see if someone has won the race if (max > raceDuration) { break;
} } System.out.println(“We have a winner!!! \n*** Vehicle “+RaceTrack.GetFurthestVehicle(allVehicles)+” ***”); } //just a helper method to find out which vehicle won the race public static int GetFurthestVehicle(Vehicle[] allVehicles) { int VIN=0; for (int i=0; i { max = allVehicles[i].RaceProgress; VIN = allVehicles[i].VIN; } return VIN; } class Engine { int speed; public Engine(int speed) { this.speed = speed; } /** * This is the original speed modifier (it may need to be redefined) * @return int random between half the speed and the whole speed */ public int SpeedModifier() { //returns speed/2 to maxSpeed return (int)(Math.random()*speed/2)+speed/2; } //Base class (abstract means that we can’t make Vehicles) //class’ responsibility to protect its data/attributes abstract class Vehicle extends Object { //attributes (fields int passengers; int VIN; int RaceProgress; Engine engine; //storage (association, aggregation, composition) //Constructors (used to create the objects): Vehicle(int vin, Engine e) { passengers = 1; RaceProgress = 0; VIN = vin; engine = e; } /** * This is the main function that progresses the vehicles through the race * this should be called each loop of the program (this must be redefined in each * subclass of vehicle) */ abstract public void Go(); //this is correctly coded, the abstract method only has its header, and no body – the body must be overwritten in subclasses /** * Part of object. This is invoked when an instance of this class is attempted to be used as a string (like during System.out.println) */ public String toString() { return “Vehicle: “+VIN+ ” Progress: “+RaceProgress; } public boolean equals(Object other) { return this.VIN == ((Vehicle)other).VIN; } public void reset() { * SubClass of Vehicle * class Car extends Vehicle //car “is a” vehicle { * Car Constructor (no return specified) * @param i = (0,100) * @param passengers * @param speed */ Car(int i, Engine e, int passengers) //Working constructor { //super or this //super(); implied super(i,e);//calling the constructor in Vehicle this.passengers = passengers; } return “Car::”+super.toString(); } //This is overwriting the super/base classes method //car satisfies the vehicles Go requirement public void Go() { RaceProgress += engine.SpeedModifier() – 10 * (passengers-1); } //Another subclass of Vehicle (this is considered a concrete class because it is not abstract) class Truck extends Vehicle { //This is data that exists only in trucks int towWeight;//special note that vehicle cannot access this. In order for it to do so a //cast operation must be applied to a valid truck. Truck(int i, Engine e, int passengers, int towWeight) { super(i, e); this.passengers = passengers; this.towWeight = towWeight; } return “Truck::”+super.toString(); } //truck satisfies the vehicles Go requirement public void Go() RaceProgress += engine.SpeedModifier() – (0.1f * towWeight); } 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 · 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. · 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:
int max=0;
}
}
}
RaceProgress = 0;
}
}
/**
*
*/
/**
public String toString()
{
}
public String toString()
{
{
}
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).
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.
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.
We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.
Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.
Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.
Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.
Someone from our customer support team is always here to respond to your questions. So, hit us up if you have got any ambiguity or concern.
Sit back and relax while we help you out with writing your papers. We have an ultimate policy for keeping your personal and order-related details a secret.
We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.
Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.
You don’t have to wait for an update for hours; you can track the progress of your order any time you want. We share the status after each step.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.
Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.
Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.
You can purchase this feature if you want our writers to sum up your paper in the form of a concise and well-articulated summary.
You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.
Join us for the best experience while seeking writing assistance in your college life. A good grade is all you need to boost up your academic excellence and we are all about it.
We create perfect papers according to the guidelines.
We seamlessly edit out errors from your papers.
We thoroughly read your final draft to identify errors.
Work with ultimate peace of mind because we ensure that your academic work is our responsibility and your grades are a top concern for us!
Dedication. Quality. Commitment. Punctuality
Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.
We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.
We understand your guidelines first before delivering any writing service. You can discuss your writing needs and we will have them evaluated by our dedicated team.
We write your papers in a standardized way. We complete your work in such a way that it turns out to be a perfect description of your guidelines.
We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.