simple calculator program in java

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

showing results for - "simple calculator program in java"
Emmanuel
17 Jan 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}
Luciano
18 Jul 2020
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;  
Donovan
23 Jun 2020
1import java.util.Scanner;
2
3public class Calculator {
4
5    public static void main(String[] args) {
6
7        Scanner reader = new Scanner(System.in);
8        System.out.print("Enter two numbers: ");
9
10        // nextDouble() reads the next double from the keyboard
11        double first = reader.nextDouble();
12        double second = reader.nextDouble();
13
14        System.out.print("Enter an operator (+, -, *, /): ");
15        char operator = reader.next().charAt(0);
16
17        double result;
18
19        switch(operator)
20        {
21            case '+':
22                result = first + second;
23                break;
24
25            case '-':
26                result = first - second;
27                break;
28
29            case '*':
30                result = first * second;
31                break;
32
33            case '/':
34                result = first / second;
35                break;
36
37            // operator doesn't match any case constant (+, -, *, /)
38            default:
39                System.out.printf("Error! operator is not correct");
40                return;
41        }
42
43        System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
44    }
45}
queries leading to this page
java program calculatorjava calculator projectcode for calculator javascalc javajava program to create a calcualtor java 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 javaimplement a simple calculator javajava program for creating calculatorcalculater in java how 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 javacreate a basic calculator jacacaculator javajava program to make a calculatorsimple calculator program in java source codewriting a calculator in javabuild a calculator in javacode calculator javacalculator program with exit code javacreate simple calculator javawhow to make java calculatorcalculator 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 javaprogram 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 codejava program that calculate in java calculator programcalculator code javajava calculator program codecode to creat a simple calculator using javamaking calculator with javacalculaor javacalculator in java making 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 javaa simple calculator in javacalcultor example in javacreate a simple calculator taking input as stringcalculator java source codebasic calculator in javajava calculator app codecode for calculator javacalbulator program in javawrite a program in java to exhibit the functionality of a calculator how to make a basic calculator in javahow to program a calculator in javaalgorithm for building multiple arithmetic operation calculator with javajava bmi calculatorjava calculatocalculator function in javacreate a simple calculator in javahow to calculate in java programingsimple caulcotr with javajava program to implement basic calculator operationssimple calculator in core javacalculator java codeprogram in java to create a calculator with methods in javaexamples of calculator javajava create calculatormake a simple calculator using a string in javacalculator program in java using methodscalculator using java classsimple calculator program in java 22calculator java 22java calculator appwrite a java program to perform basic calculator operations java make calculatorimplement calculator in javajava example calculatorcreate a simple calculator javacalculator using switch case in javajava code for calculatorfull calculator program on javahow to make a calculator javacreate a basic calculator in javajava simple calculatorcalculator program in java source codejava code for calculator in notepadcreating a calculater in javasimple calculator javahow to make a calculator in javawhat is the code of calculator in javacalculator program in javajava calc functonsimple calculaotr javabasic java calculator programsimple calculator in java without switch casecomplete calculator in javajava big o calculatorhow to create a functioing calculator in javawrite a simple calculator program in javacalc 28 javacalculator for java mathcalculator app in javahow to make calculator in javaexample program for calculator in javajava calculator code examplenumber manipulation programs in javacreate calculator using javasimple calculator command in javacalculator javajava program for calculatorsimple calc in javahow to build a simple calculator using javacalculator in javabuilding calculator program in javacreate a calculator in javajava calculaterjava code for calculationmake a calculator in javacalculator app using javajava simple calculator programhow calculator works in jasimple caclater in javacalculator java program in javajava function calculatorcalculator javasimple calculator javascript github vs codejava 25 calculatorjava project calculatorhow to put 25 in a calculator using javajava calculator codehow to make a caculator in javajava caculator example code 5cbasic calculator program in javacalaculator in java codehow to create calculator in javasimple calculator java programcode 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 javasimple calculator design in javamake calculator in javacalculator java programmake a simple calculator in javajava calculator program using methodscalculator clas in java how to program a calculator with javajava tutorial calculatorhow to make java calculatorhow to program a simple calculator in javajava calculator filebasic 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 tutorialjava calculator onlinecalculator code for javacalculator on javajava simple calculator with methodshow to make a simple calculator in java using methodscreate a java calculatorcalculator in java source code 25 calculator in javajava calculator source codejava codes for calculatorsimple caculator javajava code calculatorcalculator kjavahow to make a simple calculator in javahow to amke a calcualtor in javacode in java for calculatorcreate simple calculator java classesjava calculator simplejava calculatorcalculator using javajava program calculations examplecalculator app javahow to build calculator in javacalculator 2f in javasimple calculator program in java