CS 1401 Assignment #4, Solutions

Date Assigned: Monday, February 12 or Tuesday, February 13, 2007.

Due Date: Monday, February 19, or Tuesday, February 20, 2007, before the beginning of your lab section.

Objective: The main objective of this assignment is to practice using conditional statements.

Programming assignment: use JOptionPane to design a simple calculator. This calculator should read simple expressions describing operations on positive integers, like 15+5 or 3*41, and generate the result of the corresponding arithmetic operation (or an error message if the user accidentally types in something like 1/0). For simplicity, feel free to assume that the expressions do not have blank spaces (but please inform the user about this restriction in the input prompt).

Hints: You already know all the tricks that you need to build such a calculator:

All you have to do is add if-then statements to this set-up.


Solution:

import javax.swing.JoptionPane;
import java.io.*;
import java.util.*;

public class Calculator
{
  public static void main(String [] args) throws IOException
  {
    String inputString;
    int length;
    String beforeOperation;
    String afterOperation;
    int firstNumber;
    int secondNumber;
    int result;
    int location;
    Boolean correct = true;

    // reading the expression
    inputString = JOptionPane.showInputDialog("Please enter an expression \n" +
      "such as 3+15; your expression should not have blank spaces");
    length = inputString.length();

    // if the expression contains +, we separate the numbers, add them,
    // and return the results

    location = inputString.indexOf("+");
    if (location >= 0){
      beforeOperation = inputString.substring(0,location);
      afterOperation = inputString.substring(location+1,length);
      firstNumber = Integer.parseInt(beforeOperation);
      secondNumber = Integer.parseInt(afterOperation);
      result = firstNumber + secondNumber;
    }

    //case of subtraction
    location = inputString.indexOf("-");
    if (location >= 0){
      beforeOperation = inputString.substring(0,location);
      afterOperation = inputString.substring(location+1,length);
      firstNumber = Integer.parseInt(beforeOperation);
      secondNumber = Integer.parseInt(afterOperation);
      result = firstNumber - secondNumber;
    }

    location = inputString.indexOf("*");
    if (location >= 0){
      beforeOperation = inputString.substring(0,location);
      afterOperation = inputString.substring(location+1,length);
      firstNumber = Integer.parseInt(beforeOperation);
      secondNumber = Integer.parseInt(afterOperation);
      result = firstNumber * secondNumber;
    }

    location = inputString.indexOf("/");
    if (location >= 0){
      beforeOperation = inputString.substring(0,location);
      afterOperation = inputString.substring(location+1,length);
      firstNumber = Integer.parseInt(beforeOperation);
      secondNumber = Integer.parseInt(afterOperation);
      if (secondNumber == 0)
        {correct = false;}
      else
        {result = firstNumber / secondNumber;}
    }

    if (correct)
      {JOptionPane.showMessageDialog(null, "Result is" + result,
      "Calculator", JOptionPane.INFORMATION_MESSAGE);}
    else
      {JOptionPane.showMessageDialog(null, "Division by 0 is impossible",
      "Calculator", JOptionPane.ERROR_MESSAGE);}

    System.exit(0);
  }
}


Homework assignment: on a separate sheet of paper, solve Ex. 10 from p. 218 and Ex. 12 from p. 219.

Deliverables: as instructed by your TA.