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

Write a Java program called \"MyCalculator.java\" which can perform the operatio

ID: 3546388 • Letter: W

Question

Write a Java program called "MyCalculator.java" which can perform the operations of addition, subtraction, multiplication, and division with two integers (prompt user to input the two integers and type of operand).

MUST USE the following as a starting point:

import javax.swing.JOptionPane;

public class MyCalculator {
  
  public static void main(String[] args) {
    
    String myInput = JOptionPane.showInputDialog("Enter an expressions:");
    String[] myExpression = myInput.split(" ");
    for (int i = 0; i < myExpression.length; i++)
      System.out.println("myExpression[" + i + "]= " + myExpression[i]);
    if (myExpression.length != 3) {
      System.out.println(
                         "Exit: (Please enter: operand1 operator operand2)");
                          System.exit(0);
    }
    int myResult = 0;
    if (myExpression[1].charAt(0) != '+' && myExpression[1].charAt(0) != '-') {
      System.out.println("Wrong operator [" + myExpression[1] + "]");
      System.exit(0);
    }
    else if (myExpression[1].charAt(0) == '+')
      myResult = Integer.parseInt(myExpression[0])+
                 Integer.parseInt(myExpression[2]);
    
    else
      myResult = Integer.parseInt(myExpression[0])-
                 Integer.parseInt(myExpression[2]);
    System.out.println(myInput + " = " + myResult);
  }
}

Explanation / Answer

import

public

class MyCalculator

{

public static void main(String[] args) throws NumberFormatException

{

String myInput = JOptionPane.showInputDialog("Enter an expressions:");

String[] myExpression = myInput.split(" ");

for(int i = 0; i < myExpression.length; i++)

System.out.println("myExpression[" + i + "]= " + myExpression[i]);

if(myExpression.length != 3)

{

System.out.println(

"Exit: (Please enter: operand1 operator operand2)");

System.exit(0);

}

String str1 = myExpression[0];

for(int i = 0; i < str1.length(); i++)

{

if(!Character.isDigit(str1.charAt(i)))

{

System.out.println(" Wrong operand [" + myExpression[0] + "]");

System.exit(0);

}

}

String str2 = myExpression[2];

for(int i = 0; i < str2.length(); i++)

{

if(!Character.isDigit(str2.charAt(i)))

{

System.out.println(" Wrong operand [" + myExpression[2] + "]");

System.exit(0);

}

}

int myResult = 0;

if(myExpression[1].charAt(0) != '+' && myExpression[1].charAt(0) != '-' && myExpression[1].charAt(0) != '*' && myExpression[1].charAt(0) != '/')

{

System.out.println("Wrong operator [" + myExpression[1] + "]");

System.exit(0);

}

else if(myExpression[1].charAt(0) == '+')

myResult = Integer.parseInt(myExpression[0]) +

Integer.parseInt(myExpression[2]);

else if(myExpression[1].charAt(0) == '-')

myResult = Integer.parseInt(myExpression[0]) -

Integer.parseInt(myExpression[2]);

else if(myExpression[1].charAt(0) == '*')

myResult = Integer.parseInt(myExpression[0]) *

Integer.parseInt(myExpression[2]);

else

myResult = Integer.parseInt(myExpression[0]) /

Integer.parseInt(myExpression[2]);

System.out.println(myInput + " = " + myResult);

}

}

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