Answers for "how to make a calculator in java"

0

calculator program in java

import java.util.Scanner;
public class Calculator {
   public static void main(String[] args) {
      double num1;
      double num2;
      double ans;
      char op;
      Scanner reader = new Scanner(System.in);
      System.out.print("Enter two numbers: ");
      num1 = reader.nextDouble();
      num2 = reader.nextDouble();
      System.out.print("\nEnter an operator (+, -, *, /): ");
      op = reader.next().charAt(0);
      switch(op) {
         case '+': ans = num1 + num2;
            break;
         case '-': ans = num1 - num2;
            break;
         case '*': ans = num1 * num2;
            break;
         case '/': ans = num1 / num2;
            break;
      default: System.out.printf("Error! Enter correct operator");
         return;
      }
      System.out.print("\nThe result is given as follows:\n");
      System.out.printf(num1 + " " + op + " " + num2 + " = " + ans);
   }
}
Posted by: Guest on August-15-2020
1

how to make a calculator in java

// this was all coded by me, to use it get Eclipse, or IntelliJ and make a new
// Java Project

package javaCode;

import java.util.Scanner;

public class testCode {

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Would you like to Add, Subtract, Divide, or Multiply?");
		
		String mathType = scan.next();
		
		if (mathType.equalsIgnoreCase("add")) {
			System.out.println("Whats the first number you want to add?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " + ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " + " + secondNumber + " =");
			System.out.println(firstNumber + secondNumber);
			
		} else if (mathType.equalsIgnoreCase("subtract")) {
			System.out.println("Whats the first number you want to subtract?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " - ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " - " + secondNumber + " =");
			System.out.println(firstNumber - secondNumber);
		} else if (mathType.equalsIgnoreCase("divide")) {
			System.out.println("Whats the first number you want to divide?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " % ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " % " + secondNumber + " =");
			System.out.println(firstNumber / secondNumber);
		} else if (mathType.equalsIgnoreCase("multiply")){
			System.out.println("Whats the first number you want to multiply?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " x ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " x " + secondNumber + " =");
			System.out.println(firstNumber * secondNumber);
		}
		
	}

}
Posted by: Guest on August-13-2020
1

java calculator code

Save this file as MyCalculator.java 
to compile it use 
    javac MyCalculator.java 
to use the calcuator do this 
    java MyCalculator 
 
**********************************************/  
import java.awt.*;  
import java.awt.event.*;  
/*********************************************/  
  
public class MyCalculator extends Frame  
{  
  
public boolean setClear=true;  
double number, memValue;  
char op;  
  
String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-", "." };  
String operatorButtonText[] = {"/", "sqrt", "*", "%", "-", "1/X", "+", "=" };  
String memoryButtonText[] = {"MC", "MR", "MS", "M+" };  
String specialButtonText[] = {"Backspc", "C", "CE" };  
  
MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length];  
MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length];  
MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length];  
MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length];  
  
Label displayLabel=new Label("0",Label.RIGHT);  
Label memLabel=new Label(" ",Label.RIGHT);  
  
final int FRAME_WIDTH=325,FRAME_HEIGHT=325;  
final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10;  
final int TOPX=30, TOPY=50;  
///////////////////////////  
MyCalculator(String frameText)//constructor  
{  
super(frameText);  
  
int tempX=TOPX, y=TOPY;  
displayLabel.setBounds(tempX,y,240,HEIGHT);  
displayLabel.setBackground(Color.BLUE);  
displayLabel.setForeground(Color.WHITE);  
add(displayLabel);  
  
memLabel.setBounds(TOPX,  TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT);  
add(memLabel);  
  
// set Co-ordinates for Memory Buttons  
tempX=TOPX;   
y=TOPY+2*(HEIGHT+V_SPACE);  
for(int i=0; i<memoryButton.length; i++)  
{  
memoryButton[i]=new MyMemoryButton(tempX,y,WIDTH,HEIGHT,memoryButtonText[i], this);  
memoryButton[i].setForeground(Color.RED);  
y+=HEIGHT+V_SPACE;  
}  
  
//set Co-ordinates for Special Buttons  
tempX=TOPX+1*(WIDTH+H_SPACE); y=TOPY+1*(HEIGHT+V_SPACE);  
for(int i=0;i<specialButton.length;i++)  
{  
specialButton[i]=new MySpecialButton(tempX,y,WIDTH*2,HEIGHT,specialButtonText[i], this);  
specialButton[i].setForeground(Color.RED);  
tempX=tempX+2*WIDTH+H_SPACE;  
}  
  
//set Co-ordinates for Digit Buttons  
int digitX=TOPX+WIDTH+H_SPACE;  
int digitY=TOPY+2*(HEIGHT+V_SPACE);  
tempX=digitX;  y=digitY;  
for(int i=0;i<digitButton.length;i++)  
{  
digitButton[i]=new MyDigitButton(tempX,y,WIDTH,HEIGHT,digitButtonText[i], this);  
digitButton[i].setForeground(Color.BLUE);  
tempX+=WIDTH+H_SPACE;  
if((i+1)%3==0){tempX=digitX; y+=HEIGHT+V_SPACE;}  
}  
  
//set Co-ordinates for Operator Buttons  
int opsX=digitX+2*(WIDTH+H_SPACE)+H_SPACE;  
int opsY=digitY;  
tempX=opsX;  y=opsY;  
for(int i=0;i<operatorButton.length;i++)  
{  
tempX+=WIDTH+H_SPACE;  
operatorButton[i]=new MyOperatorButton(tempX,y,WIDTH,HEIGHT,operatorButtonText[i], this);  
operatorButton[i].setForeground(Color.RED);  
if((i+1)%2==0){tempX=opsX; y+=HEIGHT+V_SPACE;}  
}  
  
addWindowListener(new WindowAdapter()  
{  
public void windowClosing(WindowEvent ev)  
{System.exit(0);}  
});  
  
setLayout(null);  
setSize(FRAME_WIDTH,FRAME_HEIGHT);  
setVisible(true);  
}  
//////////////////////////////////  
static String getFormattedText(double temp)  
{  
String resText=""+temp;  
if(resText.lastIndexOf(".0")>0)  
    resText=resText.substring(0,resText.length()-2);  
return resText;  
Posted by: Guest on March-15-2020
0

java calculator

package main;

import java.util.Scanner;

public class calculator {

    public static void greetings(){
        System.out.println("Hi welcome to the calculator made by Pepa pig made in java! :D");
    }

    public static void instruction(){
        System.out.println("Hi welcome to the calculator made by Pepa pig made in java! :D");
    }

    public static void calculator(){
        Scanner keyboardInput = new Scanner(System.in);
        String calculator;

        do {
            System.out.print("Enter your first digit: ");
            double num1 = keyboardInput.nextDouble();

            System.out.print("Enter your operator: ");
            String op = keyboardInput.next();

            System.out.print("Enter your second digit: ");
            double num2 = keyboardInput.nextDouble();

            System.out.print("= ");

            if (op.equals("+")){
                System.out.print(num1 + num2);
                System.out.println("\nYou were adding");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("-")){
                System.out.print(num1 - num2);
                System.out.println("\nYou were subtracting");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("*")){
                System.out.print(num1 * num2);
                System.out.println("\nYou were multiplying");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("/")){
                System.out.print(num1 / num2);
                System.out.println("\nYou were dividing");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else if (op.equals("%")){
                System.out.print(num1 % num2);
                System.out.println("\nYou were remanding");
                System.out.println("How I know that cause I have big brain and a smart programmer ;)");
            }else{
                System.out.println("Invalid operator");
            }
            System.out.print("Would you like to restart the calculator? (y or n): ");
            calculator = keyboardInput.next();
        }
        while (calculator.equals("y"));

        if (calculator.equals("n")){
            System.out.println("Leave then kiddo ;-;");
        }
    }


    public static void main(String[] args) {
        greetings();
        instruction();
        calculator();
    }
}
Posted by: Guest on October-07-2020
0

calculator in java

import java.util.Scanner;

class Main {
  public static void main(String[] args) {

    char operator;
    Double number1, number2, result;

    // create an object of Scanner class
    Scanner input = new Scanner(System.in);

    // ask users to enter operator
    System.out.println("Choose an operator: +, -, *, or /");
    operator = input.next().charAt(0);

    // ask users to enter numbers
    System.out.println("Enter first number");
    number1 = input.nextDouble();

    System.out.println("Enter second number");
    number2 = input.nextDouble();

    switch (operator) {

      // performs addition between numbers
      case '+':
        result = number1 + number2;
        System.out.println(number1 + " + " + number2 + " = " + result);
        break;

      // performs subtraction between numbers
      case '-':
        result = number1 - number2;
        System.out.println(number1 + " - " + number2 + " = " + result);
        break;

      // performs multiplication between numbers
      case '*':
        result = number1 * number2;
        System.out.println(number1 + " * " + number2 + " = " + result);
        break;

      // performs division between numbers
      case '/':
        result = number1 / number2;
        System.out.println(number1 + " / " + number2 + " = " + result);
        break;

      default:
        System.out.println("Invalid operator!");
        break;
    }

    input.close();
  }
}
Posted by: Guest on June-18-2021
0

calculator in java using methods

import java.util.Scanner;


public class Calculator {


    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        System.out.println("Simple Calculator");

        System.out.println("\nHere are your options:");
        System.out.println("\n1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Division");
        System.out.println("4. Multiplication");

        System.out.print("\nWhat would you like to do?: ");
        int choice = kb.nextInt();
        System.out.println();


        if (choice == 1){
            addition();
        }
        else if (choice == 2){
            subtraction();
        }
        else if (choice == 3){
            division();
        }
        else if (choice == 4){
            multiplication();
        }

        System.out.println();
        kb.close();
    }

    public static void addition(){

        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Addition");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " + " + nTwo + " = " + (nOne + nTwo));
    }

    public static void subtraction(){
        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Subtraction");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " - " + nTwo + " = " + (nOne - nTwo));
    }

    public static void division(){
        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Division");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " / " + nTwo + " = " + (nOne / nTwo));
    }

    public static void multiplication(){
        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Multiplication");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " x " + nTwo + " = " + (nOne * nTwo));
    }
}
Posted by: Guest on August-04-2021

Code answers related to "how to make a calculator in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language