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