Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Yet another calculator filter_list
Author
Message
Yet another calculator #1
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);
        
        System.out.print("} ");
        String[] eq=in.nextLine().split(" ");

        double a=Double.parseDouble(eq[0]);
        double b=Double.parseDouble(eq[2]);
        
        System.out.print(">>");
        if(eq[1].equals("+")){System.out.println(a+b);}
        else if(eq[1].equals("-")){System.out.println(a-b);}
        else if(eq[1].equals("/")){System.out.println(a/b);}
        else if(eq[1].equals("*")){System.out.println(a*b);}
        else if(eq[1].equals("%")){System.out.println(a%b);}
        else if(eq[1].equals("**")){System.out.println(Math.pow(a,b));}
    }

    public static void main(String[] args){
        for(int i=0;i==i;){
            calc();
        }
    }
}
Spoiler: Version 1.5.3
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;
    }
    
    //output solution
    public static double output(){
        //solve
        double solution=Parse.solve(Parse.getValues(getInput()));
        //return solution
        return solution;
    }
}

Parse.java
Code:
//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;
    }
}

zipped package(source code, compiled code, bluej package(because why not?), and readme):
[attachment=278]
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.

Reply





Messages In This Thread
Yet another calculator - by Inori - 09-29-2015, 04:10 PM
RE: Yet another calculator - by vent22 - 10-03-2015, 11:55 AM
RE: Yet another calculator - by Inori - 10-03-2015, 03:51 PM
RE: Yet another calculator - by vent22 - 10-03-2015, 04:04 PM
RE: Yet another calculator - by Coca. - 10-03-2015, 07:17 PM
RE: Yet another calculator - by vent22 - 10-03-2015, 07:26 PM
RE: Yet another calculator - by Nil - 10-03-2015, 09:53 PM
RE: Yet another calculator - by Binary. - 10-03-2015, 11:26 PM
RE: Yet another calculator - by Nil - 10-04-2015, 05:04 AM
RE: Yet another calculator - by Binary. - 10-04-2015, 05:18 AM



Users browsing this thread: 1 Guest(s)