Project #4: Calculator
Code
/// Name: Kelsey Lieberman
/// Period 5
/// Program Name: Calculator
/// File Name: Calculator3.java
/// Date Finished: 5/27/2016
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
public class Calculator3
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) throws Exception
{
System.out.println("\nType what you would like to calculate. If you need help, type \"help\" for legal syntax and supported functions.");
double num1 = 1;
double prevAns = 0;
int firstTerm = 69;
do
{
PrintWriter fileOut = new PrintWriter("Answer.txt");
Scanner fileHelp = new Scanner(new File("help.txt"));
System.out.print("\n> ");
String str = keyboard.nextLine();
if (str.equalsIgnoreCase("clear ans"))
prevAns = 0;
else
{
fileOut.println(str);
fileOut.close();
Scanner fileIn = new Scanner(new File("Answer.txt"));
String[] myst = new String[3];
int z = 0;
while (fileIn.hasNext())
{
myst[z] = fileIn.next();
z++;
}
if (myst[1] == null || myst[2] == null)
{
if (myst[2] == null && myst[1] == null)
{
myst[2] = "nonexistant";
if (myst[1] == null && !myst[0].equalsIgnoreCase("help"))
{
System.out.println(myst[0]);
myst[1] = "nonexistant";
}
else
myst[1] = "nonexistant";
}
else
myst[2] = "nonexistant";
}
if (myst[0].equals("e"))
myst[0] = Double.toString(Math.E);
if (myst[1].equals("e"))
myst[1] = Double.toString(Math.E);
if (myst[2].equals("e"))
myst[2] = Double.toString(Math.E);
if (myst[0].equalsIgnoreCase("help"))
{
while (fileHelp.hasNextLine())
System.out.println(fileHelp.nextLine());
}
else if (myst[0] != null && myst[1] != null && myst[2] != null)
{
int numburrs = -1;
numburrs++;
if (myst[0].equalsIgnoreCase("ans") || myst[1].equalsIgnoreCase("ans") || myst[2].equalsIgnoreCase("ans"))
{
firstTerm = 69;
if (myst[0].equalsIgnoreCase("ans"))
{
myst[0] = Double.toString(prevAns);
if (prevAns == 0)
firstTerm = 420;
}
else
myst[0] = myst[0];
if (myst[1].equalsIgnoreCase("ans"))
myst[1] = Double.toString(prevAns);
else
myst[1] = myst[1];
if (myst[2].equalsIgnoreCase("ans"))
myst[2] = Double.toString(prevAns);
else
myst[2] = myst[2];
}
if (Numeric.isNumeric(myst[0]) == true && myst[1].equals("!"))
{
myst[2] = "nonexistant";
num1 = Double.valueOf(myst[0]);
double ans = num1 * (num1 - 1);
num1--;
while (num1 != 1)
{
num1--;
ans = ans * num1;
}
prevAns = ans;
System.out.println(prevAns);
}
else if (Numeric.isNumeric(myst[0]) == true && Numeric.isNumeric(myst[2]) == true)
{
num1 = Double.valueOf(myst[0]);
double num2 = Double.valueOf(myst[2]);
prevAns = operate(num1, myst[1], num2);
System.out.println(prevAns);
}
else if (Numeric.isNumeric(myst[0]) == false && Numeric.isNumeric(myst[1]) == true)
{
myst[2] = "nonexistant";
double num2 = Double.valueOf(myst[1]);
prevAns = strFuncs(myst[0], num2);
System.out.println(prevAns);
}
else
System.out.println("Please enter a task supported by this program. Type \"help\" Kcalc supported operations.");
}
}
}while ((num1 != 0 ) || (num1 == 0 && firstTerm == 420));
}
public static double operate(double num1, String op, double num2)
{
if (op.equals("+"))
return (num1 + num2);
else if (op.equals("-"))
return (num1 - num2);
else if (op.equals("*"))
return (num1 * num2);
else if (op.equals("/"))
return (num1 / num2);
else if (op.equals("%"))
return (num1 % num2);
else if (op.equals("^"))
return (Math.pow(num1,num2));
else
{
System.out.println("ERROR");
System.out.println("Please enter a task supported by this program. Type \"help\" Kcalc supported operations.");
return 999999;
}
}
public static double strFuncs(String op, double num)
{
if (op.equals("sin") || (op.equals("sin(")))
{
String dorg;
System.out.println("Degrees or Radians?");
System.out.print("> ");
dorg = keyboard.nextLine();
if (dorg.equalsIgnoreCase("degrees"))
{
num = num * ((Math.PI)/180);
}
else if (dorg.equalsIgnoreCase("radians"))
num = num;
else
System.out.println("Please type either \"degrees\" or \"radians\".");
return (Math.sin(num));
}
else if (op.equals("cos") || (op.equals("cos(")))
{
String dorg;
System.out.println("Degrees or Radians?");
System.out.print("> ");
dorg = keyboard.nextLine();
if (dorg.equalsIgnoreCase("degrees"))
{
num = ((num * (Math.PI)) / 180);
}
else if (dorg.equalsIgnoreCase("radians"))
num = num;
else
System.out.println("Please type either \"degrees\" or \"radians\".");
return (Math.cos(num));
}
else if (op.equals("tan") || (op.equals("tan(")))
{
String dorg;
System.out.println("Degrees or Radians?");
System.out.print("> ");
dorg = keyboard.nextLine();
if (dorg.equalsIgnoreCase("degrees"))
{
num = num * ((Math.PI) / 180);
}
else if (dorg.equalsIgnoreCase("radians"))
num = num;
else
System.out.println("Please type either \"degrees\" or \"radians\".");
return (Math.tan(num));
}
else if (op.equals("-"))
return (-1 * num);
else if (op.equals("sqrt") || (op.equals("sqrt(")))
return (Math.sqrt(num));
else if (op.equals("ln"))
return (Math.log(num));
else if (op.equals("log"))
{
System.out.println("What would you like the base of your logarithm to be?");
System.out.print("> ");
double base = keyboard.nextDouble();
String temp = keyboard.nextLine();
return ( (Math.log(num)) / (Math.log(base)) );
}
else if (op.equalsIgnoreCase("round"))
{
return (Math.round(num));
}
else
{
System.out.println("ERROR");
System.out.println("Please enter a task supported by this program. Type \"help\" Kcalc supported operations.");
return 99999;
}
}
}
Picture of the output