Can someone please help me with how to write this in Java?
You will write a program to evaluate a math expression given by a user.
• Read the input from the scanner
• Expressions should match the forms below. Note that you will need spaces between the numbers and operators for your scanner methods to work best.
- “number1 operator number2”
- “operator number1”
• Declare number1 and number2 as type integer. Test using integer values.
• You must support the following binary operations:
- ^ : exponent (“ 3 ^ 2” means 3 raised to the 2nd power
- * : multiplication (“ 3 * 2” means 3 multiplied by 2)
- + : addition (“3 + 2” means 3 plus 2)
- / : division ( “3 / 2” means 3 divided by 2, * if you want a result with decimals you will need to cast one of the numbers to double)
• You must support the following unary operations:
- # : square root (“# 5” means the square root of 5)
- ~ : absolute (“~ -5” means the absolute value of -5)
• Your program should display the result of evaluating the expression in an appropriate form. For instance, a square root should be a double or float.
Hints:
• You can use nested if statements for all your logic, but you may want to consider a switch statement for your binary operations.
• When you read from the scanner, read the next string then decide what to do, is it a #? Is it a ~? …and so on. These checks may mean moving on to read the next piece of data as an integer and display the result, or moving on to do a binary operation.
Name your class program “MathExpressions”.
Sample output:
Example 1: Enter a math expression: ~ -75 The absolute value of -75 is: 75
Example 2: Enter a math expression: # 50 The square root of 50 is 7.07
Example 3: Enter a math expression: 3 * 4 The product of 3 and 4 is 12
Example 4: Enter a math expression: 5 ^ 2 5 raised to the 2 power is 25
Code for the problem is as follows:
**************************************************************************************************************************************
import java.util.Scanner;
import java.lang.Math;
public class Main
{
public static void main(String[] args) {
while(true){ //while true
Scanner sc = new Scanner(System.in); //Scanner
object
System.out.print("Enter a math
expression: ");
String s = sc.nextLine(); //get
input
String arr[] = s.split(" ");
//inputs in array
try{
if(arr[0].equals("~")||arr[0].equals("#")){ //if first string in
array is ~ or #
//find abs value
if(arr[0].equals("~"))
System.out.println("The absolute
value of "+arr[1]+" is "+Math.abs(Integer.parseInt(arr[1])));
//find sqrt
else{
double roundOff =
Math.round(Math.sqrt(Integer.parseInt(arr[1])) * 100.0) /
100.0;
System.out.println("The square root
of "+arr[1]+" is "+roundOff);
}
}
else{
int a =
Integer.parseInt(arr[0]);
int b =
Integer.parseInt(arr[2]);
//do operation based on the
operation
switch(arr[1]){
case "+":
System.out.println("The sum of
"+a+" and "+b+" is "+(a+b));
break;
case "*":
System.out.println("The product of
"+a+" and "+b+" is "+(a*b));
break;
case "/":
System.out.println(a+" divided by
"+b+" is "+((double)a/b));
break;
case "^":
System.out.println(a+" raised to
the "+b+" power is "+(Math.pow(a,b)));
break;
default: // of other than these
operators then throw a Exception
throw new Exception();
}
}
break;//break the while loop if all
things are perfect
}catch(Exception e){ //Catch the
Exception
System.out.println("You have not
entered the expression properly!! Please enter again"); //wrong
expression entered show the message and continue in while
}
}
}
}
**************************************************************************************************************************************
Screenshot of the code:

Output:








Can someone please help me with how to write this in Java? You will write a...
IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...
You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: (1) Integer constants (a series of decimal digits). (2) One alphabetic character - "x" (representing a value to be supplied later). (3) Binary operators (+, -, *, / and % (modulo)). (4) Parentheses You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract...
In java The Java language has a math class with several methods you can use for mathematical calculations. For instance, to find the square root of a number you can use the Math.sqrt method: double value = 9.0; double result = Math.sqrt(value); In the above example the variable result will hold 3.0. The same holds true for the cube root: double value = 9.0; double result = Math.cbrt(value); You can also get the base 10 logarithm of a number: double...
Need help for this assignment
Please develop 5 programs to calculate arithmetic operations using MS Visual Studio or Dev C++ 1. st program for subtraction and multiplication from 2 input variables. The example output is shown below a. Example Output Type 2 integers: The result of subtraction The result of multiplication 2, 2nd program to calculate this equation: result = a*b-c a. Example Output Type 3 integers: The result of this equation: 3. 3rd program to calculate square root a....
JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...
Read description carefully. Needs to catch exceptions and still be
able to keep the program going on past the error. Program should
allow user to keep going until he or she quits
Math tutor) Write a program that displays a menu as shown in the sample run. You can enter 1, 2. 3, or 4 for choosing an addition. subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter...
Can you please complete it in java and add comments explaining
the program so I can understand it.
Test cases:
Test case 2 input
PROBLEM: Evaluate a prefix expression. The operands in the expression are single digit whole numbers. The operators are binary addition (+), subtraction (), and multiplication(*), and a trinary operator "switcher" (a). The a operator of a, b, and c returns b when a is positive; otherwise, it returns Example 1: * + 4 53 1 simplifies...
using java to write,show me the output. please write some
common.
You CAN NOT use inbuild functions for Tree ADT operations.
using code below to finsih
public class Main
{
public static void main(String[] args) {
BinaryTree tree = new
BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(6);
tree.root.right.right = new Node(7);
tree.root.left.left.left = new Node(8);
tree.root.left.left .right= new Node(9);...
Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...
I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks. Documentation: Explain the purpose of the program as detail as possible - 8%. Develop a solution for the problem and mention algorithms to be used -12% List data structures to be used in solution. - 5%. Give a description of how to use the program and expected input/output - 5% Explain the purpose of each class you develop in the program. - 5%. Programming:...