Trybotics Logo

How to Make a Simple Calculator in Java

DESCRIPTION

This is a simple introduction to the Java programming language meant for people with little to no knowledge of programming.

Materials:
Computer or Laptop (with Eclipse installed)

Can install eclipse at https://www.eclipse.org/downloads/

Description:

Open up the Eclipse program

Description:

  • Click “file” in top left corner hover over “new” then click on “java project”
  • Enter “Calculator” in the “project name” text box and click “finish” on the bottom right

Description:

  • Click on “Calculator” folder on the left side
  • Click “file” in top left corner hover over “new” then click on “class”

Description:

  • (WARNING: Make sure the source folder says “Calculator/src”)
  • Enter “Calc” in the “name” text box Check the checkbox correlating to “Public static void main(String[] args)” and “generate comments” (be sure all of your checkboxes match the image) then press “Finish”

Description:

  • Delete the text on line 29 (Line numbers are located on left side of page)
  • (WARNING: you will now begin to type in your code so make sure it is formatted exactly as the step says and each line of code should be followed by a semi-colon or ;)
  • Import the scanner by typing import java.util.Scanner; on line 14 Begin your code on line 29 by typing Scanner scan = new Scanner(System.in); and press enter

Description:

  • Type on line 30 double num1; and press enter
  • Type on line 31 double num2; and press enter

Description:

  • Type on line 33 System.out.println(“Enter the first number: ”); and press enter
  • Type on line 34 num1 = scan.nextDouble(); and press enter
  • Type on line 35 System.out.println(“Enter the second number: ”); and press enter Type on line 36 num2 = scan.nextDouble(); and press enter

Description:

  • Type on line 38 System.out.println(“Addition: “ + (num1 + num2)); and press enter
  • Type on line 39 System.out.println(“Subtraction: “ + (num1 - num2)); and press enter
  • Type on line 40 System.out.println(“Multiplication: “ + (num1 * num2)); and press enter
  • Type on line 41 System.out.println(“Division: “ + (num1 / num2)); and press enter

Description:

  • Press the “run” (or green play button) that is shown in the picture below then select “OK”:

Description:

  • Look at the bottom of the screen for the output of the code it should just be one line of text that says “Enter the first number:”
  • (WARNING: if the code does not run, review the code with the picture following step 8 and be sure you do not have any errors)
  • Follow the prompt that is displayed by entering in each number and the calculator should print out the answer of your two numbers added, subtracted, divided, and multiplied like the image above


YOU MIGHT ALSO LIKE