Name______________________________This lab practices the use of interfaces and inheritance. You should print a copy of this lab so you can record your answers to questions. Turn in the completed questions and any supporting printouts as required in the lab.
Simulations are an important application of computer science. Simulations can model lengthy processes in a short time, or dangerous experiments without the danger. In this lab you will be simulating cars on a highway.
Create a Vehicle class that will represent a Vehicle on the roadway. Each Vehicle must remember its speed and license number. The constructor should require this information. Include get methods for these two private items and a toString method to return the basic Vehicle information.
Next create a SpeedCheck class that has a label for the checkpoint (as in "Exit 23A" or "Corner of 23 ans W Main"), a speed limit (such as 45 mph), and the ability to process vehicles one at a time (a method public void passing(Vehicle v)), gathering statistics representing the number of vehicles, average speed, and license numbers of any speeding vehicles. You will need one method that returns a String reporting the statistics. A typical report might look like this:
Checkpoint Exit 23A - 60 mph limit Number of vehicles: 4 Average Speed: 63 mph Speeders: ASD-5678 QRT-1100
Keep in mind that this is returned from the object as a single String.
Write a main method in the SpeedCheck class that creates 4 Vehicles with speeds and license numbers of your choosing. Display each using its toString method, and then pass each through a SpeedCheck with an appropriate speed limit at "The corner of Waterloo and S Main." You want to catch at least two speeders. Display the speed report. Be sure to check that the program works even if there are no speeders and even if there are no Vehicles processed.
Subclass Vehicle to create two new classes, Car and Truck. For convenience, place these classes in the same file with Vehicle. Other than a constructor, no new methods are required in these classes (yet). Add statements to SpeedCheck main (after the existing statements) to generate random Vehicles in a loop. Random Vehicles will be generated from a new static method of the Vehicle class. You should add this to Vehicle and complete the ??'s.
private static Random speedgenerator = new Random();
private static int licnum = 1000;
public static Vehicle getInstance(int minspeed, int maxspeed){
if (speedgenerator.nextBoolean())
return new Car("TST-"+licnum++, speedgenerator.nextInt(??)+??);
else
return new Truck("TRK-"+??, ??);
}
Finish up main so about 20 cars are randomly generated (speeds between 50 and 70), displayed, and passed through a SpeedCheck on "I-76" with a 65 mph limit. Do not remove the code for the earlier tests; simply add more testing. Display the report after the loop terminates.
One more addition to the project needs to be made. The EPA has asked that you count the number of green Vehicles passing through the SpeedCheck. You agree to modify the SpeedCheck class to include this information. In SpeedCheck, add a greenCounter integer member, and modify the passing method to count green Vehicles. Use the following statement (the variable names are your choice but the structure shown here is non-negotiable):
if (v.isGreen())greenCount++;
Of course, this will fail. Why?
Answer:
Create an interface named EPAMeasurable that can be used to ensure certain objects incude the isGreen() method. Add the required implements clause to the Vehicle class. Now you will need to implement the method in Vehicle. However, the definition of isGreen depends on the type of Vehicle, so we will satisfy the interface requirement by adding the required method to Vehicle, but making it abstract. Do this and then tell why Vehicle must also be abstract.
Answer:
You must also modify the report contents in SpeedCheck to include a statement displaying the number of and percentage of green Vehicles passing the checkpoint.
At this point, all should be well in Vehicle and EPAMeasurable, but there will be problems in Car, Truck, and SpeedCheck. Implement the required methods in Car and Truck. Cars are considered green if their speed is 55 or less. Trucks are never green.
Now, Everything should be OK except for the code in main. Specifically, the test statements written at the beginning of this exercise will no longer be valid. These are the ones that use the syntax new Vehicle(.... Why are these instantiations no longer allowed?
Answer:
Simply place a block comment around this initial section of main; retain only the loop that creates the random Vehicles. Test and correct any bugs you may detect. Make sure nothing fails if there are no green vehicles. Get a printed copy of SpeedCheck, Vehicle (which includes Car and Truck), and EPAMeasurable. Be sure your name apears in each file.
Why must Vehicle be a class and not an interface?
Answer:
Why is it unnecessary to say Car implements EPAMeasurable? In your answer, explain why the statement "a Car is an EPAMeasurable" is true.
Answer:
We could have left the implements clause off of the Vehicle class and simply added it to the Car and Truck class. In this way, the Vehicle class could remain concrete (not abstract). If this change is made, how would the code have to be rewritten in SpeedCheck's passing method? Specifically, tell what change would need to be made to the folowing statement to make it legal. What runtime error might occur in this code if this change is made?
if (v.isGreen())greenCount++;
Answer:
You will need to attach the three printouts to this report with a staple. Be sure your name is on each page.