Sinisterly
Yet another calculator - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE)
+--- Thread: Yet another calculator (/Thread-Yet-another-calculator)



Yet another calculator - Inori - 09-29-2015

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]



RE: Yet another calculator - vent22 - 10-03-2015

I've always been told the first step to java is to reconsider as it's a terrible language, would you recommend it?


RE: Yet another calculator - Inori - 10-03-2015

(10-03-2015, 11:55 AM)overseer Wrote: I've always been told the first step to java is to reconsider as it's a terrible language, would you recommend it?

Java sucks. If you have other options, take them. I'm only learning it for school.


RE: Yet another calculator - vent22 - 10-03-2015

(10-03-2015, 03:51 PM)Gaige Wrote: Java sucks. If you have other options, take them. I'm only learning it for school.

I like javascript, but i understand there is a massive difference between js and java however i love webdev.


RE: Yet another calculator - Coca. - 10-03-2015

(10-03-2015, 11:55 AM)overseer Wrote: I've always been told the first step to java is to reconsider as it's a terrible language, would you recommend it?

Java does have a lot of downsides, yes, but I believe that there is one good thing about it: the fact that it is truly multi-platform. In other words, if you're too lazy to create three releases of your program (one for windows, one for mac, one for linux, etc.), but need/want it to support all platforms, Java is an excellent choice. Otherwise, if you're not lazy and want to execute machine code directly instead of indirectly by bytecode (which, even if slightly, will always be slower), and want to create multiple releases for multiple operating systems of your program, use a native programming language such as C or C++. However, Java code can be faster than native code depending on the optimizations and quality to it. Since modern optimizers can optimize better than humans can directly, a native language will most likely always be faster than executing p-code (Java).

(10-03-2015, 04:04 PM)overseer Wrote: I like javascript, but i understand there is a massive difference between js and java however i love webdev.

Java is also useful for the web. I believe that Java should only be used for the web if anything.

(10-03-2015, 03:51 PM)Gaige Wrote: Java sucks. If you have other options, take them. I'm only learning it for school.

You shouldn't just say that a programming language sucks. Java can be better in some terms than other languages, so Java could be useful for some programmers, and be worth it.


RE: Yet another calculator - vent22 - 10-03-2015

(10-03-2015, 07:17 PM)Coca. Wrote: Java does have a lot of downsides, yes, but I believe that there is one good thing about it: the fact that it is truly multi-platform. In other words, if you're too lazy to create three releases of your program (one for windows, one for mac, one for linux, etc.), but need/want it to support all platforms, Java is an excellent choice. Otherwise, if you're not lazy and want to execute machine code directly instead of indirectly by bytecode (which, even if slightly, will always be slower), and want to create multiple releases for multiple operating systems of your program, use a native programming language such as C or C++. However, Java code can be faster than native code depending on the optimizations and quality to it. Since modern optimizers can optimize better than humans can directly, a native language will most likely always be faster than executing p-code (Java).


Java is also useful for the web. I believe that Java should only be used for the web if anything.


You shouldn't just say that a programming language sucks. Java can be better in some terms than other languages, so Java could be useful for some programmers, and be worth it.

Found this very useful, thanks alot man!


RE: Yet another calculator - Nil - 10-03-2015

While Java sure has some downside, I wouldn't tell anyone to ever reconsider. It's one of the most popular, if not the most popular, programming language in America at the moment. Looking at github statistics you can see the popularity and the actual continued growth of the language even now. I believe most computer science, computer engineering, CIS, majors teach Java in their introductory programming language classes. This ensures that new batches of programmers are all gaining a basic foundation of Java. You could say that it's saturating the language and you'd rather learn something else, but it's also increasing the longevity of the language and ensuring it's relevance. Java is extremely popular in majority of tech companies so for those looking for a job, it'll be useful to understand the environment you're in and what you'll most likely have to be working with anyway.

Doesn't mean you have to like it or use it all the time, but just understand you'll come across it quite often so a little knowledge won't hurt you is all.


RE: Yet another calculator - Binary. - 10-03-2015

(10-03-2015, 09:53 PM)God Wrote: While Java sure has some downside, I wouldn't tell anyone to ever reconsider. It's one of the most popular, if not the most popular, programming language in America at the moment. Looking at github statistics you can see the popularity and the actual continued growth of the language even now. I believe most computer science, computer engineering, CIS, majors teach Java in their introductory programming language classes. This ensures that new batches of programmers are all gaining a basic foundation of Java. You could say that it's saturating the language and you'd rather learn something else, but it's also increasing the longevity of the language and ensuring it's relevance. Java is extremely popular in majority of tech companies so for those looking for a job, it'll be useful to understand the environment you're in and what you'll most likely have to be working with anyway.

Doesn't mean you have to like it or use it all the time, but just understand you'll come across it quite often so a little knowledge won't hurt you is all.

Java is meant to be fully platform independent. If your program in Java isn't platform independent, you shouldn't be using Java. Say that you're writing a program for Windows only. Using a native language or low level language would be better in a big variety of ways. However, if you want to create one program to run on any platform (write once, run anywhere), Java would indeed be an excellent, yet suitable choice. The programming language that you choose to code in depends on what the program is, or how it will be.


RE: Yet another calculator - Nil - 10-04-2015

(10-03-2015, 11:26 PM)Binary. Wrote: Java is meant to be fully platform independent. If your program in Java isn't platform independent, you shouldn't be using Java. Say that you're writing a program for Windows only. Using a native language or low level language would be better in a big variety of ways. However, if you want to create one program to run on any platform (write once, run anywhere), Java would indeed be an excellent, yet suitable choice. The programming language that you choose to code in depends on what the program is, or how it will be.

I don't disagree with that. Just pointing out that there is already an absurd amount of devices coded with Java that it's helpful for one to understand it. Won't bother to find a statistic right now but it's pretty commonplace to be working with or around Java at the majority of tech companies you'll be looking for a job at. Of course other languages can get the job done a lot smoother in many instances but you shouldn't overlook the effect of Java's popularity.


RE: Yet another calculator - Binary. - 10-04-2015

(10-04-2015, 05:04 AM)God Wrote: I don't disagree with that. Just pointing out that there is already an absurd amount of devices coded with Java that it's helpful for one to understand it. Won't bother to find a statistic right now but it's pretty commonplace to be working with or around Java at the majority of tech companies you'll be looking for a job at. Of course other languages can get the job done a lot smoother in many instances but you shouldn't overlook the effect of Java's popularity.

There are enormous amounts of computer-related job opportunities not requiring anything related to Java. I wouldn't worry about jobs, but it is true that Java is a very popular programming language nowadays.