Here is the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | import java.util.*; class HorseSelected{ static String selectedname=""; } class Score{ int position=0; static boolean winner=false; } class Horse implements Runnable{ Score s=new Score(); String horsename=null; Random rand=new Random(); public Horse(String stemp){ horsename=stemp; } public void run(){ System.out.println(horsename+" is start running."); try{ for(int i=0;i<5;i++){ Thread.currentThread().sleep(rand.nextInt(10000)); s.position++; if(s.winner==false){ System.out.println("Position "+horsename+":"+s.position); } if((s.position==5)&&(s.winner==false)){ System.out.println(horsename+" wins!"); s.winner=true; if((HorseSelected.selectedname).equals(horsename)){ System.out.println("Congratulation! You selected the right horse."); } else{ System.out.println("You selected the wrong horse!"); } Thread.currentThread().interrupt(); } } }catch(Exception e){ } } } public class HorseRacing{ public static void main(String[] args){ HorseRacing ap=new HorseRacing(); } public HorseRacing(){ System.out.print("Pick one (1.Horse1, 2.Horse2, 3.Horse3): "); Scanner sc=new Scanner(System.in); String st=sc.nextLine(); switch(st){ case "1": HorseSelected.selectedname="Horse1"; break; case "2": HorseSelected.selectedname="Horse2"; break; case "3": HorseSelected.selectedname="Horse3"; break; default: System.out.println("Pick a horse!"); break; } System.out.println(""); Horse horse1=new Horse("Horse1"); Horse horse2=new Horse("Horse2"); Horse horse3=new Horse("Horse3"); Thread t=new Thread(horse1); Thread t2=new Thread(horse2); Thread t3=new Thread(horse3); t.start(); t2.start(); t3.start(); } } |
class HorseSelected{ static String selectedname=""; }
This class stores the name of the horse that is selected by user.
class Score{ int position=0; static boolean winner=false; private void showScore(){ System.out.println(position); } }
The position of the horses are stored in a variable named "position". The variable "winner" is used to determine whether the race has been finished or still continues.
class Horse implements Runnable{ . . . }
To implement multithreading, this class implements Runnable interface.
public void run(){ System.out.println(horsename+" is start running."); try{ for(int i=0;i<5;i++){ . . . } }catch(Exception e){ } }
This is the most important method in this program. Each object of the class "Horse" runs simultaneously. The for() loop determines the initial position of each horse ("0") and the last position/finish line ("5").
Thread.currentThread().sleep(rand.nextInt(10000));
Method sleep( ) is called to cease execution for 10000 milliseconds so the race does not run too fast.
s.position++; if(s.winner==false){ System.out.println("Position "+horsename+":"+s.position); }
The position of each horse will always be updated and displayed on the screen as long as the race still continues (no winner yet).
if((s.position==5)&&(s.winner==false)){ System.out.println(horsename+" wins!"); s.winner=true; if((HorseSelected.selectedname).equals(horsename)){ System.out.println("Congratulation! You selected the right horse."); } else{ System.out.println("You selected the wrong horse!"); } Thread.currentThread().interrupt(); } }
If one of the horses reaches the finish line and there is no winner yet, print the name of the horse. The horse selected by user is then compared with the winner. If he selected the right horse, the message ""Congratulation! You selected the right horse." will appear on the screen.
System.out.print("Pick one (1.Horse1, 2.Horse2, 3.Horse3): "); Scanner sc=new Scanner(System.in); String st=sc.nextLine(); switch(st){ case "1": HorseSelected.selectedname="Horse1"; break; case "2": HorseSelected.selectedname="Horse2"; break; case "3": HorseSelected.selectedname="Horse3"; break; default: System.out.println("Pick a horse!"); break; }
User is asked to pick his horse. The switch() statement converts the user input to horse's name and store it in the variable "selectedname".
Horse horse1=new Horse("Horse1"); Horse horse2=new Horse("Horse2"); Horse horse3=new Horse("Horse3"); Thread t=new Thread(horse1); Thread t2=new Thread(horse2); Thread t3=new Thread(horse3);
The above lines of code show the creation of thread objects. Three threads are created here.
t.start(); t2.start(); t3.start();
Method run() in the class "Horse" is called.
No comments:
Post a Comment