Wednesday, November 6, 2019

Calculator Program in Java

A simple calculator program in Java programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod City, Negros Occidental I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My Facebook address is https://www.facebook.com/profile.php?id=100009212511791

My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.com

I am also a book author you can purchase my books on computer programming and information technology in the following links below.

https://www.mindshaperspublishing.com/

https://www.unlimitedbooksph.com/

Thank you very much for your help and support.


Program Listing

Calc.java

class Calc {

public static void main (String[] args) {

try {

if (args.length == 0) {

System.out.println("\nPROGRAM ERROR: You didn't enter any arguments!");

} else if  (args.length < 3) {

System.out.println("\nPROGRAM ERROR: You passed fewer than 3 arguments.");

} else if (args.length > 3) {

System.out.println("\nPROGRAM ERROR: You passed more than 3 arguments.");

} else {

double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[2]);
String op = args[1];

// Product variables
double p = a + b;
double s = a - b;
double m = a * b;
double d = a / b;

System.out.println("\n================");
System.out.println("WELCOME TO CALCACULATOR IN JAVA!");
System.out.println("================");
System.out.println("\nYou have entered: " + a + " " + op + " " + b);

switch (op) {

case "+" :
System.out.println("\n" + a + " " + op + " " + b + " = " + p);
break;

case "-" :
System.out.println("\n" + a + " " + op + " " + b + " = " + s);
break;

case "x" :
System.out.println("\n" + a + " " + op + " " + b + " = " + m);
break;

case "/" :
System.out.println("\n" + a + " " + op + " " + b + " = " + d);
break;

default :
System.out.println("INVALID OPERATOR SPECIFIED");

}
}

} catch (NumberFormatException e) {

System.out.println("\nPROGRAM ERROR");
System.out.println("\nFirst and third arguments must be numbers!");
System.out.println("\nEXAMPLE: 2 + 2");
System.out.println("\nERROR DETAILS: " + e);

} catch (Exception e) {

System.out.println("\nPROGRAM ERROR");
throw e;

}
}

}

No comments:

Post a Comment