Output Enter base 2 supply a list of digits separated by spa

Output

Enter base: 2

supply 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

supply a list of digits separated by space: 99

All digits must be in the range [0,n)

Enter base: 16

supply a list of digits separated by space: 9 9

The value for Base = 16 and digits = 9 9 is 153

Enter base: 2

supply a list of digits separated by space:

All digits must be in the range [0,n)

Enter base: 0

supply a list of digits separated by space: 1 0 0

Base Should be > 1

Enter base: 3

supply a list of digits separated by space: d 5

Digits must be integers

All digits must be in the range [0,n)

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<digits.length; i++) {

           ret = ret * base + digits[i];

       }

       return ret;

   }

   boolean equals(BasedNumber that) {

       return getValue() == that.getValue();

   }

}

Lab 8 October 29, 2016 1 Overview In this lab, you will use the BasedNumber class, where the UML diagram is given below. We\'ll be adding exceptions to this class when the values of base or digits are independently illegal or mutually incompatible. lagram is givein BasedNumber - base: nt - digils: III digits: int BasedNuber(base: in, digits: int) glBas: in +get Digit(n: int): int gelValuc): int als(other: BasedNuer): boolcanu 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? 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 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 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
Enter base:

Output Enter base: 2 supply 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 supply a list of digits
Output Enter base: 2 supply 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 supply a list of digits
Output Enter base: 2 supply 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 supply a list of digits

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site