Question: 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

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!