In: Computer Science
*OBJECT ORIENTED PROGRAMMING*
JAVA PROGRAMMING
GOAL: will be able to throw and catch exceptions and create multi-threaded programs.
Part I
Part II
Part 1 :
Animal.java
package com.company; import java.util.Random; public class Animal extends Food implements Runnable { private String name; private int position; private int speed; private int restMax; public static boolean winner = false; public Animal(String name,int pos, int speed, int rest) { this.name = name; this.position =pos; this.speed = speed; this.restMax = rest; } @Override public void run() { try { while (position < 100 && !winner) { Random r = new Random(); int time = r.nextInt(restMax); Thread.sleep(time); position = position + speed; System.out.println(name + " is running with speed " + speed + " and is at position " + position); if(position>=100){ winner =true; System.out.println(name + " is winner."); break; } } } catch (InterruptedException e){ System.out.println(name + " interrupted."); } } public void start() { System.out.println(name + " starts to run."); Thread t; t = new Thread(this); t.start(); } }
Main.java
package com.company; public class Main { public static void main(String[] args) { Animal rabbit = new Animal("rabbit",0,5,150); Animal turtle = new Animal("turtle",0,3,100); rabbit.start(); turtle.start(); } }
Output :
rabbit starts to run.
turtle starts to run.
rabbit is running with speed 5 and is at position 5
turtle is running with speed 3 and is at position 3
rabbit is running with speed 5 and is at position 10
turtle is running with speed 3 and is at position 6
rabbit is running with speed 5 and is at position 15
turtle is running with speed 3 and is at position 9
rabbit is running with speed 5 and is at position 20
turtle is running with speed 3 and is at position 12
turtle is running with speed 3 and is at position 15
turtle is running with speed 3 and is at position 18
rabbit is running with speed 5 and is at position 25
turtle is running with speed 3 and is at position 21
rabbit is running with speed 5 and is at position 30
turtle is running with speed 3 and is at position 24
rabbit is running with speed 5 and is at position 35
turtle is running with speed 3 and is at position 27
turtle is running with speed 3 and is at position 30
turtle is running with speed 3 and is at position 33
rabbit is running with speed 5 and is at position 40
turtle is running with speed 3 and is at position 36
rabbit is running with speed 5 and is at position 45
turtle is running with speed 3 and is at position 39
turtle is running with speed 3 and is at position 42
rabbit is running with speed 5 and is at position 50
turtle is running with speed 3 and is at position 45
turtle is running with speed 3 and is at position 48
rabbit is running with speed 5 and is at position 55
turtle is running with speed 3 and is at position 51
turtle is running with speed 3 and is at position 54
rabbit is running with speed 5 and is at position 60
rabbit is running with speed 5 and is at position 65
turtle is running with speed 3 and is at position 57
turtle is running with speed 3 and is at position 60
turtle is running with speed 3 and is at position 63
rabbit is running with speed 5 and is at position 70
turtle is running with speed 3 and is at position 66
rabbit is running with speed 5 and is at position 75
rabbit is running with speed 5 and is at position 80
turtle is running with speed 3 and is at position 69
turtle is running with speed 3 and is at position 72
rabbit is running with speed 5 and is at position 85
rabbit is running with speed 5 and is at position 90
turtle is running with speed 3 and is at position 75
rabbit is running with speed 5 and is at position 95
turtle is running with speed 3 and is at position 78
rabbit is running with speed 5 and is at position 100
rabbit is winner.
Process finished with exit code 0
Explanation :
Animal.java :
void start() :
void run() :
Main.java :
Part 2 :
Food.java :
package com.company; import java.util.Random; public class Food { protected String food; synchronized protected void eat(String name,int rest) { try { Random r = new Random(); int time = r.nextInt(rest); System.out.println(name + " start eating " + food ); Thread.sleep(time); } catch (InterruptedException e) { System.out.println("Interrupted"); } } }
Animal.java :
package com.company; import java.util.Random; public class Animal extends Food implements Runnable { private String name; private int position; private int speed; private int restMax; public static boolean winner = false; public Animal(String name,int pos, int speed, int rest,String food) { this.name = name; this.position =pos; this.speed = speed; this.restMax = rest; super.food = food; } public void run() { try { while (position <= 100 && !winner) { eat(name,restMax); position = position + speed; if(position>100){ winner =true; } System.out.println(name + " is running with speed " + speed + " and is at position " + position); } if(position>100){ winner = true; System.out.println(name + " is winner."); } } catch (Exception e){ System.out.println(name + " interrupted."); } } public void start() { System.out.println(name + " starts to run."); Thread t; t = new Thread(this,name); t.start(); } }
Main.java :
package com.company; public class Main { public static void main(String[] args) { Animal rabbit = new Animal("rabbit",0,5,150,"fruits"); Animal turtle = new Animal("turtle",0,3,100,"grapes"); rabbit.start(); turtle.start(); } }
Output :
rabbit starts to run.
turtle starts to run.
rabbit start eating fruits
turtle start eating grapes
turtle is running with speed 3 and is at position 3
turtle start eating grapes
rabbit is running with speed 5 and is at position 5
rabbit start eating fruits
turtle is running with speed 3 and is at position 6
turtle start eating grapes
rabbit is running with speed 5 and is at position 10
rabbit start eating fruits
turtle is running with speed 3 and is at position 9
turtle start eating grapes
rabbit is running with speed 5 and is at position 15
rabbit start eating fruits
turtle is running with speed 3 and is at position 12
turtle start eating grapes
rabbit is running with speed 5 and is at position 20
rabbit start eating fruits
turtle is running with speed 3 and is at position 15
turtle start eating grapes
turtle is running with speed 3 and is at position 18
turtle start eating grapes
turtle is running with speed 3 and is at position 21
turtle start eating grapes
rabbit is running with speed 5 and is at position 25
rabbit start eating fruits
turtle is running with speed 3 and is at position 24
turtle start eating grapes
turtle is running with speed 3 and is at position 27
turtle start eating grapes
turtle is running with speed 3 and is at position 30
turtle start eating grapes
turtle is running with speed 3 and is at position 33
turtle start eating grapes
rabbit is running with speed 5 and is at position 30
rabbit start eating fruits
turtle is running with speed 3 and is at position 36
turtle start eating grapes
rabbit is running with speed 5 and is at position 35
rabbit start eating fruits
turtle is running with speed 3 and is at position 39
turtle start eating grapes
rabbit is running with speed 5 and is at position 40
rabbit start eating fruits
turtle is running with speed 3 and is at position 42
turtle start eating grapes
rabbit is running with speed 5 and is at position 45
rabbit start eating fruits
turtle is running with speed 3 and is at position 45
turtle start eating grapes
turtle is running with speed 3 and is at position 48
turtle start eating grapes
rabbit is running with speed 5 and is at position 50
rabbit start eating fruits
rabbit is running with speed 5 and is at position 55
rabbit start eating fruits
turtle is running with speed 3 and is at position 51
turtle start eating grapes
rabbit is running with speed 5 and is at position 60
rabbit start eating fruits
rabbit is running with speed 5 and is at position 65
rabbit start eating fruits
turtle is running with speed 3 and is at position 54
turtle start eating grapes
rabbit is running with speed 5 and is at position 70
rabbit start eating fruits
turtle is running with speed 3 and is at position 57
turtle start eating grapes
rabbit is running with speed 5 and is at position 75
rabbit start eating fruits
turtle is running with speed 3 and is at position 60
turtle start eating grapes
rabbit is running with speed 5 and is at position 80
rabbit start eating fruits
rabbit is running with speed 5 and is at position 85
rabbit start eating fruits
rabbit is running with speed 5 and is at position 90
rabbit start eating fruits
turtle is running with speed 3 and is at position 63
turtle start eating grapes
turtle is running with speed 3 and is at position 66
turtle start eating grapes
turtle is running with speed 3 and is at position 69
turtle start eating grapes
rabbit is running with speed 5 and is at position 95
rabbit start eating fruits
rabbit is running with speed 5 and is at position 100
rabbit start eating fruits
turtle is running with speed 3 and is at position 72
turtle start eating grapes
rabbit is running with speed 5 and is at position 105
rabbit is winner.
turtle is running with speed 3 and is at position 75
Process finished with exit code 0
Explanation :
Food.java :
eat() :
Main.java :
Output without using non-synchronized eat() method :
rabbit starts to run.
turtle starts to run.
rabbit start eating fruits
turtle start eating grapes
turtle is running with speed 3 and is at position 3
turtle start eating grapes
rabbit is running with speed 5 and is at position 5
rabbit start eating fruits
rabbit is running with speed 5 and is at position 10
rabbit start eating fruits
turtle is running with speed 3 and is at position 6
turtle start eating grapes
turtle is running with speed 3 and is at position 9
turtle start eating grapes
turtle is running with speed 3 and is at position 12
turtle start eating grapes
rabbit is running with speed 5 and is at position 15
rabbit start eating fruits
turtle is running with speed 3 and is at position 15
turtle start eating grapes
turtle is running with speed 3 and is at position 18
turtle start eating grapes
turtle is running with speed 3 and is at position 21
turtle start eating grapes
turtle is running with speed 3 and is at position 24
turtle start eating grapes
rabbit is running with speed 5 and is at position 20
rabbit start eating fruits
turtle is running with speed 3 and is at position 27
turtle start eating grapes
turtle is running with speed 3 and is at position 30
rabbit is running with speed 5 and is at position 25
rabbit start eating fruits
turtle start eating grapes
turtle is running with speed 3 and is at position 33
turtle start eating grapes
turtle is running with speed 3 and is at position 36
turtle start eating grapes
turtle is running with speed 3 and is at position 39
turtle start eating grapes
rabbit is running with speed 5 and is at position 30
rabbit start eating fruits
turtle is running with speed 3 and is at position 42
turtle start eating grapes
turtle is running with speed 3 and is at position 45
turtle start eating grapes
rabbit is running with speed 5 and is at position 35
rabbit start eating fruits
turtle is running with speed 3 and is at position 48
turtle start eating grapes
rabbit is running with speed 5 and is at position 40
rabbit start eating fruits
turtle is running with speed 3 and is at position 51
turtle start eating grapes
rabbit is running with speed 5 and is at position 45
rabbit start eating fruits
turtle is running with speed 3 and is at position 54
turtle start eating grapes
rabbit is running with speed 5 and is at position 50
rabbit start eating fruits
turtle is running with speed 3 and is at position 57
turtle start eating grapes
turtle is running with speed 3 and is at position 60
turtle start eating grapes
rabbit is running with speed 5 and is at position 55
rabbit start eating fruits
turtle is running with speed 3 and is at position 63
turtle start eating grapes
turtle is running with speed 3 and is at position 66
turtle start eating grapes
turtle is running with speed 3 and is at position 69
turtle start eating grapes
rabbit is running with speed 5 and is at position 60
turtle is running with speed 3 and is at position 72
rabbit start eating fruits
turtle start eating grapes
rabbit is running with speed 5 and is at position 65
rabbit start eating fruits
turtle is running with speed 3 and is at position 75
turtle start eating grapes
rabbit is running with speed 5 and is at position 70
rabbit start eating fruits
turtle is running with speed 3 and is at position 78
turtle start eating grapes
turtle is running with speed 3 and is at position 81
turtle start eating grapes
turtle is running with speed 3 and is at position 84
turtle start eating grapes
rabbit is running with speed 5 and is at position 75
rabbit start eating fruits
turtle is running with speed 3 and is at position 87
turtle start eating grapes
turtle is running with speed 3 and is at position 90
rabbit is running with speed 5 and is at position 80
rabbit start eating fruits
turtle start eating grapes
turtle is running with speed 3 and is at position 93
turtle start eating grapes
rabbit is running with speed 5 and is at position 85
rabbit start eating fruits
turtle is running with speed 3 and is at position 96
turtle start eating grapes
rabbit is running with speed 5 and is at position 90
rabbit start eating fruits
turtle is running with speed 3 and is at position 99
turtle start eating grapes
turtle is running with speed 3 and is at position 102
turtle is winner.
Explanation: The Highlighted output shows animals do not wait for another animal to finish and start eating together.