Skip to content

Instantly share code, notes, and snippets.

@julianjupiter
Created January 28, 2018 10:33
Show Gist options
  • Save julianjupiter/cc5fec1b141b10688edb67e79b6b1874 to your computer and use it in GitHub Desktop.
Save julianjupiter/cc5fec1b141b10688edb67e79b6b1874 to your computer and use it in GitHub Desktop.
Basic calculator in Java
import java.util.Scanner;
public class CalculatorTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("First Number: ");
double firstNumber = input.nextDouble();
System.out.print("Second Number: ");
double secondNumber = input.nextDouble();
Calculator calculator = new Calculator();
calculator.setFirstNumber(firstNumber);
calculator.setSecondNumber(secondNumber);
calculator.add();
calculator.subtract();
calculator.multiply();
calculator.divide();
System.out.println("First Number is " + calculator.getFirstNumber());
System.out.println("Second Number is " + calculator.getSecondNumber());
}
}
class Calculator {
private double firstNumber;
private double secondNumber;
private double sum;
private double difference;
private double product;
private double quotient;
public double getFirstNumber() {
return firstNumber;
}
public void setFirstNumber(double firstNumber) {
this.firstNumber = firstNumber;
}
public double getSecondNumber() {
return secondNumber;
}
public void setSecondNumber(double secondNumber) {
this.secondNumber = secondNumber;
}
public void add() {
sum = firstNumber + secondNumber;
System.out.println("The sum is " + sum);
}
public void subtract() {
difference = firstNumber - secondNumber;
System.out.println("The difference is " + difference);
}
public void multiply() {
product = firstNumber * secondNumber;
System.out.println("The product is " + product);
}
public void divide() {
quotient = firstNumber / secondNumber;
System.out.println("The quotient is " + quotient);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment