how to make a calculator in java

Solutions on MaxInterview for how to make a calculator in java by the best coders in the world

showing results for - "how to make a calculator in java"
Joaquín
31 Sep 2016
1import java.util.Scanner;
2public class Calculator {
3   public static void main(String[] args) {
4      double num1;
5      double num2;
6      double ans;
7      char op;
8      Scanner reader = new Scanner(System.in);
9      System.out.print("Enter two numbers: ");
10      num1 = reader.nextDouble();
11      num2 = reader.nextDouble();
12      System.out.print("\nEnter an operator (+, -, *, /): ");
13      op = reader.next().charAt(0);
14      switch(op) {
15         case '+': ans = num1 + num2;
16            break;
17         case '-': ans = num1 - num2;
18            break;
19         case '*': ans = num1 * num2;
20            break;
21         case '/': ans = num1 / num2;
22            break;
23      default: System.out.printf("Error! Enter correct operator");
24         return;
25      }
26      System.out.print("\nThe result is given as follows:\n");
27      System.out.printf(num1 + " " + op + " " + num2 + " = " + ans);
28   }
29}
Gianluca
19 Aug 2017
1// this was all coded by me, to use it get Eclipse, or IntelliJ and make a new
2// Java Project
3
4package javaCode;
5
6import java.util.Scanner;
7
8public class testCode {
9
10	public static void main(String[] args) {
11		
12		Scanner scan = new Scanner(System.in);
13		
14		System.out.println("Would you like to Add, Subtract, Divide, or Multiply?");
15		
16		String mathType = scan.next();
17		
18		if (mathType.equalsIgnoreCase("add")) {
19			System.out.println("Whats the first number you want to add?");
20			
21			int firstNumber = scan.nextInt();
22			
23			System.out.println(firstNumber + " + ");
24			System.out.println("Enter your second number");
25			
26			int secondNumber = scan.nextInt();
27			
28			System.out.println(firstNumber + " + " + secondNumber + " =");
29			System.out.println(firstNumber + secondNumber);
30			
31		} else if (mathType.equalsIgnoreCase("subtract")) {
32			System.out.println("Whats the first number you want to subtract?");
33			
34			int firstNumber = scan.nextInt();
35			
36			System.out.println(firstNumber + " - ");
37			System.out.println("Enter your second number");
38			
39			int secondNumber = scan.nextInt();
40			
41			System.out.println(firstNumber + " - " + secondNumber + " =");
42			System.out.println(firstNumber - secondNumber);
43		} else if (mathType.equalsIgnoreCase("divide")) {
44			System.out.println("Whats the first number you want to divide?");
45			
46			int firstNumber = scan.nextInt();
47			
48			System.out.println(firstNumber + " % ");
49			System.out.println("Enter your second number");
50			
51			int secondNumber = scan.nextInt();
52			
53			System.out.println(firstNumber + " % " + secondNumber + " =");
54			System.out.println(firstNumber / secondNumber);
55		} else if (mathType.equalsIgnoreCase("multiply")){
56			System.out.println("Whats the first number you want to multiply?");
57			
58			int firstNumber = scan.nextInt();
59			
60			System.out.println(firstNumber + " x ");
61			System.out.println("Enter your second number");
62			
63			int secondNumber = scan.nextInt();
64			
65			System.out.println(firstNumber + " x " + secondNumber + " =");
66			System.out.println(firstNumber * secondNumber);
67		}
68		
69	}
70
71}
Valeria
31 Jun 2019
1Save this file as MyCalculator.java 
2to compile it use 
3    javac MyCalculator.java 
4to use the calcuator do this 
5    java MyCalculator 
6 
7**********************************************/  
8import java.awt.*;  
9import java.awt.event.*;  
10/*********************************************/  
11  
12public class MyCalculator extends Frame  
13{  
14  
15public boolean setClear=true;  
16double number, memValue;  
17char op;  
18  
19String digitButtonText[] = {"7""8""9""4""5""6""1""2""3""0""+/-""." };  
20String operatorButtonText[] = {"/""sqrt""*""%""-""1/X""+""=" };  
21String memoryButtonText[] = {"MC""MR""MS""M+" };  
22String specialButtonText[] = {"Backspc""C""CE" };  
23  
24MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length];  
25MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length];  
26MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length];  
27MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length];  
28  
29Label displayLabel=new Label("0",Label.RIGHT);  
30Label memLabel=new Label(" ",Label.RIGHT);  
31  
32final int FRAME_WIDTH=325,FRAME_HEIGHT=325;  
33final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10;  
34final int TOPX=30, TOPY=50;  
35///////////////////////////  
36MyCalculator(String frameText)//constructor  
37{  
38super(frameText);  
39  
40int tempX=TOPX, y=TOPY;  
41displayLabel.setBounds(tempX,y,240,HEIGHT);  
42displayLabel.setBackground(Color.BLUE);  
43displayLabel.setForeground(Color.WHITE);  
44add(displayLabel);  
45  
46memLabel.setBounds(TOPX,  TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT);  
47add(memLabel);  
48  
49// set Co-ordinates for Memory Buttons  
50tempX=TOPX;   
51y=TOPY+2*(HEIGHT+V_SPACE);  
52for(int i=0; i<memoryButton.length; i++)  
53{  
54memoryButton[i]=new MyMemoryButton(tempX,y,WIDTH,HEIGHT,memoryButtonText[i], this);  
55memoryButton[i].setForeground(Color.RED);  
56y+=HEIGHT+V_SPACE;  
57}  
58  
59//set Co-ordinates for Special Buttons  
60tempX=TOPX+1*(WIDTH+H_SPACE); y=TOPY+1*(HEIGHT+V_SPACE);  
61for(int i=0;i<specialButton.length;i++)  
62{  
63specialButton[i]=new MySpecialButton(tempX,y,WIDTH*2,HEIGHT,specialButtonText[i], this);  
64specialButton[i].setForeground(Color.RED);  
65tempX=tempX+2*WIDTH+H_SPACE;  
66}  
67  
68//set Co-ordinates for Digit Buttons  
69int digitX=TOPX+WIDTH+H_SPACE;  
70int digitY=TOPY+2*(HEIGHT+V_SPACE);  
71tempX=digitX;  y=digitY;  
72for(int i=0;i<digitButton.length;i++)  
73{  
74digitButton[i]=new MyDigitButton(tempX,y,WIDTH,HEIGHT,digitButtonText[i], this);  
75digitButton[i].setForeground(Color.BLUE);  
76tempX+=WIDTH+H_SPACE;  
77if((i+1)%3==0){tempX=digitX; y+=HEIGHT+V_SPACE;}  
78}  
79  
80//set Co-ordinates for Operator Buttons  
81int opsX=digitX+2*(WIDTH+H_SPACE)+H_SPACE;  
82int opsY=digitY;  
83tempX=opsX;  y=opsY;  
84for(int i=0;i<operatorButton.length;i++)  
85{  
86tempX+=WIDTH+H_SPACE;  
87operatorButton[i]=new MyOperatorButton(tempX,y,WIDTH,HEIGHT,operatorButtonText[i], this);  
88operatorButton[i].setForeground(Color.RED);  
89if((i+1)%2==0){tempX=opsX; y+=HEIGHT+V_SPACE;}  
90}  
91  
92addWindowListener(new WindowAdapter()  
93{  
94public void windowClosing(WindowEvent ev)  
95{System.exit(0);}  
96});  
97  
98setLayout(null);  
99setSize(FRAME_WIDTH,FRAME_HEIGHT);  
100setVisible(true);  
101}  
102//////////////////////////////////  
103static String getFormattedText(double temp)  
104{  
105String resText=""+temp;  
106if(resText.lastIndexOf(".0")>0)  
107    resText=resText.substring(0,resText.length()-2);  
108return resText;  
Theo
18 Aug 2016
1package main;
2
3import java.util.Scanner;
4
5public class calculator {
6
7    public static void greetings(){
8        System.out.println("Hi welcome to the calculator made by Pepa pig made in java! :D");
9    }
10
11    public static void instruction(){
12        System.out.println("Hi welcome to the calculator made by Pepa pig made in java! :D");
13    }
14
15    public static void calculator(){
16        Scanner keyboardInput = new Scanner(System.in);
17        String calculator;
18
19        do {
20            System.out.print("Enter your first digit: ");
21            double num1 = keyboardInput.nextDouble();
22
23            System.out.print("Enter your operator: ");
24            String op = keyboardInput.next();
25
26            System.out.print("Enter your second digit: ");
27            double num2 = keyboardInput.nextDouble();
28
29            System.out.print("= ");
30
31            if (op.equals("+")){
32                System.out.print(num1 + num2);
33                System.out.println("\nYou were adding");
34                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
35            }else if (op.equals("-")){
36                System.out.print(num1 - num2);
37                System.out.println("\nYou were subtracting");
38                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
39            }else if (op.equals("*")){
40                System.out.print(num1 * num2);
41                System.out.println("\nYou were multiplying");
42                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
43            }else if (op.equals("/")){
44                System.out.print(num1 / num2);
45                System.out.println("\nYou were dividing");
46                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
47            }else if (op.equals("%")){
48                System.out.print(num1 % num2);
49                System.out.println("\nYou were remanding");
50                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
51            }else{
52                System.out.println("Invalid operator");
53            }
54            System.out.print("Would you like to restart the calculator? (y or n): ");
55            calculator = keyboardInput.next();
56        }
57        while (calculator.equals("y"));
58
59        if (calculator.equals("n")){
60            System.out.println("Leave then kiddo ;-;");
61        }
62    }
63
64
65    public static void main(String[] args) {
66        greetings();
67        instruction();
68        calculator();
69    }
70}
Louisa
28 Oct 2017
1import java.util.Scanner;
2
3class Main {
4  public static void main(String[] args) {
5
6    char operator;
7    Double number1, number2, result;
8
9    // create an object of Scanner class
10    Scanner input = new Scanner(System.in);
11
12    // ask users to enter operator
13    System.out.println("Choose an operator: +, -, *, or /");
14    operator = input.next().charAt(0);
15
16    // ask users to enter numbers
17    System.out.println("Enter first number");
18    number1 = input.nextDouble();
19
20    System.out.println("Enter second number");
21    number2 = input.nextDouble();
22
23    switch (operator) {
24
25      // performs addition between numbers
26      case '+':
27        result = number1 + number2;
28        System.out.println(number1 + " + " + number2 + " = " + result);
29        break;
30
31      // performs subtraction between numbers
32      case '-':
33        result = number1 - number2;
34        System.out.println(number1 + " - " + number2 + " = " + result);
35        break;
36
37      // performs multiplication between numbers
38      case '*':
39        result = number1 * number2;
40        System.out.println(number1 + " * " + number2 + " = " + result);
41        break;
42
43      // performs division between numbers
44      case '/':
45        result = number1 / number2;
46        System.out.println(number1 + " / " + number2 + " = " + result);
47        break;
48
49      default:
50        System.out.println("Invalid operator!");
51        break;
52    }
53
54    input.close();
55  }
56}
Hannes
07 Jul 2018
1import java.util.Scanner;
2
3
4public class Calculator {
5
6
7    public static void main(String[] args) {
8
9        Scanner kb = new Scanner(System.in);
10
11        System.out.println("Simple Calculator");
12
13        System.out.println("\nHere are your options:");
14        System.out.println("\n1. Addition");
15        System.out.println("2. Subtraction");
16        System.out.println("3. Division");
17        System.out.println("4. Multiplication");
18
19        System.out.print("\nWhat would you like to do?: ");
20        int choice = kb.nextInt();
21        System.out.println();
22
23
24        if (choice == 1){
25            addition();
26        }
27        else if (choice == 2){
28            subtraction();
29        }
30        else if (choice == 3){
31            division();
32        }
33        else if (choice == 4){
34            multiplication();
35        }
36
37        System.out.println();
38        kb.close();
39    }
40
41    public static void addition(){
42
43        int nOne, nTwo;
44        Scanner kb = new Scanner(System.in);
45
46        System.out.println("Addition");
47
48        System.out.print("\nFirst Number: ");
49        nOne = kb.nextInt();
50
51        System.out.print("\nSecond Number: ");
52        nTwo = kb.nextInt();
53
54        kb.close();
55        System.out.println("\nSum: " + nOne + " + " + nTwo + " = " + (nOne + nTwo));
56    }
57
58    public static void subtraction(){
59        int nOne, nTwo;
60        Scanner kb = new Scanner(System.in);
61
62        System.out.println("Subtraction");
63
64        System.out.print("\nFirst Number: ");
65        nOne = kb.nextInt();
66
67        System.out.print("\nSecond Number: ");
68        nTwo = kb.nextInt();
69
70        kb.close();
71        System.out.println("\nSum: " + nOne + " - " + nTwo + " = " + (nOne - nTwo));
72    }
73
74    public static void division(){
75        int nOne, nTwo;
76        Scanner kb = new Scanner(System.in);
77
78        System.out.println("Division");
79
80        System.out.print("\nFirst Number: ");
81        nOne = kb.nextInt();
82
83        System.out.print("\nSecond Number: ");
84        nTwo = kb.nextInt();
85
86        kb.close();
87        System.out.println("\nSum: " + nOne + " / " + nTwo + " = " + (nOne / nTwo));
88    }
89
90    public static void multiplication(){
91        int nOne, nTwo;
92        Scanner kb = new Scanner(System.in);
93
94        System.out.println("Multiplication");
95
96        System.out.print("\nFirst Number: ");
97        nOne = kb.nextInt();
98
99        System.out.print("\nSecond Number: ");
100        nTwo = kb.nextInt();
101
102        kb.close();
103        System.out.println("\nSum: " + nOne + " x " + nTwo + " = " + (nOne * nTwo));
104    }
105}
queries leading to this page
java operator how to make an calculatorin java calculator programhow to program a calculator with javacalculator using switch case in javajava make caculatorcreate calculator javawrite a java program to perform basic calculator operations how to program a calculator in javamaking a simple calculator in javacalculator in javajava code to create a simple calculatorcalculator in javaswingcalculator app in javacalculator kjavacalculator example in javajava calculatrowindows calculator frontend development in javajava program where the user chooses his operation without case full calculator program on javacalculaor javabig o calculator javahow to code a calculator in javasimple caulcotr with javacalculator class in javacode for calculator in java calculator java program in javajava program for calculatorcalculator in java how to program a simple calculator in javacalculator using scanner in javajava how to make calculatorcalculator java programcreate a class calculator in javamake calculator using oops in javacreating a calculator with java how to write calculator program in javacalculator simple program in javabuilding aa basic calculator javacalcultor using javajava code for simple calculatorhow to make a caculator in javahow to make in calculator x 5ey javacreate simple calculator java classescalculator javacode java calculatricehow to build calculator in javasimple caculator javacalculator on javaprogram a calculator in javajava calculator apphow to make a simple calculator in java using methodscalculate with javahow to covert 0721501212 to 27721501212 in javacalculator using scanner method in javacode calculator javacreate a calculator javajava project calculatormake calculator in javamake calculator using javajava calculator operatorscalculator java codesimple calculator program in javacalculator in javsmaking calculator in javacalculator using java classcreating a calculater in javajava program to implement basic calculator operationsjava code to create a calculatorcreate a simple calculator javajava calcuator codejava code for calculatorjava calc functonjava bmi calculatorhow to make simple cacultor in javahow to create calculator in javajava calculatprjava calculation basic java calculator programhow to make ajava statistics calculatorcode to creat a simple calculator using javacalculator 2f in java 22calculator java 22calculator java source codejava program to make a calculatorcalculator code javabuilding a calculator using javaprogram in java to create a calculator with methods in javahow calculator works in jacreating calculator in javahow to create a functioing calculator in javacalculator for java mathcalculator program with exit code javacalculator java classesmaking calculator with javaimplement calculator in javabasic calculator javahow to make calculator in jaajava calculatecalculatorsys javahow to make calculator in javain java calculator program needed a classcalculator in javasimple java calculatorjava calculator app codesimple calculator in java without switch casejava make a calculatorjava method calculatorjava program to make a simple calculatorsimple calculator in javabasic java calculator using scanner 25 calculator in javacalc javaprogram for making calculator in javajava how to use multiple calculator operations in one screenwrite a program in java to exhibit the functionality of a calculator java calculator simplesteps in building a calculator in javacreate calculator in javajava calassessimple calculator operators program with same method name in javajava calculator onlinecode in java for calculatorprogram to make a simple calculator in javacalculator function in javajava calculator filesimple calculation in javajava program calculations examplesimple java calculator in javajava calculator program using methods calculator javahow to make a calculator using javajava code for calculationjava 25 calculatoralgorithm for building multiple arithmetic operation calculator with java 21 java calculator program needed a classjava codes for calculatorcalculator in java source codecreate a basic calculator jacalogic of calculator program of many numbers in javasimple calc in javajava program that calculate create a simple calculator in javacalbulator program in javacreate a simple calculator using javacalcultor example in javacalculator logic in javacalculator app javacreate a calculator in javaexamples of calculator javawriting a calculator in javacreate calculator app in javajava class method calculatorcalculator in java entter any numberhow to make a basic calculator in javacalculator java codehow to write a calculator in javahow to covert 0721501212 to 2b27721501212 in javahow to calculate in java programingcalculator code for javajava code for calculator in notepadcode for calculator javahow to make a java calculatorjava code calculatorhow to make a calculator javajava solvermaking a calculator in javacode for java calcullatorhow to amke a calcualtor in javasimple caclater in javahow to make a simple calculator in javajava calculator code examplecalculatro javamake a simple calculator using a string in javacalculator program in java using switch casecalculator app java point functioncalculator using javacalc 28 javajava function calculator codehow to make a calculator in javaprogram to create calculator in javajava create calculatorhow to put 25 in a calculator using javacalculator program in javabasic java calculatorcalculator code in javabasic calculator program in javacalculator program with calulate method in javawhat does java use to calculatewhow to make java calculatorhow to make calculater in javacomputer programing code for a calculator in javacreate simple calculator javacreate a java calculatorcalcultor in javacalaculator in java codewhat does calc do in javaa program of calculator in javacalculator clas in java create a simple calculator taking input as stringarithmetic calculator java programsimple calculator javacalculator coding in javajava calculator programjava program to create a calculatorwrite a simple calculator program in javaimplement a simple calculator javajava program of calculatorcalculator app using javahow to build a calculator in javacalculator in java tutorialjava calculator program codecaculator java java calculatorjava comparatorcalculator javajava make calculatorexample program for calculator in javahow to create a calculator in javacalculator program in java using methodsjava calculatorjava tutorial calculatorbuilding a calculator in javajava program to make calculatorjava calculator classhow to make calculator app in javacreate calculator using javamake a simple calculator using stringsimple calculator java programcreate a basic calculator in javajava calculator source codebuilding calculator program in javacalculadora javacalculator program in java source codejava example calculatorcalcullator java program examplemake a calculator in javawhat is the code of calculator in javajava program to create a calcualtor code for calculator javashow to use methods to make a calculator in javajava function calculatorcalculater in java how to make calculator javajava simple calculatorbasic calculator in javabasic calculation codes javacalculator with javahow to make a calculator in jaajava program calculatorcomplete calculator in javajava caculator example code 5caqi calculator javahow to calculate are in java full codebasic calculator code in javajava calculatercacert javajava calculator projectsimple calculator javascript github vs codejava program for creating calculatormake a simple calculator in javabuild a calculator in javahow to make java calculatorjava program make calculatorjava calculatosimple calculator program in java source codehow to make a simple java calculatorjava big o calculatorjava simple calculator programjava calculator codecalculatrice javahow to build a calculator with javasimple calculator java source codehow to make a calculator in java