Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program to convert the user input based on the following specificat

ID: 3671840 • Letter: W

Question

Write a java program to convert the user input based on the following specifications. The user input has the following format

[variable-length number] [a space] [A|B|H|M]

If the input is a number followed by the character "A," output the ASCII representation of (number - 1), (number), and (number +1).

If the input is a number followed by the character "B," output the decimal equivalent.

If the input is a number followed by the character "H," output the binary equivalent.

If the input is a number followed by the character "M," output the multiplication of all digits as decimal numbers.

If none of the above conditions is met, output "Goodbye."

Please observe the following requirements:

1. There should be at least two (2) "fall through" switch statements in your submission. Use as many "fall through" constructs as possible to simplify your code.

2. Please use only else and else if statements not loops.

3. Please use simple math work not the advanced java math library.

If possible I would appreciate it if you could leave comments in your code.

Explanation / Answer

package com.he.capillary.chegg1;

/**
* Uncomment the Scanner part if you want to take input from Standard input
*
*/
public class NumberValidator {

   public static void main(String[] args) {

       // Scanner sc = new Scanner(System.in);
       // String data = sc.nextLine();

       String data = "dddssdfA97B1101HABM1235";
       boolean allFalse = true;

       // Case 1
       int index1 = data.indexOf('A');
       int index2 = data.indexOf('B');
       String str = data.substring(index1 + 1, index2);
       try {
           int num = Integer.parseInt(str);
           System.out.println(((char) (num - 1)) + " " + ((char) num) + " " + ((char) (num + 1)));
           allFalse = false;
       } catch (Exception exception) {
           System.out.println("PASS");
       }

       // Case 2
       index1 = data.indexOf('B');
       index2 = data.indexOf('H');
       str = data.substring(index1 + 1, index2);
       try {
           int binary = Integer.parseInt(str);
           int decimal = 0;
           int power = 0;
           while (true) {
               if (binary == 0) {
                   break;
               } else {
                   int tmp = binary % 10;
                   decimal += tmp * Math.pow(2, power);
                   binary = binary / 10;
                   power++;
               }
           }
           System.out.println(decimal);
           allFalse = false;
       } catch (Exception exception) {
           System.out.println("PASS");
       }

       // Case 3
       index1 = data.indexOf('H');
       index2 = data.indexOf('M');
       str = data.substring(index1 + 1, index2);
       try {
           int temp = Integer.parseInt(str, 16);
           System.out.println(temp);
           allFalse = false;
       } catch (Exception exception) {
           System.out.println("PASS");
       }

       // Case 4
       index1 = data.indexOf('M');
       str = data.substring(index1 + 1, data.length());
       try {
           int mul = 1;
           for (int i = 0; i < str.length(); i++) {
               mul *= Integer.parseInt(str.charAt(i) + "");
           }
           System.out.println(mul);
           allFalse = false;
       } catch (Exception exception) {
           System.out.println("PASS");
       }

       if (allFalse == false)
           System.out.println("Goodbye.");
   }
}

Let me know if you require more details. I didn't get the fall back part. If you can explain I can submit code according to that.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote