**Due Date:** Monday, October 1, or Tuesday, October 2, 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
function calculator for computing the values of elementary functions. This
function calculator should read simple expressions like exp(-1.5), sin(0.0)
or ln(1.0), and generate the result of the corresponding arithmetic
operation (or an error message if the user wants to compute the
logarithm of 0 or of a negative number). Let us restrict ourselves
to functions exp, ln, sin, and cos. 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 function calculator:

- you know how to set up an input message box and how to read from such box -- we had that in Programming Assignment 3;
- you know how to check whether a given sequence-of-symbols string contains a given sequence such as exp -- it is in the book, and we had that in class; specifically, if the string contains the exp, the corresponding operation returns the location of this string, otherwise it returns -1;
- you know how to separate a part of the string after the given substring and before the final parenthesis;
- you know how to translate strings into real numbers -- we learned that when we learned how to read an number from the GUI;
- you know how to apply mathematical functions to real numbers;
- you know how to return the result -- and, if necessary, how to set up an error message box.

Solution: import javax.swing.JoptionPane; import java.io.*; import java.util.*; import Java.lang.Math.*; public class FunctionCalculator { public static void main(String [] args) throws IOException { String inputString; int length; String value; int location; double number; double result; Boolean correct = true; // reading the expression inputString = JOptionPane.showInputDialog("Please enter an expression \n" + "such as exp(-1.5); your expression should not have blank spaces"); length = inputString.length(); // case when the expression contains exp location = inputString.indexOf("exp"); if (location >= 0){ value = inputString.substring(location + 4, length - 1); number = Double.parseDouble(value); result = Math.exp(value); } // case of sin location = inputString.indexOf("sin"); if (location >= 0){ value = inputString.substring(location + 4, length - 1); number = Double.parseDouble(value); result = Math.sin(value); } // case of cos location = inputString.indexOf("cos"); if (location >= 0){ value = inputString.substring(location + 4, length - 1); number = Double.parseDouble(value); result = Math.cos(value); } // case of ln location = inputString.indexOf("ln"); if (location >= 0){ value = inputString.substring(location + 3, length - 1); number = Double.parseDouble(value); if (number <= 0) {correct = false;} else {result = Math.ln(value);} } if (correct) {JOptionPane.showMessageDialog(null, "Result is" + result, "FunctionCalculator", JOptionPane.INFORMATION_MESSAGE);} else {JOptionPane.showMessageDialog(null, "Logarithms are only defined for positive values", "FunctionCalculator", JOptionPane.ERROR_MESSAGE);} System.exit(0); } }

**Deliverables:** as instructed by your TA.