hpvs15c
2012-09-12 23:18:12 UTC
import javax.swing.JOptionPane;
public class MyCalculatorTwo
{
public static void main(String [ ] args)
{
double first;
double second;
double power1;
double sum=0;
double difference=0;
String oneInputString;
String twoInputString;
String type;
String power=null;
String calctype = JOptionPane.showInputDialog("Will you be calculating one number or two numbers? (Please enter either \"one\" or \"two\")");
// Executes an operation with one number.
if(calctype.equals("one"))
{
oneInputString = JOptionPane.showInputDialog("Enter the first number.");
first = Double.parseDouble(oneInputString);
type = JOptionPane.showInputDialog("What type of operation would you like to execute? (Enter \"square root,\" \"cubic root,\" \"sine,\" \"power,\" or \"absolute value.\" ");
if(type.equals("square root"))
JOptionPane.showMessageDialog(null,"Answer: " + Math.sqrt(first));
else if (type.equals("cubic root"))
JOptionPane.showMessageDialog(null,"Answer: " + Math.cbrt(first));
else if (type.equals("sine"))
JOptionPane.showMessageDialog(null,"Answer: " + Math.sin(first));
else if (type.equals("power"))
{
power = JOptionPane.showInputDialog("To what power would you like to raise " +first+" to?");
power1 = Double.parseDouble(power);
JOptionPane.showMessageDialog(null,"Answer: " + Math.pow(first, power1));
}
else if (type.equals("absolute value"))
JOptionPane.showMessageDialog(null,"Answer: " + Math.abs(first));
else
JOptionPane.showMessageDialog(null,"Invalid entry. Please enter either \"square root,\" \"cubic root,\" \"sine,\" \"power,\" or \"absolute value.\"");
}
// Executes an operation with two numbers.
else if(calctype.equals("two"))
{
oneInputString = JOptionPane.showInputDialog("Enter the first number.");
first = Double.parseDouble(oneInputString);
twoInputString = JOptionPane.showInputDialog("Enter the second number.");
second = Double.parseDouble(twoInputString);
type = JOptionPane.showInputDialog("What type of operation would you like to execute? Enter either \"addition,\" \"subtraction,\" \"multiplication,\" or \"subtraction.\"");
if (type.equals("addition"))
sum = first + second;
JOptionPane.showMessageDialog(null, "Answer: " + sum);
else if (type.equals("subtraction"))
difference = first - second;
JOptionPane.showMessageDialog(null, "Answer: " + difference);
}
else
{
System.out.println("Invalid entry. Please enter either \"one\" or \"two.\"");
}
}
}