Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. /*Race Class (includes main) */
  2. package HorseRace;
  3.  
  4. import javax.swing.JOptionPane;
  5.  
  6. public class Race {
  7.  
  8.     public static void main(String[] args) {
  9.         final int FINISHLINE = 100; // how far the finish line is (in spaces)
  10.         final int NUM_HORSES = 4;       // number of horses to include in race
  11.        
  12.         // create some horses
  13.         Horse[] horses = new Horse[NUM_HORSES];
  14.         for(int i = 0; i < NUM_HORSES; i++) {
  15.             horses[i] = new Horse("Horse_"+(i+1));
  16.         }
  17.        
  18.         // check the horse count
  19.         if( NUM_HORSES != Horse.getHorseCount()) {
  20.             System.out.println("Horse count does not match NUM_HORSES!");
  21.             System.out.println("NUM_HORSES:"+NUM_HORSES+" Horse count:"+Horse.getHorseCount());
  22.         }
  23.        
  24.         // Run the race
  25.         for(int i = 0;; i++) {
  26.             for(int j = 0; j < NUM_HORSES; j++) {
  27.                 horses[j].doStep(i);
  28.                 System.out.println("");
  29.             }
  30.             for(int j = 0; j < NUM_HORSES; j++) {
  31.                 if(horses[j].hasWon(FINISHLINE) == true) {
  32.                     JOptionPane.showMessageDialog(null,horses[j].getName()+" has won!");
  33.                     return;
  34.                 }
  35.             }
  36.             try{ Thread.sleep(30); } catch(Exception e) {}
  37.             System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  38.         }
  39.     }
  40. }
  41.  
  42.  
  43. /*Horse Class*/
  44. package HorseRace;
  45. import java.util.Random;
  46.  
  47. public class Horse {
  48.     protected double speed;
  49.     protected String name;
  50.     protected double location;
  51.     protected static int numHorses = 0;
  52.    
  53.     /*
  54.      * This is the class constructor, this is what is called when you make a new Horse object.
  55.      * In this case, the constructor must set the instance variables (speed, name, location) and
  56.      * update the class variable (numHorses).
  57.      * Hint: speed should be a random value((double)) (remember the random library from week 5?)
  58.      * Hint2: it might help to initialize location to something neutral (say 0) just to be safe
  59.      */
  60.     public Horse(String name) {
  61.        
  62.         //Set horse name, speed & location
  63.         this.name = name;
  64.        
  65.         Random r = new Random();
  66.         this.speed = r.nextDouble();
  67.        
  68.         this.location = 0.0;
  69.        
  70.         //update number of horses
  71.         Horse.numHorses +=1;
  72.     }
  73.    
  74.     /*
  75.      * doStep is called when the horse is running. It calculates the new location of the horse
  76.      * and sets the instance variable accordingly. To calculate new location use the
  77.      * formula: time*speed. This method should also call the drawHorse method.
  78.      */
  79.     public void doStep(int time) {
  80.    
  81.         //Calculate new location of the horse
  82.         this.location = (time*this.speed);
  83.        
  84.         //Call drawHorse method
  85.         drawHorse();
  86.     }
  87.    
  88.     /*
  89.      *  This method returns the value stored in the numHorses class variable.
  90.      */
  91.     public static int getHorseCount() {
  92.         return numHorses;
  93.     }
  94.    
  95.     /*
  96.      * This method returns the value stored in the name instance variable.
  97.      */
  98.     public String getName() {
  99.         return this.name;
  100.     }
  101.    
  102.     /*
  103.      * This method should print out a number of blank spaces equal to the integer portion of the
  104.      * value stored in the location instance variable. It should then print out the horse /V/"
  105.      * (don't forget to escape the quotation mark!)
  106.      * note: don't print out a newline for either of these print statements
  107.      */
  108.     private void drawHorse() {
  109.         int numSpaces;
  110.         numSpaces = (int) this.location;
  111.         String blanks ="";
  112.        
  113.         //print blank spaces
  114.         for(int i=0 ;i <= numSpaces; i++){
  115.         blanks += " "; 
  116.     }
  117.         //Add horse to end of blanks and print
  118.         System.out.println(blanks + "/V/\"");
  119.     }
  120.    
  121.     /*
  122.      * This method checks whether the horse's location is greater than or equal to the trackLength.
  123.      * If it is, then the method returns true, otherwise it returns false.
  124.      */
  125.     public boolean hasWon(int trackLength) {
  126.         if(this.location>= trackLength){
  127.             return true;
  128.         }else{
  129.             return false;
  130.         }
  131.     }
  132.    
  133.     /*
  134.      *  Method increments numHorses by 1.
  135.      */
  136.     public void updateHorseCount() {
  137.         Horse.numHorses +=1;
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement