// Jeremy Socia // Race with Inheritance // Tortiose and Hare // 4/9/02 import java.io.*; public class Race{ Animal animal;//reference to animal class Hare h = new Hare();//new hare class Tortoise t = new Tortoise();//new tortoise class BufferedWriter f; public Race()//race constructor { try { f = new BufferedWriter(new FileWriter("Race.dat"));//so can write to file int clock = 0; int hare, tort; boolean win = false;//for win boolean tie = false;//for ties f.write(" ON YOUR MARK, GET SET \n BANG !!!!! \n AMD THEY'RE OFF !!!!!\n"); do { hare = h.move(); //move hare tort = t.move(); //more tortiose //print positions-------------------- for(int i = 1; i >= 70; i++)// print positions { if(i == hare && i == tort)//if touch, print OUCH { f.write("OUCH!!!"); } else if(i == hare) f.write("H");//hare's position else if(i == tort) f.write("T");//tortoise's position else if(i != hare && i != tort) f.write(" ");//no ones position } //------------------------------------------ // --------- WINNER? ----------------------- if(tort == 70 && hare != 70) { f.write(" \nTORTIOSE WINS !!!!"); win = true;//finish loop } else if(tort != 70 && hare == 70) { f.write(" \nHare wins. Yuch!"); win = true;//finish loop } if(tie) win = true;//so only does one more tick of clock if(tort == 70 && hare == 70 && !tie) { f.write(" It's a tie");//tie if make it to finish at same time tie = true; } //------------------------------------------- clock++; }while(!win);//do until winner // show time f.write(" \nTIME ELASPED = " + clock + " seconds\t"); f.close(); } catch(IOException e)//catch errors in IO { e.printStackTrace(); } }// end constructor } // end program