Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Consider the Original Counter class below: public class Original Counter { public final boolean biti; public final boolean bit); public Original Counter(final boolean biti, final
Consider the Original Counter class below: public class Original Counter { public final boolean biti; public final boolean bit); public Original Counter(final boolean biti, final boolean bit) { this.bit1 = biti; this.bito - bito; } public Original Counter increment() { if (!bit1 && !bito) { // 00 return new Original Counter(false, true); // 01 } else if (!biti && bit) { // 01 return new Original Counter(true, false); // 10 } else if (bit1 && !bito) { // 10 return new Original Counter(true, true); // 11 } else { // should be 11 return new Original Counter(false, false); // 00 } } public int toInt() { if (!biti && !bito) { // 00 return 0; } else if (!biti && bit) { // 01 return 1; } else if (bit1 && !bit0) { // 10 return 2; } else { // should be 11 return 3; } } public static void main(String[] args) { Original Counter counter = new Original Counter(false, false); while (true) { System.out.println(counter.toInt(); counter = counter.increment(); Original Counter represents two bits, and can increment them. The main method prints 0, 1, 2, 3, 0, 1, 2, 3, and so on, for infinity. This code can be refactored to use virtual dispatch. Rather than use two boolean values, we can instead represent each possible pair of values as a distinct class. A modified main is shown below, which uses this style, changing Original Counter to NewCounter: public static void main(String[] args) { NewCounter counter = new ZeroZero(); while (true) { System.out.println(counter.toInt()); counter = counter.increment(); } } The above main produces the same output as below. Implement the ZeroZero class, along with any additional classes needed to make this output be the same, below
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