Question
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
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. Heres 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started