Question
In mathematics, a number is expressed in positional notation to a certain base, B. For example, the 3-digit number 123 in base 4 represents 16+8+3=27
In mathematics, a number is expressed in positional notation to a certain base, B. For example, the 3-digit number 123 in base 4 represents 16+8+3=27 (base 10).
Each digit is represented as a Counter object. A Counter object has an optional left neighbour which is also a Counter object. (The absence of a left neighbour is indicated with the keyword null)
If there is no left neighbour, the count is the same as the digit.
If there is a left neighbour, the count is the sum of the digit and the modulus times the count of the left neighbour.
The increment() method increment's the Counter's digit and, if it reaches its maximum (modulus) value, it is reset to zero. Furthermore, if there is a left neighbour and if the Counter has rolled over, its left neighbour should be incremented as well.
Fill in the code please:
public class Counter { //Instance variables here public Counter(int modulus, Counter left) { } /** * @return the modulus */ public int getModulus() { return 1; } /** * Returns the Counter to the left attached to this * Counter. Returns null if there is no Counter * to the left. * @return the left */ public Counter getLeft() { return null; } /** * @return the digit */ public int getDigit() { return 1; } /** * @param digit the digit to set */ public void setDigit(int digit) { } /** * Increment this counter. If it rolls over, * its left Counter is also incremented if it * exists. */ public void increment() { } /** Return the count of this Counter combined * with any Counter to its left. * * @return the count */ public int getCount() { return 1; } /** Returns a String representation of the Counter's * total count (including its left neighbour). * @return the String representation */ @Override public String toString() { //DO NOT MODIFY THIS CODE return "" + getCount(); } }
public class CounterTry { /** * The output should be: ** d0 = 0 * d0 = 1 * d0 = 2 * d0 = 0 * * Two digit counter... * d1, d2 digits: 00 d0 count = 0 * d1, d2 digits: 01 d0 count = 1 * d1, d2 digits: 02 d0 count = 2 * d1, d2 digits: 03 d0 count = 3 * d1, d2 digits: 10 d0 count = 4 * d1, d2 digits: 11 d0 count = 5 * d1, d2 digits: 12 d0 count = 6 * d1, d2 digits: 13 d0 count = 7 * d1, d2 digits: 20 d0 count = 8 * d1, d2 digits: 21 d0 count = 9 ** @param args the command line arguments */ public static void main(String[] args) { Counter d1, d0; d0 = new Counter(3, null); for (int i = 0; i < 4; i++) { System.out.println("d0 = " + d0); d0.increment(); } System.out.println(" Two digit counter..."); d1 = new Counter(4, null); d0 = new Counter(4, d1); for (int i = 0; i < 10; i++) { System.out.println("d1, d2 digits: " + d1.getDigit() + d0.getDigit() + " d0 count = " + d0); d0.increment(); } } }
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