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

I have the start of a program and I know that it\'s not all right, but I\'m not

ID: 3653467 • Letter: I

Question

I have the start of a program and I know that it's not all right, but I'm not sure of the next step. For example the switch class is just there and it isn't doing anything yet. Any help would be awesome. I need a program that does all of the following: -Takes three inputs from the keyboard, two of them single digits (0 to 9) -Takes a char from the keyboard, representing one of five operations from the keyboard: + (addition), -(subtraction), * (multiplication), / (division), and ^ (exponentiation) -Outputs the description of the operation in plain English, as well as the numeric result For example: Five multiplied by Three is 15 import java.util.*; import java.math.*; public class EnglishLanguageCalc { public static void main(String[] args) { //Intro to the program System.out.println("English Language Calculator"); System.out.println(); System.out.println("When prompted, type in a number (1-9) and press enter when promted."); System.out.println("Next, input one of the following operations: +, -, *, /, and ^"); //Creates new scanner Scanner input = new Scanner(System.in); //Asks user to give the first number System.out.print("First number: "); int num1 = input.nextInt(); //Asks user to give the second number System.out.print("Second number: "); int num2 = input.nextInt(); System.out.print("Operation: "); char ch = input.findInLine(".").charAt(0); int output; if (ch == '+') { output = num1 + num2; System.out.println("Addition =" + output); } else if(ch == '-') { output= num1 - num2; System.out.println("Subtraction =" + output); } else if(ch == '/') { output= num1 / num2; System.out.println("Division =" + output); } else if(ch == '*') { output= num1 * num2; System.out.println("Multiplication =" + output); } else if(ch == '^') { output = (int)Math.pow(num1,num2); System.out.println("Exponent =" + output); } else { //Output if user inputs invalid numbers/opperations System.out.println("Invalid Operation"); System.out.println("End of program"); System.out.println(); } switch (output) { case 1: System.out.println("One "); case 2: System.out.println("Two "); case 3: System.out.println("Three "); case 4: System.out.println("Four "); case 5: System.out.println("Five"); case 6: System.out.println("Six "); case 7: System.out.println("Seven "); case 8: System.out.println("Eight "); case 9: System.out.println("Nine "); } } }

Explanation / Answer

import java.util.*;
import java.math.*;
public class EnglishLanguageCalc {
        public static void main(String[] args) { //Intro to the program
                System.out.println("English Language Calculator"); System.out.println();
                System.out.println("When prompted, type in a number (1-9) and press enter when promted.");
                System.out.println("Next, input one of the following operations: +, -, *, /, and ^"); //Creates new scanner
                Scanner input = new Scanner(System.in); //Asks user to give the first number
                System.out.print("First number: ");
                int num1 = Integer.parseInt(input.nextLine()); //Asks user to give the second number
                System.out.print("Second number: ");
                int num2 = Integer.parseInt(input.nextLine());
                System.out.print("Operation: ");
                char ch = input.nextLine().charAt(0);
                int output;
                // get String representation of num1 and num2
                String left = getStringNum(num1);
                String right = getStringNum(num2);
                // if num1 and num2 are not in the required range ,left or right will be null , so we have wrong input, exit the program
                if ( left == null || right == null ){
                        System.out.print("Worng input ");
                        System.exit(1);
                }
                switch(ch) {
                        case '+' :
                                output = num1 + num2;
                                System.out.println(left + " added with " + right + " gives " + output );
                                break;
                        case '-' :
                                output = num1 - num2;
                                System.out.println(left + " subtracted by " + right + " gives " + output );
                                break;
                        case '*' :
                                output = num1 * num2;
                                System.out.println(left + " multiplied with " + right + " gives " + output );
                                break;
                        case '/' :
                                output = num1 / num2;
                                System.out.println(left + " divided by " + right + " gives " + output );
                                break;
                        case '^' :
                                output = (int)Math.pow(num1,num2);
                                System.out.println(left + " to the power " + right + " gives " + output );
                                break;
                        default :
                                System.out.println("Wrong operator");
                                System.out.println("End of program"); System.out.println();
                }

        }
        // get String Representation of Number i

        public static String getStringNum(int i){
                switch (i) {
                        case 1: return "One ";
                        case 2: return "Two ";
                        case 3: return ("Three ");
                        case 4: return ("Four ");
                        case 5: return ("Five");
                        case 6: return ("Six ");
                        case 7: return ("Seven ");
                        case 8: return ("Eight ");
                        case 9: return ("Nine ");
                        default : return null;
                }
        }
}