Template package lab8 import javautilArrays import javautilS
Template:
package lab8;
import java.util.Arrays;
import java.util.Scanner;
public class Lab8 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(true) {
// prompt user to supply a base
// parse the base as an integer
//
// prompt user to supply a list of digits separated by space
// parse the input string (for digits) to an array of digits
//
// create a based number object of the base and the digit array and print its value
//
// catch exceptions as required (number format, illegal base, illegal digits)
}
in.close();
}
}
// define illegal base exception class
//
// define illegal digit exception class
class BasedNumber {
private int base;
private int[] digits;
BasedNumber(int base, int[] digits) {
// check base and throw exception if it is not great than 1
// if the digit array is empty
// if the digits is not within legal range (specified by the base)
}
int getBase() { return base; }
int getDigit(int n) { return digits[n]; }
int getValue() {
int ret = 0;
for(int i=0; i ret = ret * base + digits[i];
}
return ret;
}
boolean equals(BasedNumber that) {
return getValue() == that.getValue();
}
}
Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
---------------------------------------------------------------------------
OUTPUT:
Enter base:
2
Enter a list of digits separated by space:
1 0 0 1
The value for Base = 2 and digits = [1, 0, 0, 1] is 9
Enter base:
16
Enter a list of digits separated by space:
99
All digits must be in the range [0,n)
Enter base:
16
Enter a list of digits separated by space:
9 9
The value for Base = 16 and digits = [9, 9] is 153
Enter base:
2
Enter a list of digits separated by space:
Digits must be integers
Enter base:
0
Enter a list of digits separated by space:
1 0 0
Base Should be > 1
Enter base:
3
Enter a list of digits separated by space:
d 5
Digits must be integers
![Template: package lab8; import java.util.Arrays; import java.util.Scanner; public class Lab8 { public static void main(String[] args) { Scanner in = new Scanner Template: package lab8; import java.util.Arrays; import java.util.Scanner; public class Lab8 { public static void main(String[] args) { Scanner in = new Scanner](/WebImages/28/template-package-lab8-import-javautilarrays-import-javautils-1078253-1761565851-0.webp)
![Template: package lab8; import java.util.Arrays; import java.util.Scanner; public class Lab8 { public static void main(String[] args) { Scanner in = new Scanner Template: package lab8; import java.util.Arrays; import java.util.Scanner; public class Lab8 { public static void main(String[] args) { Scanner in = new Scanner](/WebImages/28/template-package-lab8-import-javautilarrays-import-javautils-1078253-1761565851-1.webp)