Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Race Class (includes main) */
- package HorseRace;
- import javax.swing.JOptionPane;
- public class Race {
- public static void main(String[] args) {
- final int FINISHLINE = 100; // how far the finish line is (in spaces)
- final int NUM_HORSES = 4; // number of horses to include in race
- // create some horses
- Horse[] horses = new Horse[NUM_HORSES];
- for(int i = 0; i < NUM_HORSES; i++) {
- horses[i] = new Horse("Horse_"+(i+1));
- }
- // check the horse count
- if( NUM_HORSES != Horse.getHorseCount()) {
- System.out.println("Horse count does not match NUM_HORSES!");
- System.out.println("NUM_HORSES:"+NUM_HORSES+" Horse count:"+Horse.getHorseCount());
- }
- // Run the race
- for(int i = 0;; i++) {
- for(int j = 0; j < NUM_HORSES; j++) {
- horses[j].doStep(i);
- System.out.println("");
- }
- for(int j = 0; j < NUM_HORSES; j++) {
- if(horses[j].hasWon(FINISHLINE) == true) {
- JOptionPane.showMessageDialog(null,horses[j].getName()+" has won!");
- return;
- }
- }
- try{ Thread.sleep(30); } catch(Exception e) {}
- 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");
- }
- }
- }
- /*Horse Class*/
- package HorseRace;
- import java.util.Random;
- public class Horse {
- protected double speed;
- protected String name;
- protected double location;
- protected static int numHorses = 0;
- /*
- * This is the class constructor, this is what is called when you make a new Horse object.
- * In this case, the constructor must set the instance variables (speed, name, location) and
- * update the class variable (numHorses).
- * Hint: speed should be a random value((double)) (remember the random library from week 5?)
- * Hint2: it might help to initialize location to something neutral (say 0) just to be safe
- */
- public Horse(String name) {
- //Set horse name, speed & location
- this.name = name;
- Random r = new Random();
- this.speed = r.nextDouble();
- this.location = 0.0;
- //update number of horses
- Horse.numHorses +=1;
- }
- /*
- * doStep is called when the horse is running. It calculates the new location of the horse
- * and sets the instance variable accordingly. To calculate new location use the
- * formula: time*speed. This method should also call the drawHorse method.
- */
- public void doStep(int time) {
- //Calculate new location of the horse
- this.location = (time*this.speed);
- //Call drawHorse method
- drawHorse();
- }
- /*
- * This method returns the value stored in the numHorses class variable.
- */
- public static int getHorseCount() {
- return numHorses;
- }
- /*
- * This method returns the value stored in the name instance variable.
- */
- public String getName() {
- return this.name;
- }
- /*
- * This method should print out a number of blank spaces equal to the integer portion of the
- * value stored in the location instance variable. It should then print out the horse /V/"
- * (don't forget to escape the quotation mark!)
- * note: don't print out a newline for either of these print statements
- */
- private void drawHorse() {
- int numSpaces;
- numSpaces = (int) this.location;
- String blanks ="";
- //print blank spaces
- for(int i=0 ;i <= numSpaces; i++){
- blanks += " ";
- }
- //Add horse to end of blanks and print
- System.out.println(blanks + "/V/\"");
- }
- /*
- * This method checks whether the horse's location is greater than or equal to the trackLength.
- * If it is, then the method returns true, otherwise it returns false.
- */
- public boolean hasWon(int trackLength) {
- if(this.location>= trackLength){
- return true;
- }else{
- return false;
- }
- }
- /*
- * Method increments numHorses by 1.
- */
- public void updateHorseCount() {
- Horse.numHorses +=1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement