Project
...CODES FOR THE CONSOLE CALCULATOR APPLICATION...
interface iCalc
/*
-- Interface iCalc represents the basic methods for the Calculate class
-- Creates Interface Structure
-- Can be used for creating any class which would do any sort of calculations
*/
//adds interface iCalc to package calculator.
public interface iCalc {
public void doCalculation();
public void getResult();
public void showHistory();
}
class calculator
/*
* -- Class Calculator Performs basic operations like Add, Substract, Multiply, Division for two numbers
*- Uses class Calculate by creating its objects and then calling its methods
*/
import java.io.*; //imports required packages
//adds class to the package
public class Calculator extends Calculate {
public void Calc() throws java.io.IOException {
boolean next;
do { //for repeated inputs
Integer iFirstNumber = new Integer(0);
Integer iSecondNumber = new Integer(0);
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter First Number: "); // get user input
System.out.flush();
try {
iFirstNumber = Integer.parseInt(buffer.readLine());
} catch (NumberFormatException e) {
System.out.println("*///*Please provide numeric values.*///*");
System.exit(0);
}
System.out.println("Please enter the Operation (Add : +, Minus : -, Product : *, Divide : /):");
System.out.flush();
String option = buffer.readLine();
System.out.println("Please enter Second Number: ");
System.out.flush();
try {
iSecondNumber = Integer.parseInt(buffer.readLine(), 10);
} catch (NumberFormatException e) {
System.out.println("*///*Please provide numeric values.*///*");
System.exit(0);
}
if (option.length() == 1) {
Calculate c = new Calculate(iFirstNumber, option.charAt(0), iSecondNumber); // // Creates Calculate
// Class Instance
// calling class methods
c.doCalculation();
c.getResult();
} else {
System.out.println("*///*Operation Not Available. Please select any of the available options.*///*");
}
// Checks if the user would like to calculate again
System.out.println("Would you like to calculate again (y/n)?");
System.out.flush();
char response = (char) buffer.read();
if ((response == 'y') || (response == 'Y')) {
next = false;
} else {
next = true;
}
} while (!next);
}
}
class calculate
/*
* -- Class Calculate has all methods for calculation as required by any Calculator classes
*-- Implements an interface iCalc
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;
//this is including class to package calculator
public class Calculate implements iCalc { // class definition
private char Operator;
private int iFNum, iSNum;
private Double dblNumber = new Double(0);
private Double dblResult = new Double(0);
private int iResult = 0;
private boolean typeDouble = false;
private boolean typeInt = false;
public Calculate() { // Defines a constructor for scientific calculations
}
public Calculate(Double dblNum, char cOperator) {
dblNumber = dblNum;
Operator = cOperator;
typeDouble = true;
}
public Calculate(int iFirstNum, char cOperator, int iSecondNum) { // Defines a constructor for basic calculations
iFNum = iFirstNum;
iSNum = iSecondNum;
Operator = cOperator;
typeInt = true;
}
public void doCalculation() { // Calculates the Result based on the operator selected by the user
File file = new File("calculationhistory.txt"); //text file to save current history
file.deleteOnExit(); //this file is just for that calculation history after this it will delete automatically
try {
FileWriter fw = new FileWriter(file); //this is saving history
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
iResult = 0;
dblResult = 0.0;
switch (Operator) {
case '+': // case for addition
checkInt();
iResult = iFNum + iSNum;
fw.append(iFNum + "" + Operator + "" + iSNum + "=" + iResult);
fw.close();
break;
case '-': // case for substraction
checkInt();
iResult = iFNum - iSNum;
bw.newLine();
fw.append(iFNum + "" + Operator + "" + iSNum + "=" + iResult);
fw.close();
break;
case '*': // case for multiplication
checkInt();
iResult = iFNum * iSNum;
bw.newLine();
fw.write(iFNum + "" + Operator + "" + iSNum + "=" + iResult);
fw.close();
break;
case '/': // case for division
checkInt();
if (!checkSecondNum()) {
iResult = iFNum / iSNum;
bw.newLine();
fw.write(iFNum + "" + Operator + "" + iSNum + "=" + iResult);
fw.close();
break;
}
case 'S': // case for sine operator
case 's':
checkDouble();
dblResult = Math.sin(dblNumber);
fw.write("Sin" + "" + dblNumber + "" + "" + "=" + dblResult);
fw.close();
break;
case 'C': // case for cosine operator
case 'c':
checkDouble();
dblResult = Math.cos(dblNumber);
fw.write("Cos" + "" + dblNumber + "" + "" + "=" + dblResult);
fw.close();
break;
case 'T': // case for tangent operator
case 't':
checkDouble();
dblResult = Math.tan(dblNumber);
fw.write("Tan" + "" + dblNumber + "" + "" + "=" + dblResult);
fw.close();
break;
case 'L': // case for logarithmic operation
case 'l':
checkDouble();
dblResult = Math.log(dblNumber);
fw.write("Log" + "" + dblNumber + "" + "" + "=" + dblResult);
fw.close();
break;
case 'z':
case 'Z':
checkDouble();
dblResult = Math.log(dblNumber);
break;
case 'r': // case for square root
case 'R':
checkDouble();
dblResult = Math.sqrt(dblNumber);
fw.write("Sqrt" + "" + dblNumber + "" + "" + "=" + dblResult);
fw.close();
break;
case 'i':
case 'I': // case for inverse of cosine operator
checkDouble();
dblResult = Math.acos(dblNumber);
fw.write("ICos" + "" + dblNumber + "" + "" + "=" + dblResult);
fw.close();
break;
case 'j': // case for inverse of sine operator
case 'J':
checkDouble();
dblResult = Math.asin(dblNumber);
fw.write("ISin" + "" + dblNumber + "" + "" + "=" + dblResult);
fw.close();
break;
case 'k': // case for inverse of tangent operator
case 'K':
checkDouble();
dblResult = Math.atan(dblNumber);
fw.write("ITan" + "" + dblNumber + "" + "" + "=" + dblResult);
fw.close();
break;
case 'n': // case for nth power of entered number
case 'N':
Scanner sc = new Scanner(System.in);
int nrt;
System.out.println("enter the number which you want power for ");
nrt = sc.nextInt();
checkDouble();
dblResult = (double) Math.round(Math.pow(dblNumber, nrt));
fw.write(nrt + "" + "root of" + "" + dblNumber + "" + "" + "=" + dblResult);
fw.close();
break;
case 'd': // case for converting decimal number into binary string
case 'D':
Scanner sc1 = new Scanner(System.in);
int dn;
System.out.println("enter the number to convert into binary ");
dn = sc1.nextInt();
System.out.println("Binary representation of " + dn);
String qwe = Integer.toBinaryString(dn);
System.out.println(Integer.toBinaryString(dn));
fw.write("binary rep of " + "" + dn + "" + "" + "=" + qwe);
fw.close();
break;
case '1': //case for future investment calculator
Scanner sc11 = new Scanner(System.in);
System.out.print("Enter present value: ");
double p = sc11.nextInt();
System.out.print("Enter the interest rate: ");
double r = sc11.nextInt();
System.out.print("Enter the time period in years: ");
double y = sc11.nextInt();
double f = p * Math.pow((1 + r / 100), y);
System.out.print("value is: " + f);
bw.close();
break;
case 'b': //case to change binary string to decimal number
case 'B':
System.out.println("Please enter a binary string: ");
Scanner sc2 = new Scanner(System.in);
String b2d = sc2.nextLine();
System.out.println("the decimal number is " + Integer.parseInt(b2d, 2));
break;
default:
iResult = 0;
dblResult = 0.0;
System.out.println("*///*Operation Not Available. Please select any of the available options.*///*");
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//display the result of calculations as an output
public void getResult() {
if (typeInt) {
System.out.println("The result is: " + iResult);
} else if (typeDouble) {
System.out.println("The result is: " + dblResult);
}
}
//checks for zero
public boolean checkSecondNum() {
if (iSNum == 0) {
System.out.println("Zero Not allowed");
System.exit(0);
return true;
} else {
return false;
}
}
public void checkInt() { //checking if number type is integer
if (!typeInt) {
iResult = 0;
System.out.println("*///*Operation Not Available. Please select any of the available options.*///*");
System.exit(0);
}
}
public void checkDouble() { //checking if number type is float
if (!typeDouble) {
dblResult = 0.0;
System.out.println("*///*Operation Not Available. Please select any of the available options.*///*");
System.exit(0);
}
}
@Override //overriden method
public void showHistory() {
// TODO Auto-generated method stub
}
}
class ScientificCalculator
/*
* -- Class ScientificCalculator Performs scientific calculations like Sine, Cosine, Tangent and Log of a number, squareroot, nth power of number, inverse functions, conversion of decimal to binary and vise versa
*
*-- Inherits class Calculate
*-- Methods of Super class Calculate can be directly called by using the object of this Sub class ScientificCalculator
*/
import java.io.*; //imports required packages
import java.util.Scanner;
public class ScientificCalculator extends Calculate // class definition
{
char Operator;
Double dblNumber = new Double(0);
ScientificCalculator() { //ScientificCalculator
}
ScientificCalculator(double dblNumber, char Operator) {
super(dblNumber, Operator); // Calls Super Class Constructor
this.Operator = Operator;
this.dblNumber = dblNumber;
}
public void Calc() throws java.io.IOException {
boolean next;
do {
Double d = new Double(0);
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));//buffered reader to read the input from the user
// Gets User Input
System.out.println(
"Please enter the Operation (Sine-s, Cosine-c, Tangent-t, NaturalLog-z,Logarithm-l");
System.out.println( " SquareRoot-r, NthPower-n, InverseCos-i, InverseSin-j, InverseTan-k, decimaltobinary-d");
System.out.println( " binarytodecimal-b, fiv-1, ):");
System.out.flush();
String option = buffer.readLine(); //option from above is saved here
System.out.println("Please enter a Value: ");
System.out.flush();
try {
d = Double.valueOf(buffer.readLine());
} catch (NumberFormatException e) {
System.out.println("*///*Please provide numeric values.*///*");
System.exit(0);
}
if (option.length() == 1) {
// Creates Class Instance
ScientificCalculator sc = new ScientificCalculator(d, option.charAt(0));
sc.doCalculation(); // Calls Super Class Methods
sc.getResult(); // Calls Super Class Methods
} else {
System.out.println("*///*Operation Not Available. Please select any of the available options.*///*");
}
System.out.println("Would you like to calculate again (y/n)?");
System.out.println("");
System.out.println("ShowpreviousCalculation: p"); //to show the previous calculation done by user
System.out.println("");
System.out.println("Store for later use" + "(s)");
System.out.flush();
char aa = (char) buffer.read();
if ((aa == 'y') || (aa == 'Y')) {
System.out.println("your saved memo is");
File file = new File("History.txt"); //recalling facility provided with this
BufferedReader br = new BufferedReader(new FileReader(file));
String abc = br.readLine();
System.out.println(abc);
next = false;
} else if (aa == 'p') {
File file = new File("calculationHistory.txt"); //previous calculation
BufferedReader br = new BufferedReader(new FileReader(file));
String abc = br.readLine();
System.out.println(abc);
next = false;
} else if (aa == 's') { //if want to add from existing then
System.out.println("do you want to add previous one or your own?");
System.out.println("previous :::p");
System.out.println("your own:::o");
Scanner sc = new Scanner(System.in);
String mc = sc.next();
if (mc.equals("p")) {
File file = new File("calculationHistory.txt");
BufferedReader br = new BufferedReader(new FileReader(file)); //readed from the history of calculation and saved as memory history.txt
String memo1 = br.readLine();
FileWriter fw = new FileWriter("History.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(memo1);
bw.close();
System.out.println("success!!");
} else {
System.out.println("add number of your choice."); //if want to add memo of own choice
String mc2 = sc.next();
String memo2 = mc2;
FileWriter fw = new FileWriter("History.txt"); //memo saved here
BufferedWriter bw = new BufferedWriter(fw);
bw.write(memo2);
bw.close();
System.out.println("success!!");
}
next = false;
} else {
next = true;
}
} while (!next);
}
}
class UseCalculator
/*
*-- Class UseCalculator is the Main Class for the Calculator Application
*-- Provides options to the user: Basic or Scientific Calculator
*/
import java.io.*; //importing required packages
public class UseCalculator // class definition
{
public static void main(String[] args) throws java.io.IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
//// Gets User Input
// here two type calculator created
System.out.println("Select the Calculator: Basic - B or Scientific - S."); // b for basic calculations and s for
// scientific ones
System.out.flush();
String option = buffer.readLine();
if (option.length() == 1) {
if (option.equals("B") || option.equals("b")) {
Calculator c = new Calculator(); // Calls the Basic Calculator Application
c.Calc();
} else if (option.equals("S") || option.equals("s")) {
ScientificCalculator sc = new ScientificCalculator(); // Calls the scientific Calculator Application
sc.Calc();
} else {
System.out.println("*///*Please enter option 'B' or 'S'.*///*");
}
} else {
System.out.println("*///*Please enter option 'B' or 'S'.*///*");
}
}
}