CS 1341 – Lab 7 Assignment
Overview
In Lab 6 you created the Truck Race game using object-oriented programming techniques. In this lab, you will improve your application to incorporate inheritance, file I/O, and exception handling
Pre-Lab
No pre-lab
Lab (100 Points)
Improve the TruckRace system following the design provided on the following pages. Your output should match the format shown in the instructions, although your actual output will vary based on the the outcome of the game. It’s recommended to build one class at a time and test that each works properly before continuing.
Submit the java and class files via Canvas (as a single zip- file). Include a comment block at the top of each Java file that includes your name, student id number, and “Lab 7 – Spring 2020”.
NOTES:
Each program should include comments that explain what
each block of code is doing. Additionally, the programs should compile without errors, and run with the results described in the exercise. The following deductions will be made from each exercise if any of the following is incorrect or missing:
Proper formatting [5 points]
Proper names for classes and variables [5 points]
Comments [5 points per class]
Program doesn’t compile [5 points for each minor error up to 5 errors provided that after fixing the errors the program compiles. If the program does not compiler after the 5 errors are fixed, partial credit will be given not to exceed 50 points] Source code (java file) missing [ 10 points]
Executable (class file) missing [10 points]
Missing array where an array was required [5 points each] Missing loop where a loop was required [5 points each] Missing class from the design provided [10 points each] Missing method from the design provided [ 5 points each]
This Lab is due Saturday April 18 at 6:00am.
GameLauncher
+main(String):void
State
-stateName : String
+State(String) +getStateName() : String +setStateName(String):void +getStartLocation():Location +getLastLocation():Location +move(Location,int):Location +showState(ArrayList): void
TruckRace
+TruckRace() +playGame(): void
Spinner
+spin():int
Truck
cars
+Truck(String,Location)
Car
Location
-label: String
+LocationString) +setLabel(String):void +getLabel():String +toString():String
currentLocation
#name : String
+Car(String,Location) +getName():String +setName(String):void +getCurrentLocation():Location +setCurrentLocation(Location):void +takeTurn(Spinner,State):void +isLocatedOn(Location) : boolean +toString():String #printWinnerCertificate(String): void
Coupe
+Coupe(String,Location) +takeTurn(Spinner, State_:void
1
1 -theState
-theSpinner 1
-locations 7
2-
# 1
GameLauncher
+main(String):void
State
Car:
-stateName : String
Spinner
+spin():int
TruckRace()
cars
Truck
+Truck(String,Location)
Car
winner is printed out
Location
-label: String
+LocationString) +setLabel(String):void +getLabel():String +toString():String
currentLocation
#name : String
+Car(String,Location) +getName():String +setName(String):void +getCurrentLocation():Location +setCurrentLocation(Location):void +takeTurn(Spinner,State):void +isLocatedOn(Location) : boolean +toString():String #printWinnerCertificate(String): void
Coupe
+Coupe(String,Location) +takeTurn(Spinner, State_:void
• Replace the Truck class with the Car class instead. It will +State(String)
1 TruckRace
function exactly as the Truck class did. Same functions + g e t S a t n a d t e e N v e a r my t e h ( i n ) g : . S t r i n g 1 +s•etSTthaitsewNiallmnoew(Sbtreinogu)r:vpoairdent class for the Truck and
+ g e t S C t o a u r p t L e o c c l a a s t i s o w n e ( ) : w L i o l l c b a e t i wo n r i t i n g – t h e S t a t e
• name and currentLocation are now protected +getLastLocation():Location
-theSpinner 1
• Write a new printWinnerCertificate method +move(Location,int):Location
+
+playGame(): void
2-
• This method is protected +showState(ArrayList): void
• Takes a String as a parameter
• Prints to file called winner.txt a certificate that
matches the one on the last slide • Be sure to use exception handling
-locations 7
• In the takeTurn method, print the certificate when the
# 1
GameLauncher
+main(String):void
State
-stateName : String
+State(String) +getStateName() : String +setStateName(String):void +getStartLocation():Location +getLastLocation():Location +move(Location,int):Location +showState(ArrayList): void
currentLocation
TruckRace()
TruckRace
cars
Spinner
Truck
+Truck(String,Location)
Car
Location
-label: String
+LocationString) +setLabel(String):void +getLabel():String +toString():String
#name : String
+Car(String,Location) +getName():String +setName(String):void +getCurrentLocation():Location +setCurrentLocation(Location):void +takeTurn(Spinner,State):void +isLocatedOn(Location) : boolean +toString():String
Coupe
+Coupe(String,Location) +takeTurn(Spinner, State_:void
1 -theState
1
Truck:
-theSpinner
• Clear out the Truck class that you had
-locations 7
making the super() call to the Car constructor
2-
+
inthepreviouslab.AlltheTruckclass +spin():int
needs now is the constructuor 1 +playGame(): void
# 1
GameLauncher
+main(String):void
State
-stateName : String
+State(String) +getStateName() : String +setStateName(String):void +getStartLocation():Location +getLastLocation():Location +move(Location,int):Location +showState(ArrayList): void
TruckRace
+TruckRace() +playGame(): void
Spinner
+spin():int
Truck
+Truck(String,Location)
Location
-label: String
+LocationString) +setLabel(String):void +getLabel():String +toString():String
currentLocation
Coupe
+Coupe(String,Location) +takeTurn(Spinner, State_:void
1
1 -theState
-theSpinner 1
Coupe:
• Have a constructor that calls the super()
-locations 7
2 -cars
constructor of the Car class
• The takeTurn method behaves a little differently in
Car
the Coupe class. Instead of using the Spinner just
# 1
move faster than trucks)
choose a random number between 0-2. (Coupes
#name : String
• Even though we aren’t using the Spinner +Car(String,Location)
anymore, we still need to pass it in since
+getName():String
we are using polymorphism and calling the
+setName(String):void
same method regardless of the object type • In t+hgeetatCkeuTrurrennmtLeothcoadti,opnri(n)t:Ltohecacteirotinficate when
the winner is printed out
+setCurrentLocation(Location):void +takeTurn(Spinner,State):void +isLocatedOn(Location) : boolean +toString():String
-stateName : String
a Coupe or Truck?” and depending on what they type in, add that type of
1
GameLauncher
TruckRace:
In the TruckRace constructor when
populating the cars(formerly trucks),
prompt the user ”Will this vehicle be
•
object to the array
+State(String) +getStateName() : String +setStateName(String):void +getStartLocation():Location +getLastLocation():Location +move(Location,int):Location +showState(ArrayList): void
-locations 7
State
TruckRace
+TruckRace() +playGame(): void
Spinner
+spin():int
1 -theState
-theSpinner 1
2-
cars
Car
#name : String
+Car(String,Location) +getName():String +setName(String):void +getCurrentLocation():Location +setCurrentLocation(Location):void +takeTurn(Spinner,State):void +isLocatedOn(Location) : boolean +toString():String
Location
-label: String
+LocationString) +setLabel(String):void +getLabel():String +toString():String
#currentLocation 1
+main(String):void
Truck
+Truck(String,Location)
Coupe
+Coupe(String,Location) +takeTurn(Spinner, State_:void
Sample Output
continued…
CURRENT LOCATION:
El Paso- Amarillo- Wichita Falls- Dallas Fort Worth- Houston- San Antonio- South Padre Island-
carTwo
carOne’s turn. Press Enter to spin… carOne spun 2 and is the Winner!
CURRENT LOCATION:
El Paso- Amarillo- Wichita Falls- Dallas Fort Worth- Houston- San Antonio- South Padre Island-
carTwo
carOne
carOne
java GameLauncher
How many cars are competing? 2
Will vehicle #1 be a Coupe or a Truck? Coupe Enter the name of the Coupe: carOne
Will vehicle #2 be a Coupe or a Truck? Truck
Enter the name of the CURRENT LOCATION:
El Paso- Amarillo- Wichita Falls- Dallas Fort Worth- Houston- San Antonio- South Padre Island-
Truck: carTwo
carOne’s turn. Press Enter to spin… carOne spun 2 and is on Location Amarillo
CURRENT LOCATION:
El Paso- Amarillo- Wichita Falls- Dallas Fort Worth- Houston- San Antonio- South Padre Island-
carOne
carTwo’s turn. Press Enter to spin… carTwo spun 2 and is on Location Amarillo
Sample Certificate Output
*********************************** CONGRATS!
Billy has won the game!!! ***********************************