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}
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;
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}