answersLogoWhite

0


Best Answer

Here is a calculator program I had to make in my Computer class in bluej (free java compiler)

import java.io.*;

import java.util.*;

public class Calculator

{

public static void main(String args[])

{

System.out.println("Make your arithmetic selection from the choices below:\n");

System.out.println(" 1. Addition");

System.out.println(" 2. Subtraction");

System.out.println(" 3. Multiplication");

System.out.println(" 4. Division\n");

System.out.print(" Your choice? ");

Scanner kbReader = new Scanner(System.in);

int choice = kbReader.nextInt();

if((choice<=4) && (choice>0))

{

System.out.print("\nEnter first operand. ");

double op1 = kbReader.nextDouble();

System.out.print("\nEnter second operand.");

double op2 = kbReader.nextDouble();

System.out.println("");

switch (choice)

{

case 1: //addition

System.out.println(op1 + " plus " + op2 + " = " + (op1 + op2) );

break;

case 2: //subtraction

System.out.println(op1 + " minus " + op2 + " = " + (op1 - op2) );

break;

case 3: //multiplication

System.out.println(op1 + " times " + op2 + " = " + (op1 * op2) );

break;

case 4: //division

System.out.println(op1 + " divided by " + op2 + " = " + (op1 / op2) );

}

}

else

{

System.out.println("Please enter a 1, 2, 3, or 4.");

}

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

// Program for simple calculator

public class Calc

{

public static void main(String args[])

{

int x,w,z;

char y;

if(args[]!=3)

{

System.out.println("Wrong Syntax");

System.out.println("NOTE: Please compile like this");

System.out.println("c:\java\jdk1.6\javac Calc 25 + 3 ");

System.out.println("c:\java\jdk1.6\javac Calc 25 - 3 ");

System.out.println("c:\java\jdk1.6\javac Calc 25 x 3 ");

System.out.println("c:\java\jdk1.6\javac Calc 25 / 3 ");

System.out.println("c:\java\jdk1.6\javac Calc 25 % 3 ");

System.exit(0);

}

x= Integer.parseInt(args[0]);

y= Character.parseChar(args[1]);

z= Integer.parseInt(args[2]);

switch(y)

{

case '+' : w=x+z;

System.out.println("Addition = "+w);

break;

case '-' : w=x-z;

System.out.println("Substraction = "+w);

break;

case 'x' :

case 'X' : w=x*z;

System.out.println("Multiplication = "+w);

break;

case '/' : w=x/z;

System.out.println("Division = "+w);

break;

case '%' : w=x%z;

System.out.println("Modulus = "+w);

break;

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

You can use the built-in methods of the java Math class to do the actual calculations. If you are supposed to do this yourself, rather than using available components, you can use the infinite series expansion (Taylor series) which you can find online in many places. For example:

sin(x) = x - x3/3! + x5/5! - x7/7! + x9/9! ...

Although in theory the series is infinite, in practice you'll continue adding terms until the current term becomes less than some threshold value, depending on the desired accuracy. Or rather, the absolute value of the term.

5!, for example, means 5!, in other words, 1 x 2 x 3 x 4 x 5. Note that the angle must be in radians. If the angle is in degrees, you must convert to radians first. This applies to the built-in methods of the Math class as well.

The Wikipedia article "Taylor series" contains the infinite series for other trigonometric functions. Some of these are too complicated, in my opinion; for example, to calculate tan(x), it is more convenient to use the identity tan(x) = sin(x) / cos(x), and re-use the sin() and cos() methods instead. Also, sec(x) = 1 / cos(x), csc(x) = 1 / sin(x), and cot(x) = 1 / tan(x).

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you compile the simple calculator in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the command to compile a java program named Greetingsjava?

The command to compile a Java program is "javac", followed by the class name (file name).


How do you compile and execute Java program in which we find the highest of any five numbers?

In the same way as you would compile and execute any other Java program. Compile: use the "javac" command. Or use the built-in "compile" command in your favorite IDE. Execute: Use the "java" command. Or use the built-in "run" command in your favorite IDE.


How do you compile java programming language?

Hello Frnd, As u say u want to Compile & Run Java Prog. for This if U have JAVA CREATOR then u can compile directly otherwise........ goto Start >run type "cmd" Press "Enter"after that goto C>java>bin> type for compile the prog. "javac PROG.NAME.java" Press "Enter" if Compile SuccessFully Type "java PROG.NAME" Press "Enter" For It This Is Must u save ur Prog. in the folder "bin" in java............. Hello Frnd, As u say u want to Compile & Run Java Prog. for This if U have JAVA CREATOR then u can compile directly otherwise........ goto Start >run type "cmd" Press "Enter"after that goto C>java>bin> type for compile the prog. "javac PROG.NAME.java" Press "Enter" if Compile SuccessFully Type "java PROG.NAME" Press "Enter" For It This Is Must u save ur Prog. in the folder "bin" in java.............


How do you complie package in java?

You use javac &lt;filename&gt; to compile a class or package.


What is java ide?

Java Integrated Development Environment (IDE) provides an environment to Edit, compile and debug and generate java codeThere are several java IDE like Eclipse, WSAD, BlueJ, JCreator etc

Related questions

How do you make software from a simple java program?

You compile it.You compile it.You compile it.You compile it.


How do you run and compile a java applet program?

One can run and compile a Java applet program by agreeing to the terms and downloading it. It is possible to get a compiler online that will compile and run Java programs.


What is the command to compile a java program named Greetingsjava?

The command to compile a Java program is "javac", followed by the class name (file name).


How do you compile java programs using windows vista text pad?

How can I compile java programs using windows vista text pad?


How do you compile servlet in java using tomcat?

Tomcat is a server. It is used to deploy and run Servlets and not compile them. A Servlet is a java file and has to be compiled just like any other Java Class.


How do you compile and execute Java program in which we find the highest of any five numbers?

In the same way as you would compile and execute any other Java program. Compile: use the "javac" command. Or use the built-in "compile" command in your favorite IDE. Execute: Use the "java" command. Or use the built-in "run" command in your favorite IDE.


When you compile HelloWorldjava what do you get?

HelloWorld.classrun it with command java HelloWorld


Can be used the programs in Java compiled under Windows XP with Windows 8.1?

It should work, yes. It really doesn't matter where you compile it - if you compile under Windows XP, you don't compile FOR Windows XP; rather, you compile for the virtual machine, which is compatible in different environments.


How do you compile java programming language?

Hello Frnd, As u say u want to Compile & Run Java Prog. for This if U have JAVA CREATOR then u can compile directly otherwise........ goto Start >run type "cmd" Press "Enter"after that goto C>java>bin> type for compile the prog. "javac PROG.NAME.java" Press "Enter" if Compile SuccessFully Type "java PROG.NAME" Press "Enter" For It This Is Must u save ur Prog. in the folder "bin" in java............. Hello Frnd, As u say u want to Compile & Run Java Prog. for This if U have JAVA CREATOR then u can compile directly otherwise........ goto Start >run type "cmd" Press "Enter"after that goto C>java>bin> type for compile the prog. "javac PROG.NAME.java" Press "Enter" if Compile SuccessFully Type "java PROG.NAME" Press "Enter" For It This Is Must u save ur Prog. in the folder "bin" in java.............


Why it is required to set path and classpath?

to compile and run java program you need to set path and classpath path refers to binary used to compile and run java program i.e. "javac" for compilation and "java" for execution


What is Commands for the compiler that are needed to compile and run your program?

javac is the command that is used to compile Java source files.


How to save and compile packages in java?

by adding more documents