Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RaceCar
- {
- private int location;
- private int number;
- public static final int TRACK_LENGTH = 20;
- public RaceCar()
- {
- location = 1;
- number = 0;
- }
- public RaceCar(int n)
- {
- number = n;
- }
- public int getLocation()
- {
- return location;
- }
- public boolean raceGo() //should give 50% chance of moving forward one space.
- {
- double move = Math.random();
- if (move <= 0.5)
- {
- location++;
- return true;
- }
- else return false;
- }
- public boolean atFinishLine()
- {
- if (location == TRACK_LENGTH)
- {
- return true;
- }
- else return false;
- }
- public String toString()
- {
- System.out.print("|");
- for(int i = 0; i < TRACK_LENGTH; i++)
- {
- if (location == i)
- {
- System.out.print(number);
- }
- else
- {
- System.out.print("-");
- }
- }
- System.out.print("|");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement