java calculator code

Solutions on MaxInterview for java calculator code by the best coders in the world

showing results for - "java calculator code"
Louis
30 Jan 2017
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}
Souleymane
23 Jun 2018
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}
Mirko
23 Sep 2016
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;  
Sasha
21 Apr 2019
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}
Martina
15 Apr 2020
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}
queries leading to this page
calculate with javajava program calculatorjava calculator projectcode for calculator javascalc javajava program to create a calcualtor in java calculator program needed a classjava calculatrojava program of calculatorcalculator in javajava program to make a simple calculatorcalculator app java point functioncreate a calculator javalogic of calculator program of many numbers in javawhat does calc do in javaimplement a simple calculator javajava program for creating calculatorcalculater in java calculator in javshow to make calculator in jaahow to make calculater in javacalculator simple program in javacalculatorsys javabuilding a calculator in javajava code for simple calculatorhow to make a simple java calculatorjava calculator programcalculator using scanner method in javajava method calculatorhow to use methods to make a calculator in javaa program of calculator in javabasic java calculator using scannercreate a basic calculator jacajava program to make a calculatorjava program to make calculatorcaculator javasimple calculator program in java source codehow to make in calculator x 5ey javacreating a calculator with java build a calculator in javacalculator program in java using switch casewriting a calculator in javacode calculator javahow to calculate are in java full codewhow to make java calculatorcalculator program with exit code javacreate simple calculator javacalculator java codejava make a calculatorcalculator coding in javabuilding a calculator using javajava code to create a simple calculatorcreate calculator in javaprogram to create calculator in javahow to code a calculator in javajava program where the user chooses his operation without case program to make a simple calculator in javasimple calculation in javahow to make simple cacultor in javaarithmetic calculator java programjava calcuator codehow to build a calculator with javajava function calculator codecalcultor using javacreate calculator app in javain java calculator programjava program that calculate calculator code javajava calassesjava calculator program codecode to creat a simple calculator using javamaking calculator with javacalculaor javacalculator in java code for calculator in java 21 java calculator program needed a classmaking a calculator in javahow to write calculator program in javacalculator code in javahow to make a calculator using javahow to make a java calculatorjava program to create a calculatorsimple calculator java source codecreating calculator in javacode java calculatricesimple calculator in javacreate a simple calculator using javabasic calculator javahow to make calculator javajava code to create a calculatorhow to create a calculator in javacalculator with javahow to covert 0721501212 to 2b27721501212 in javacalcultor example in javacreate a simple calculator taking input as stringjava operator how to make an calculatorcalculator java source codebasic calculator in javasimple calculator operators program with same method name in javajava calculator app codecode for calculator javacalbulator program in javawrite a program in java to exhibit the functionality of a calculator algorithm for building multiple arithmetic operation calculator with javahow to program a calculator in javacalculatro javajava bmi calculatorhow to make a basic calculator in javajava calculatosimple calculator in java without switch casecalculator function in javacreate a simple calculator in javahow to covert 0721501212 to 27721501212 in javahow to calculate in java programingsimple caulcotr with javajava program to implement basic calculator operationscalculator logic in javacalculator java codecalculator class in javajava calculatecalcultor in javaexamples of calculator javamake a simple calculator using a string in javacalculator program in java using methodswhat does java use to calculatehow to make calculator app in javacalculator using java classjava create calculatorsimple calculator program in java 22calculator java 22java calculator appwrite a java program to perform basic calculator operations java make calculatorimplement calculator in javajava example calculator calculator javacreate a simple calculator javacalculator using switch case in javajava code for calculatorfull calculator program on javajava comparatorhow to make a calculator javamaking calculator in javacreate a basic calculator in javacalculator program in java source codejava simple calculatorjava code for calculator in notepadcreating a calculater in javasimple calculator javahow to make a calculator in javajava calculator classwhat is the code of calculator in javacalculator program in javajava calc functonbig o calculator javabasic java calculator programmake calculator using oops in javasimple java calculator in javajava big o calculatorhow to create a functioing calculator in javacomplete calculator in javawrite a simple calculator program in javacalc 28 javacalculator for java mathcalculator app in javaprogram in java to create a calculator with methods in javahow to make calculator in javaexample program for calculator in javajava calculator code examplecreate calculator using javacalculator javacacert javahow to make ajava statistics calculatorcalculator using scanner in javajava program for calculatorsimple calc in javaprogram a calculator in javacalculator in javabuilding calculator program in javacreate a calculator in javajava calculaterjava code for calculationmake a calculator in javacalculator app using javajava simple calculator programcalculator in javaswinghow calculator works in jasimple caclater in javacalculator java program in javajava calculator operatorscalculadora javajava function calculatorcalculator javasimple calculator javascript github vs codejava make caculatorjava 25 calculatorjava project calculatorhow to put 25 in a calculator using javahow to write a calculator in javajava calculator codejava solverhow to make a caculator in javajava caculator example code 5cbasic calculator program in javacalaculator in java codehow to create calculator in javacalculator example in javasimple calculator java programjava program make calculatorcode for java calcullatorsteps in building a calculator in javamaking a simple calculator in javacalculator program with calulate method in javajava calculatprsimple java calculatorbasic calculation codes javamake calculator in javacalculator java classescalculator java programmake a simple calculator in javajava calculator program using methodscalculator clas in java how to make a calculator in jaajava how to make calculatorhow to program a calculator with javajava class method calculatorjava tutorial calculatorhow to make java calculatorhow to program a simple calculator in javajava calculator filemake a simple calculator using stringcalcullator java program examplebasic java calculatorcomputer programing code for a calculator in javacalculator in java entter any numbermake calculator using javawindows calculator frontend development in javaprogram for making calculator in javajava how to use multiple calculator operations in one screencalculator in java tutorialhow to build a calculator in javajava calculator onlineaqi calculator javacalculator code for javacalculator on javahow to make a simple calculator in java using methodscreate a class calculator in javacalculator in java source codecreate a java calculator 25 calculator in javajava calculator source codejava codes for calculatorsimple caculator javabuilding aa basic calculator javajava code calculatorcalculator kjavacalculatrice javahow to make a simple calculator in javacode in java for calculatorhow to amke a calcualtor in javacreate simple calculator java classesjava calculator simplejava calculatorcalculator using javajava program calculations examplejava calculation how to build calculator in javacalculator app javabasic calculator code in javacalculator 2f in javacreate calculator java java calculatorjava calculator code