Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. public class RaceCar
  2. {
  3. private int location;
  4. private int number;
  5. public static final int TRACK_LENGTH = 20;
  6.  
  7. public RaceCar()
  8. {
  9. location = 1;
  10. number = 0;
  11. }
  12.  
  13. public RaceCar(int n)
  14. {
  15. number = n;
  16. }
  17.  
  18. public int getLocation()
  19. {
  20. return location;
  21. }
  22.  
  23. public boolean raceGo() //should give 50% chance of moving forward one space.
  24. {
  25. double move = Math.random();
  26. if (move <= 0.5)
  27. {
  28. location++;
  29. return true;
  30. }
  31. else return false;
  32. }
  33.  
  34. public boolean atFinishLine()
  35. {
  36. if (location == TRACK_LENGTH)
  37. {
  38. return true;
  39. }
  40. else return false;
  41. }
  42. public String toString()
  43. {
  44. System.out.print("|");
  45. for(int i = 0; i < TRACK_LENGTH; i++)
  46. {
  47. if (location == i)
  48. {
  49. System.out.print(number);
  50. }
  51. else
  52. {
  53. System.out.print("-");
  54. }
  55. }
  56. System.out.print("|");
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement