In: Computer Science
JAVA make sure file name is FirstnameLastname_02_CS1Calculator. Thank you!
Overview
This program implements a simple, interactive calculator.
Major topics
In this document
Program Logic
Program Structure and Data
Calculator class
|
I/O
firstOperand = scan.nextLine();
firstNumber = Integer.parseInt(firstOperand);
Assumptions & Limitations
Test Plan
Note: no tests for bad data since this program is guaranteed a perfect user (i.e., no bad input).
Further work / challenges For any you do, run additional test cases to check the new functionality.
3 + 12 = 15
import java.util.Scanner; //import statement to get user input class
class Calculator{ //Class Containing Functions forlculation
void add(int a, int b){
int ans = a+b;
System.out.println("Answer is" +ans); //Function providing result for addition
}
void sub(int a, int b){
int ans = a-b;
System.out.println("Answer is " +ans); //Function providing result for subtraction
}
void mul(int a, int b){
int ans = a*b;
System.out.println("Answer is " +ans); //Function providing result for multiplication
}
void div(int a, int b){
int ans = a/b;
System.out.println("Answer is " +ans); //Function providing result for division
}
void mod(int a, int b){
int ans = a%b;
System.out.println("Answer is " +ans); //Function providing result for modulus
}
}
public class Main{ //Main class containing funcion calls, name of the main class can be changed
public static void main(String []args){ //just change the main class name and save the prog by same name if user wants
Calculator cal = new Calculator(); //Creating object of calculator class
Scanner sc = new Scanner(System.in); //Creating object of Scanner class for input
int n=0;
do{ //do-while loop to continue after one attempt
System.out.println("Enter your choice"); // Messages for User to choose Operation
System.out.println("Press 1 for Addition");
System.out.println("Press 2 for Subtraction");
System.out.println("Press 3 for Multiplication");
System.out.println("Press 4 for Division");
System.out.println("Press 5 for modulus");
int choice = sc.nextInt(); // Input for choice of user
System.out.println("Enter two numbers");
int a = sc.nextInt(); //Input for operands
int b = sc.nextInt();
if(choice == 1){ //Function calls according to user choice using objects
cal.add(a,b);
}
else if(choice == 2){
cal.sub(a,b);
}
else if(choice == 3){
cal.mul(a,b);
}
else if(choice ==4){
cal.div(a,b);
}
else if(choice == 5){
cal.mod(a,b);
}
else{
System.out.println("Invalid Choice"); //If choice entered is not present in the list
}
System.out.println("Press 0(zero) to exit and 1(one) to continue");
n = sc.nextInt(); // Input to choice to continue or not
} while(n==1); //condition for the loop
}
}
Okay so this is the code for calculator which takes user input for choices, operands and calculates the answer. I was not crystal clear about some of your lines in the question still I made what was best possible. The explaination for the code is explained in comments represented by (//) and followed by info about the particular line. You can also change class names according to your need just don't forget to save it by the name of the class which contains the main function.Thank you and All the best.