Menu

[Solved]Task Write Calculator Program Asks User Expression Operation Two Rational Number Operands Q37263142

I need help writing this code in java.

Task: Write a calculator program that asks the user for an expression. Should be a, or / operation with two rational number o

LISTING 9.5 Calculator.java 1 public class Calculator 2* Main method 3 public static void main(String[] args) Check number of

UPDATE In place of nes 4-15 ab Scanner scan new Scanner(System.in); System.out printf Enter an arithmetic operation (Ex: 7*2)

run: Enter a rational arithmetic operation (Ex: 7/4 2/5): 7/8 2/5 7/8 2/5 -14/40 BUILD SUCCESSFUL (total time: 13 seconds) ru

Task: Write a calculator program that asks the user for an expression. Should be a, or / operation with two rational number operands, e.g. 3/4 *4/5. Then, your program should output the expression and the result, e.g. 3/4* 4/5- 1220 .Your program should accept integers and rational numbers (fractions). If the user enters an integer, I suggest converting it to a rational number anyway in your program to make things more convenient (e.g, if the user enters 5, convert it to 5/1 in your program). Or, you can leave them as integers The output should simply be the original expression equaling the result, as the example above. The result does not have to be reduced, but it can be. If the operation is + or-and the denominators are the same for each operand, the result should have the same denominator (3/S-1/8-2/8). For implementation, choose one Option 1: Start with the Calculator class code in the screenshot below (note the update), and then modify it for rational numbers and the output matches the requirements. When completed, zip the project like normal and submit to Canvas by the due date. (See below for Calculator code and Sample Output) LISTING 9.5 Calculator.java 1 public class Calculator 2* Main method 3 public static void main(String[] args) Check number of strings passed if (args.1ength !-1) 6 System.out.println “Usage: java Calculator “operandl operator operand2″” System.exit(1); 10 // The result of the operation int result-0 12 13 14 15 16 17 18 19 20 // The result of the operation String[] tokens args [0].split(” // Determine the operator switch (tokens [1].charAt (0) case +’: result Integer.parseInt (tokens [0]) + case -‘: result Integer.parseInt (tokens [0] – case:result Integer.parseInt(tokens [0]) caseゾ: result-Integer.parselnt (tokens [0]) / Integer.parseInt(tokens [2]; Integer.parseInt(tokens [2]; Integer.parseInt(tokens [2] Integer.parseInt (tokens [2]) break; 23 24 25 26 27 28 29 30 31 32 break; // Display result System.out.println(tokens [0]tokens [l]’ +tokens [2]”result); 34 35 36 UPDATE In place of nes 4-15 ab Scanner scan new Scanner(System.in); System.out printf Enter an arithmetic operation (Ex: 7*2): ) String exp = scanned ineQ int result 0 The result of the operation String[] tokens = exp·split(” “); Note: Don’t forget to import Scanner Sample Output run: Enter a rational arithmetic operation (Ex: 7/4 2/5): 7/8 2/5 7/8 2/5 -14/40 BUILD SUCCESSFUL (total time: 13 seconds) run: Enter a rational arithmetic operation (Ex: 7/42/5): 3/4+5/6 BUILD SUCCESSFUL (total time: 15 seconds) Show transcribed image text Task: Write a calculator program that asks the user for an expression. Should be a, or / operation with two rational number operands, e.g. 3/4 *4/5. Then, your program should output the expression and the result, e.g. 3/4* 4/5- 1220 .Your program should accept integers and rational numbers (fractions). If the user enters an integer, I suggest converting it to a rational number anyway in your program to make things more convenient (e.g, if the user enters 5, convert it to 5/1 in your program). Or, you can leave them as integers The output should simply be the original expression equaling the result, as the example above. The result does not have to be reduced, but it can be. If the operation is + or-and the denominators are the same for each operand, the result should have the same denominator (3/S-1/8-2/8). For implementation, choose one Option 1: Start with the Calculator class code in the screenshot below (note the update), and then modify it for rational numbers and the output matches the requirements. When completed, zip the project like normal and submit to Canvas by the due date. (See below for Calculator code and Sample Output)
LISTING 9.5 Calculator.java 1 public class Calculator 2* Main method 3 public static void main(String[] args) Check number of strings passed if (args.1ength !-1) 6 System.out.println “Usage: java Calculator “operandl operator operand2″” System.exit(1); 10 // The result of the operation int result-0 12 13 14 15 16 17 18 19 20 // The result of the operation String[] tokens args [0].split(” // Determine the operator switch (tokens [1].charAt (0) case +’: result Integer.parseInt (tokens [0]) + case -‘: result Integer.parseInt (tokens [0] – case:result Integer.parseInt(tokens [0]) caseゾ: result-Integer.parselnt (tokens [0]) / Integer.parseInt(tokens [2]; Integer.parseInt(tokens [2]; Integer.parseInt(tokens [2] Integer.parseInt (tokens [2]) break; 23 24 25 26 27 28 29 30 31 32 break; // Display result System.out.println(tokens [0]tokens [l]’ +tokens [2]”result); 34 35 36
UPDATE In place of nes 4-15 ab Scanner scan new Scanner(System.in); System.out printf Enter an arithmetic operation (Ex: 7*2): ) String exp = scanned ineQ int result 0 The result of the operation String[] tokens = exp·split(” “); Note: Don’t forget to import Scanner Sample Output
run: Enter a rational arithmetic operation (Ex: 7/4 2/5): 7/8 2/5 7/8 2/5 -14/40 BUILD SUCCESSFUL (total time: 13 seconds) run: Enter a rational arithmetic operation (Ex: 7/42/5): 3/4+5/6 BUILD SUCCESSFUL (total time: 15 seconds)

Expert Answer


Answer to Task: Write a calculator program that asks the user for an expression. Should be a, or / operation with two rational num… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *