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();
   }
}

1 Overview In this lab, you will use the BasedNumber class, where the UML diagram is given >elow. We\'ll be adding exceptions to this class when the values of base or digits are independently illegal or mutually incompatible BasedNumber base: int digits: intl BasedNumber(base: int, digits: int) +getBase): int +get Digit(n: n): int +get Value): int +equals(other: BasedNumber): boolean 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,). 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? 2 Driver You will also create a driver class. This class should prompt 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 you added in the constructor should crash the program on illegal input. Make sure they do Let me see your progress at this point. Then, handle the errors so that youi program never crashes on invalid input, but displays a (problem- specific) erroi message and re-prompts the user when something is wrong. You should also

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site