The following assignment, requires some setup/explanation if possible. Assignmen
ID: 3702593 • Letter: T
Question
The following assignment, requires some setup/explanation if possible.
Assignment:
Modify BasedNumber class with code given below.
You will add exceptions to this class when the values of base or digits are independently illegal or mutually incompatible.
You should modify this source so that the constructor for BasedNumber throws two unchecked exceptions (which you should define).
It should throw an IllegalBaseException when the base is less than one.
It should throw an IllegalDigitException when the digits array is either empty or contains a
illegal value (either less than zero or equal or exceeding the base - remember
that digits of a base n number must be in the range [0; n)).
In addition to these exceptions, what's something else that could go wrong (not in the constructor)?
What is an appropriate exception to throw at this point?
You will also implement the driver class Assignment. This class prompts the user
for a base and a space-separated list of digits and create a new instance of
BasedNumber and display the result of the getValue method. The exceptions
that you added in the constructor would crash the program on illegal input. You
should handle these errors so that your program never crashes on invalid input,
but displays a (problem-specific) error message and re-prompts the user when
something is wrong. You should handle other errors such as illegal number
format. (Hint: use a while-loop that only terminates if there is no
error.) Note that you don't need to use finally for this assignment.
------------------------------------------------------------------------------------------------------
package assignment;
import java.util.Arrays;
import java.util.Scanner;
public class Assignment {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// TODO
// read input from standard I/O using in.nextLine()
in.close();
}
}
class BasedNumber {
private int base;
private int[] digits;
BasedNumber(int base, int[] digits) {
this.base = base;
this.digits = digits;
}
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;
}
public boolean equals(Object that) {
boolean ret = false;
if(that instanceof BasedNumber) {
BasedNumber b = (BasedNumber) that;
ret = base == b.base && Arrays.equals(digits, b.digits);
}
return ret;
}
}
----------------------------------------------------------------------------------------------------
Output:
1. Input a base:ab Illegal number format
Input a base:8
Input digits (separated by space):4 3 4
The value of [4, 3, 4] with base 8 is 284
In this test, the base is not a number { we got an error message `Illegal
number format'.
2. Input a base:2
Input digits (separated by space):ab
Illegal number format
Input a base:2
Input digits (separated by space):1 0 1
The value of [1, 0, 1] with base 2 is 5
In this test, the input digits are not a list of digits { we got an error
message `Illegal number format'.
3. Input a base:2
Input digits (separated by space):1 3 0
3 is not legal digit with base 2
Input a base:2
Input digits (separated by space):1 0 1
The value of [1, 0, 1] with base 2 is 5
In this test, the base is 2 and the input digits are 1 3 0 but the digit 3 is il-
legal. Thus we got an error message 3 is not legal digit with base 2.
4. Input a base:-2
Input digits (separated by space):1 0 1
-2 must be > 1
Input a base:2
Input digits (separated by space):1 0 1
The value of [1, 0, 1] with base 2 is 5
In this test, the base is -2. Thus we got an error message -2 must be > 1.
Explanation / Answer
You should place the break; after the code is executed successfully.
Examples:
... no errors.
... no errors.
... fails since "hasThrows" throws AnyBaseOfXYZException, and this is not handled
... no errors.
I'd thought about maybe some kind of "CompileTimeException" that extends Exception, but when you give it enough thought, it just can't work without being as ugly as RuntimeException.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.