Since I'm taking it in school this year, I decided to recreate (some of) my python CLI calculator with Java. It doesn't have any fancy features (pi, e, factorials, etc...) but it's good for basics.
Spoiler: Version 1.3.8
Code:
import java.util.Scanner;
public class Calc{
public static void calc(){
Scanner in=new Scanner(System.in);
For this update, I tried my hand at class dependencies and minimizing main method loads. It's still not perfect, but it's a huge improvement.
Main.java
Code:
//main class
public class Main{
//main method
public static void main(String[] args){
//infinite loop, so code runs indefinitely
while(true){
//prints formatted, solved equation
System.out.println(">>"+UI.output());
}
}
}
UI.java
Code:
//import scanner for user input
import java.util.Scanner;
//the UI class
public class UI{
//get user input
public static String[] getInput(){
Scanner parse=new Scanner(System.in);
System.out.print("} ");
//convert raw input to String array
String[] eq=parse.nextLine().split(" ");
//return array
return eq;
}
//parse class
public class Parse{
//solution variable
public static double solve=0.0;
//get values to be equated in double format
public static Object[] getValues(String[] in){
Object[] values={Double.parseDouble(in[0]),(String)in[1],Double.parseDouble(in[2])};
return values;
}
//determine operator and solve
public static double solve(Object[] vals){
double x=(double)vals[0];
double y=(double)vals[2];
String operator=(String)vals[1];
switch(operator){
case "+":
solve=x+y;
break;
case "-":
solve=x-y;
break;
case "*":
solve=x*y;
break;
case "/":
solve=x/y;
break;
case "%":
solve=x%y;
break;
case "**":
solve=Math.pow(x,y);
break;
case "./":
if(x==2.0){solve=Math.sqrt(y);}
if(x==3.0){solve=Math.cbrt(y);}
break;
default:
System.out.println("Error: '"+vals[1]+"' is an defined operator.");
}
return solve;
}
}
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.