Programming Assignment 5
GoKartTrack: Race GoKarts on a GoKart trackworth 25 points
A game designer needs you to write some backend code for a go-kart race video game. In this assignment, you will develop a GoKart class and will also create a class type representing a GoKartTrack that allows go-karts to be raced. Your classes and methods should be named as specified.
Step 1 – write class GoKart (10 points)
Define class GoKart as follows.
Define public static constants to represent the type of track a GoKart can race on: ASPHALT =1, DIRT = 2, ICE = 3. Also define MAX_FUEL = 10. Throughout the code, use these constants in place of integers where appropriate. I should be able to change the values of these constants (e.g. MAX_FUEL could be lowered or increased) and your code should still work if I change it just where the constant is defined.
Each object of this class should include the following private instance variables:
1.name- a string representing the name of the driver, e.g.MARIO.
2.type- an integer, representing the type of track the go-kart is optimized for: 1 - asphalt, 2 - dirt, 3 - ice;3.gasLevel- an integer representing how many gallons of gas are in the fuel tank,
4.numRaces- an integer representing how many races this kart has raced in.
All instance variables should be declared private, and no other instance variables should be included in this class.
Define methods for the GoKart class. We’ll start with accessors (getters) and mutators (setters). All mutators should be void, i.e. return no value.
instance method setName(), which should be passed a parameter of type String, rep- resenting the driver’s name. The method should check that the length of the string passed in as a parameter is greater than 0. Then the method should assign the instance variablenamethe value of parameter formatted to all capital letters.
instance method setType(), which should be passed a parameter of type int, representing the type of the item. If the item type is not one of 1, 2, or 3 (i.e. asphalt, dirt, ice, respectively), the item type should be set to 1, representing type asphalt. Otherwise, just set the type instance variable to the parameter that was passed in. Instead of using the numbers 1, 2, or 3, use the constants defined to represent these (ASPHALT, DIRT, ICE).
Accessor methodsfor each instance variable (there are 4 instance variables).
Further, define the methods described next and make sure to test them in the main method of the GoKart class as well. Be sure to follow the requirements on method names, input parameters, return value and functionality precisely. Remember to use the defined static constants throughout as well. The first is a static method, but the others are instance methods.
•A static methodconvertTypeToString(), which takes an integer representing the type of race track and returns a String, describing the corresponding type as a phrase, i.e. one of “asphalt”, “dirt” or “ice”.
Programming Project
Assignment#52•A 0-argument constructorthat calls the 2-argument constructor and sets name to “Unknown”
and type to the default, ASPHALT.
A 2-argument constructorthat accepts two parameters: a String representing the nameof the driver and an integer defining its type. The constructor needs to call the appropriatesetmethods to assign the values passed as parameters to the instance variablesnameandtype.
instance method toString(), which will be passed no parameters and returns a String describing the GoKart information as shown in this example (and you must use convertTypeToString() for displaying the type):
YOSHI (ice):raced 3 times,7 gallons in the tank
instance method raceCar()which will be called whenever somebody races the go-kart. The method should first check if the amount of gas left is greater than 0, and in such case increase the instance variablenumRacesby one, decrease the instance variablegasLevelby one gallon and return true. Otherwise, it should just return false.
instance method fuelUp()which will be called whenever the go-kart needs to be refueled, and therefore the amount of gas left should be increased. The method should receive an integer parameter defining the number of gallons to be added. The maximum capacity of a gokart fuel tank is MAX_FUEL and there still may be gas left when this method is called (as specified by thegasLevelinstance variable), so the method should first check how much gas can be added, then increase thegasLevelinstance variable of the calling object as much as it can without exceeding the limit. The method must return the number of gallons actually added.
instance method isCompatibleWith(). This method should be used to check if two go-karts are compatible to be raced on the same type of track (asphalt, dirt or ice). The method should accept a parameter of typeGoKartand return true if and only if both the calling object and the parameter object are made to race on the same track type. This comparison should be done using the accessor methods fortypefor the go-kart objects.
As you are developing these methods, create amain methodin the GoKart class totest all methods that you have developed. It is up to you how you do this, but you must test thoroughly. Do not remove the main method from this class when you are done. Do this before moving on to the next step.
Step 2 - create class GoKartTrack (15 points)
Further, define classGoKartTrack, that can be used in racing go-carts. Include the followingprivate instance variables:
•int numLanes- specifying how many lanes there are on the track (that is how many go-karts can be raced at one time).
•GoKart [] karts- storing an array of individual GoKart objects. This array should be createdin the constructor method and should initially contain null values instead of the actual objects. As go-karts get entered into the race (using methodaddGoKartdescribed below), the array should be populated, i.e. filled with individual GoKart objects.
•int lastUsedLane- an index of the last occupied spot in the karts array, i.e. the last occupied lane on the track. This variable should be initially set to -1, representing the fact that the track,or, technically speaking, the object’s karts array, is initially empty.
•int trackType- a variable that tells us the type of track (asphalt, dirt, ice).
There should be no other instance variables in this class, but there also should be the
following public static constants
public static final intSTANDARD_CAPACITY =10;
Add the following instance methods to classGoKartTrack. Be sure to follow the requirements on method names, input parameters, return value and functionality precisely.
Assignment#53 1.A 0-argument constructor.This constructor must call the 2 argument constructor to
initialize the instance variables to represent an asphalt track with number of lanes equal to STANDARD_CAPACITY. It should initialize the karts instance variable to an array of size equal to STANDARD_CAPACITY. It should set default values as described above.
2.A 2-argument constructor.This constructor must be passed two parameters: an integer representing the number of lanes on the track and an integer value representing the type of track. The constructor should initialize the instance variablesnumLanesandtrackTypeto the values passed through parameters. It should also initialize the k a r t s instance variable to an array ofsize equal tonumLanes.
3.methodaddGoKart(), that will add a goKart onto the track if there is a lane available and the kart is made to run on this type of track.
The method will be passed driver name (String) and type (an integer). The method should create a new object of typeGoKartbased on the specified parameters. It should first check if there are any lanes open and then whether the kart is the correct type to race on this track. If it meets both criteria, the new GoKart should be created, added to the next available lane in the k a r t s array, thelastUsedLanevariable must be incremented and the method should return true. Otherwise, if an item cannot be added, the method should print an appropriate message explaining the reason, using the phrase “full” or “incompatible” as appropriate for the error. e.g.
Cannot add go-kart, track isfull.or
Cannot add go-kart, track isincompatible.and return false.
4.method getGoKart(), that will be passed an integer parameter representing the lane number. Itwill return null if that lane is unfilled, otherwise it returns the GoKart object stored in that lane number. For this assignment, lane numbers start at 0. So if a track has 5 lanes, the lanes would be lane 0, lane 1, lane 2, lane 3, and lane 4.
5.instance method toString(), which will return a String, containing the description of the go-kart track. It shows the track type, whether it is a difficult track, the lane range and the last occupied lane, followed by the list of karts in the occupied lanes. Here is an example of a String that toString method should return on an ice track with 5 total lanes, 3 of which are occupied:
----- Go-Kart Track -----
Type: ice
Lanes: 0 - 4, occupied to lane 2
RACERS:
YOSHI (ice): raced 4 times, 7 gallons in the tank KOOPA (ice): raced 8 times, 4 gallons in the tank KING BOO (ice): raced 10 times, 6 gallons in the tank---------------------------
Another example, showing a dirt track,with 8 total lanes, 4 of which are occupied:
----- Go-Kart Track -----
Type: dirt
Lanes: 0 - 7, occupied to lane 3
RACERS:
PRINCESS PEACH (dirt): raced 14 times, 9 gallons in the tank LUDWIG (dirt): raced 2 times, 8 gallons in the tank
BOWSER (dirt): raced 3 times, 4 gallons in the tank
DIDDY KONG (dirt): raced 11 times, 1 gallons in the tank---------------------------
Assignment#54
Notethatthe string should be returned and not printed. To compose this string description, the method needs to loop through the GoKart objects in the karts array, appending each kart’s description (with String operator+)to the description of the GoKartTrack. Note that karts are listed in order from lane 0 to lastUsedSlot.
6.instance method race(). This method should be passed the lane number for the kart that is being raced. If that lane is not occupied, or the kart cannot be raced (e.g. has no fuel), the method should print an appropriate message, e.g.
There is no go-kart in lane 4.
or
and return false. Otherwise, the method runs a race and returns true. Note that the GoKart class already supports this type of functionality so you need to call the instance method to race rather than implementing it again here.
7.instance method fuelCar(). This method should be passed the lane number of the go-kart that is being fueled and how many gallons are to be added. If the lane number refers to an unoccupied lane, the method should print a message, e.g.
There is no go-kart in lane 4.
and return 0. Otherwise, it should call thefuelUpmethod on the specified GoKart object from the array and return the number of gallons that were actually added.
As you are working on this class, it is best to simultaneously work on testing your code in a main method ofthesameclassoranotherclass.Yourtestingwillnotbegradeddirectly,however,your grade for the project depends critically on how thoroughly you test your code.Below, I have included the output for a sample testing program that uses GoKart and GoKartTrack classes. Make sure that your code works on this sample program on the next page.
SubmitGoKart.javaandGoKartTrack.java
Yoshi is out of gas!
Assignment#55public static void main(String[] args) {
GoKartTrack track = new GoKartTrack(5, GoKart.ICE); System.out.println(track);
track.race(0); // Should detect that there is not a go-kart there
track.addGoKart("Bowser", GoKart.DIRT);// Shouldn’t add this since it’s type dirttrack.addGoKart("Yoshi", GoKart.ICE);
track.addGoKart("Koopa", GoKart.ICE);
track.addGoKart("King boo", GoKart.ICE);
track.addGoKart("Mario", GoKart.ICE); track.addGoKart("Luigi", GoKart.ICE); track.addGoKart("Toad", GoKart.ICE); System.out.println(track);
// track full, should not be able to add this
track.fuelCar(0, 2);
track.fuelCar(2, 12);
track.fuelCar(4, 10);
track.fuelCar(5, 6); // Should detect that there is no cart hereSystem.out.println(track);
track.race(0); track.race(0); track.race(0); // kart out of fuel, fails last race track.race(2);
track.race(3); // kart has no fuel, cannot race
System.out.println(track);}
The output produced by the main method above is shown below.
----- Go-Kart Track -----
Type: ice
Lanes: 0 - 4, occupied to lane -1 RACERS:
------------------------
There is no go-kart in lane 0
Cannot add go-kart, track is incompatible.
Cannot add go-kart, track is full.
----- Go-Kart Track -----
Type: ice
Lanes: 0 - 4, occupied to lane 4
RACERS:
YOSHI (ice): raced 0 times, 0 gallons in the tank KOOPA (ice): raced 0 times, 0 gallons in the tank KING BOO (ice): raced 0 times, 0 gallons in the tank MARIO (ice): raced 0 times, 0 gallons in the tank LUIGI (ice): raced 0 times, 0 gallons in the tank ------------------------
There is no go-kart in lane 5 ----- Go-Kart Track -----
Type: ice
Lanes: 0 - 4, occupied to lane 4 RACERS:
YOSHI (ice): raced 0 times, 2 gallons in the tank KOOPA (ice): raced 0 times, 0 gallons in the tank KING BOO (ice): raced 0 times, 10 gallons in the tank MARIO (ice): raced 0 times, 0 gallons in the tank LUIGI (ice): raced 0 times, 10 gallons in the tank ------------------------
Assignment#56YOSHI is out of gas!
MARIO is out of gas!
----- Go-Kart Track -----
Type: ice
Lanes: 0 - 4, occupied to lane 4
RACERS:
YOSHI (ice): raced 2 times, 0 gallons in the tank KOOPA (ice): raced 0 times, 0 gallons in the tank KING BOO (ice): raced 1 times, 9 gallons in the tank MARIO (ice): raced 0 times, 0 gallons in the tank LUIGI (ice): raced 0 times, 10 gallons in the tank ------------------------