Version | |
Download | 29 |
Stock | ∞ |
File Size | 17.14 KB |
Create Date | July 20, 2016 |
Download |
This simple Java calculator is made in Netbeans IDE, attached is the project file of Console calculator in Netbeans.
Description:
This Calculator takes two integer numbers as input and asks for the operator to calculate the sum, product, difference, and division. This mini project uses simple switch statements to calculate the sum, product, difference, and division quotient. The user will be repeated asked about the operator if the wrong operator is entered until the correct operator is not entered.
This example explains the use of methods, variables, calling methods to calculate. passing parameters to methods. Learn more about methods/functions in Java.
Simple Java Calculator Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package calculator; //import java.util.Scanner; /** * * @author Muneeb */ public class Calculator { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); int number1, number2; char operator; int failed=0; System.out.println("Enter First Number: "); number1=input.nextInt(); System.out.println("Enter Second Number: "); number2=input.nextInt(); System.out.println("Please enter the operator\"+,-,*,/: "); operator=input.next().charAt(0); do { failed=0; switch(operator) { case '+': { add(number1, number2); break; } case '-': { subtract(number1, number2); break; } case '*': { multiply(number1, number2); break; } case '/': { divide(number1, number2); break; } default: { System.out.println("wrong character entered"); failed=1; //because of wrong character entered } if(failed==1) { System.out.println("Please enter the operator\"+,-,*,/: "); operator=input.next().charAt(0); } } }while(failed==1); } public static void add(int num1, int num2) { System.out.println("Sum is: "+num1+num2); } public static void subtract(int num1, int num2) { System.out.println("Difference is: "+(num1-num2)); } public static void multiply(int num1, int num2) { System.out.println("Product is: "+(num1*num2)); } public static void divide(int num1, int num2) { System.out.println("Quotient is: "+(num1/num2)); } } |