Implement and test the following program Trace the output so
Implement and test the following program. Trace the output so that you understand the operation of the program. Warning: Cutting and pasting code may cause errors! public class TurnTaker { private static int turn = 0; private int myTurn; private String name; public TurnTaker(String n, int t) { name = n; myTurn = t; } public String getName() { return name; } public static int getTurn() { turn++; return turn; } public boolean isMyTurn() { if (turn==myTurn) return true; else return false; } } public class TurnTakerDemo { public static void main(String[] args) { TurnTaker person1 = new TurnTaker(\"Romeo\", 1); TurnTaker person2 = new TurnTaker(\"Juliet\", 3); for(int i = 1; i<= 5; i++) { System.out.println(\"Turn = \" + TurnTaker.getTurn()); if (person1.isMyTurn()) System.out.println(\"Love from \"+ person1.getName()); if (person2.isMyTurn()) System.out.println(\"Love from \" + person2.getName()); } } } Exercise 2 (a) : Modify the program so that you get the following output: Turn = 1 Love from Romeo Love from Juliet Turn = 2 Love from Romeo Love from Juliet Turn = 3 Love from Romeo Love from Juliet Turn = 4 Love from Romeo Love from Juliet Turn = 5 Love from Romeo Love from Juliet Exercise 2(B): Modify the program as follows: The program should prompt the user for the number of turns. Each odd turn should print “Love from Romeo” and each even turn should print “Love from Juliet”, except the last turn when it prints both “Love from Romeo” and “Love from Juliet”. Here’s a sample dialog: How many turns? 7 Turn = 1 Love from Romeo Turn = 2 Love from Juliet Turn = 3 Love from Romeo Turn = 4 Love from Juliet Turn = 5 Love from Romeo Turn = 6 Love from Juliet Turn = 7 Love from Romeo Love from Juliet
Solution
Exercise 2 (a) : Modify the program so that you get the following output: Turn = 1 Love from Romeo Love from Juliet Turn = 2 Love from Romeo Love from Juliet Turn = 3 Love from Romeo Love from Juliet Turn = 4 Love from Romeo Love from Juliet Turn = 5 Love from Romeo Love from Juliet
Class TurnTaker.java
____________________________________________________________
public class TurnTaker {
private static int turn = 0;
private int myTurn;
private String name;
public TurnTaker(String n, int t)
{
name = n; myTurn = t;
}
public String getName()
{ return name;
}
public static int getTurn()
{
turn++;
return turn;
}
public boolean isMyTurn()
{
if (turn==myTurn)
return true;
else return false;
}
}
---------------------------------------------------------------------------------------------------------------------------------------
Class TurnTakerDemo.Java
-----------------------------------------------------------------------------------------------------------------------------
public class TurnTakerDemo
{
public static void main(String[] args){
TurnTaker person1 = new TurnTaker(\"Romeo\", 1);
TurnTaker person2 = new TurnTaker(\"Juliet\", 2);
TurnTaker person3 = new TurnTaker(\"Juliet\",3);
TurnTaker person4 = new TurnTaker(\"Juliet\", 4);
TurnTaker person5 = new TurnTaker(\"Juliet\", 5);
for(int i = 1; i<= 5; i++)
{
System.out.println(\"Turn = \" + TurnTaker.getTurn());
if (person1.isMyTurn()) System.out.println(\"Love from \"+ person1.getName() +\"\\tLove from \"+ person2.getName());
if (person2.isMyTurn()) System.out.println(\"Love from \" + person1.getName()+\"\\tLove from \"+ person3.getName());
if (person3.isMyTurn()) System.out.println(\"Love from \" + person1.getName()+\"\\tLove from \"+ person4.getName());
if (person4.isMyTurn()) System.out.println(\"Love from \" + person1.getName()+\"\\tLove from \"+ person5.getName());
if (person5.isMyTurn()) System.out.println(\"Love from \" + person1.getName()+\"\\tLove from \"+ person2.getName());
}
}
}
_____________________________________________________________________________________
Exercise 2(B): Modify the program as follows: The program should prompt the user for the number of turns. Each odd turn should print “Love from Romeo” and each even turn should print “Love from Juliet”, except the last turn when it prints both “Love from Romeo” and “Love from Juliet”. Here’s a sample dialog: How many turns? 7 Turn = 1 Love from Romeo Turn = 2 Love from Juliet Turn = 3 Love from Romeo Turn = 4 Love from Juliet Turn = 5 Love from Romeo Turn = 6 Love from Juliet Turn = 7 Love from Romeo Love from Juliet
TurnTaker.java
___________________________________________________________________________________
public class TurnTaker {
private static int turn = 0;
private int myTurn;
private String name;
public TurnTaker(String n, int t)
{
name = n; myTurn = t;
}
public String getName()
{ return name;
}
public static int getTurn()
{
turn++;
return turn;
}
public boolean isMyTurn()
{
if (turn==myTurn)
return true;
else return false;
}
}
__________________________________________________________________________________
TurnTakerDemo.java
____________________________________________________________________________________
public class TurnTakerDemo
{
public static void main(String[] args){
TurnTaker person1 = new TurnTaker(\"Romeo\", 1);
TurnTaker person2 = new TurnTaker(\"Juliet\", 2);
TurnTaker person3 = new TurnTaker(\"Juliet\",3);
TurnTaker person4 = new TurnTaker(\"Juliet\", 4);
TurnTaker person5 = new TurnTaker(\"Juliet\", 5);
for(int i = 1; i<= 5; i++)
{
System.out.println(\"Turn = \" + TurnTaker.getTurn());
if (person1.isMyTurn()) System.out.println(\"Love from \"+ person1.getName());
if (person2.isMyTurn()) System.out.println(\"Love from \"+ person3.getName());
if (person3.isMyTurn()) System.out.println(\"Love from \" + person1.getName());
if (person4.isMyTurn()) System.out.println(\"Love from \"+ person5.getName());
if (person5.isMyTurn()) System.out.println(\"Love from \" + person1.getName()+\"\\tLove from \"+ person2.getName());
}
}
}


