Author Topic: Simplistic Java Calculator  (Read 22 times)

Offline HappyTheorist

  • Mortar
  • ***
  • Posts: 2,725
  • Karma: +50/-0
  • Unwind with some Coffee and me, Spinachie
    • View Profile
    • Facade Entertainment Hub
Simplistic Java Calculator
« on: June 20, 2013, 02:44 pm »
Cliché time of the week has come, as toady I have created a calculator in Java. Please pardon the potential lack of spaces:

Master Class (Putts all of the Code Together):

Code: [Select]
package Calculator;
import java.util.Scanner;

public class Master {

public static void main (String args[]){

int data;
System.out.println("Please enter the value of your operation (1 for Addition, 2 for Subtraction, 3 for Multiplication and 4 for Division");
Scanner operator = new Scanner(System.in);
data = operator.nextInt();

switch (data){

case 1:
Addition additionObject = new Addition();
additionObject.answerAddition();
break;

case 2:
Subtraction subtractionObject = new Subtraction();
subtractionObject.answerSubtraction();
break;

case 3:
Multiplication multiplicationObject = new Multiplication();
multiplicationObject.answerMultiplication();
break;

case 4:
Division divisionObject = new Division();
divisionObject.answerDivision();
break;
}
}
}

Addition Class:

Code: [Select]
package Calculator;
import java.util.Scanner;

public class Addition {

public void answerAddition(){

double fnum, snum, answer;
System.out.println("You have Selected Addition... Please Enter your First Number");

Scanner problem = new Scanner(System.in);
fnum = problem.nextDouble();
System.out.print("Number");
System.out.println("Recrded");

System.out.println("Now, Enter Your Second Number");
snum = problem.nextDouble();
answer = snum + fnum;

System.out.println("Your Answer is..." + answer);
}
}

Subtraction Class:

Code: [Select]
package Calculator;
import java.util.Scanner;

public class Subtraction {

public void answerSubtraction(){

double fnum, snum, answer;
System.out.println("You have Selected Subtraction... Please Enter your First Number");

Scanner problem = new Scanner(System.in);
fnum = problem.nextDouble();
System.out.print("Number");
System.out.println("Recrded");

System.out.println("Now, Enter Your Second Number");
snum = problem.nextDouble();
answer = fnum - snum;

System.out.println("Your Answer is..." + answer);
}
}

Multiplication Class:

Code: [Select]
package Calculator;
import java.util.Scanner;

public class Multiplication {

public void answerMultiplication(){

double fnum, snum, answer;
System.out.println("You have Selected Multiplication... Please Enter your First Number");

Scanner problem = new Scanner(System.in);
fnum = problem.nextDouble();
System.out.print("Number");
System.out.println("Recrded");

System.out.println("Now, Enter Your Second Number");
snum = problem.nextDouble();
answer = snum * fnum;

System.out.println("Your Answer is..." + answer);
}
}

Division Class:

Code: [Select]
package Calculator;
import java.util.Scanner;

public class Division {

public void answerDivision(){

double fnum, snum, answer;
System.out.println("You have Selected Division... Please Enter your First Number");

Scanner problem = new Scanner(System.in);
fnum = problem.nextDouble();
System.out.print("Number");
System.out.println("Recrded");

System.out.println("Now, Enter Your Second Number");
snum = problem.nextDouble();
answer = fnum / snum;

System.out.println("Your Answer is..." + answer);
}
}

Please feel free to leave your criticism and praises. They're always welcome ^_^.


PM me to play Portal 2, SSBB or KI: