How to Make a Simple Calculator in Java

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/

Step 1: Open Eclipse

Open up the Eclipse program

Step 2: Setting Up Your Project

  • 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

Step 3: Opening Your Project

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

Step 4: Setting Up Your Class

  • (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”

Step 5: Creating Your Scanner

  • 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

Step 6: Initializing Your Variables

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

Step 7: Asking for User Input

  • 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

Step 8: Getting Your Results to Print

  • 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

Step 9: Running Your Code

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

Step 10: Checking Your Output

  • 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

Share

    Recommendations

    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • Optics Contest

      Optics Contest
    • Plastics Contest

      Plastics Contest

    Discussions