Challenge

Project

MainOperation.java

package Calculator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class MainOperation {
private static int calCount =0;

public static void main(String[] args) {

PerformingMathOperations math = new PerformingMathOperations();




while(true) {
calCount++;

Scanner scan = new Scanner(System.in);
System.out.println("Enter operation 1.Add, 2.Subtract, 3.Multiply, 4.Divide,"
+ " 5.Power of, 6.Square root of, 7.Log Of, 8.sine of, 9.inverse sine of, "
+ "10.cos of, 11.inverse cos of, 12.tan of, 13.inverse tan of, 14.Store, 15.Recall, 16.Decimal to Binary, 17.Binary to Decimal");
int opr = scan.nextInt();

if((opr == 1) || (opr == 2) || (opr == 3) || (opr == 4) || (opr == 5)) {
System.out.println("Enter opr1");
String opr1 = scan.next();
System.out.println("Enter opr2");
String opr2 = scan.next();
String s =math.doMathOpertion(opr1,opr2,opr);
System.out.println(s);
saveCalculatorLog(calCount,s);
}else if(opr==18) {
System.out.println("Enter DATA");
String d = scan.next();
saveUserData(d);
}else if(opr==19) {
String userdata =readUserSavedData();
System.out.println("YOUR DATA IS: "+userdata);
}else {
System.out.println("Enter opr1");
String opr1 = scan.next();
//System.out.println("Enter opr2");
//String opr2 = scan.next();
String s =math.doMathOpertion(opr1,opr);
System.out.println(s);
saveCalculatorLog(calCount,s);
}


}


}


private static void saveCalculatorLog(int i, String data) {
try {
FileWriter file = new FileWriter("C:\\Users\\RUDHRESH\\Desktop\\rough\\CalculationsHistory.txt",true);
file.write("\n");
file.write("cal_"+i+" :");
file.write(data);
file.write("\n");
file.write("___________________________________________________________");
file.flush();
file.close();
}
catch(Exception e) {

}

}



private static boolean saveUserData(String data) {
try {
FileWriter file = new FileWriter("C:\\Users\\RUDHRESH\\Desktop\\rough\\UserData.txt",false);
file.write(data);
file.write("\n");
file.flush();
file.close();
return true;
}
catch(Exception e) {
return false;
}

}


private static String readUserSavedData() {
String data ="NO DATA";
try {
File file = new File("C:\\Users\\RUDHRESH\\Desktop\\rough\\UserData.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
data =br.readLine();
return data;
}
catch(Exception e) {
data = "TRY LATER or NO USER DATA";
return data;
}
}
}

PerformingMathOperations.java

package Calculator;
public class PerformingMathOperations{
private String mathResulatantValue="";
private String resultantValue="";




public PerformingMathOperations() {
// TODO Auto-generated constructor stub
}



public String doMathOpertion(String operand1, String operand2, int opr) {
resultantValue ="";
mathResulatantValue="";
try {
long opr1 = Long.parseLong(operand1);
long opr2 = Long.parseLong(operand2);
int mathOpr = opr;

if(mathOpr== 1){
mathResulatantValue =doAdd(opr1,opr2);
}else if(mathOpr ==2){
mathResulatantValue =doSubtract(opr1,opr2);
}else if(mathOpr ==3){
mathResulatantValue =doMultiply(opr1,opr2);
}else if(mathOpr ==4){
mathResulatantValue =doDivide(opr1,opr2);
}else if(mathOpr ==5){
mathResulatantValue =doPowerOf(opr1,opr2);
}

}

catch(Exception e){
mathResulatantValue =""+e;
}
resultantValue = "operand1: "+operand1+" operand2: "+operand2+ " Result: "+mathResulatantValue;
return resultantValue;
}

private String doPowerOf(long opr1, long opr2) {
// TODO Auto-generated method stub
String result ="";
try {
// long value =opr*opr2;
// result = Long.toString(value);
long value =1;
for(long i=0; i<opr2; i++) {
value = value * opr1;
}
result = Long.toString(value);


}
catch(Exception e) {
System.out.println("doPowerOf : Exception: "+e);
result =""+e;
}
return result;
}


private String doDivide(long opr1, long opr2) {
// TODO Auto-generated method stub
String result ="";
try {
long value = (opr1/ opr2);
result = Long.toString(value);
}
catch(Exception e) {
System.out.println("doDivide : Exception: "+e);
result =""+e;
}
return result;
}

private String doMultiply(long opr1, long opr2) {
// TODO Auto-generated method stub
String result ="";
try {
long value = Math.multiplyExact(opr1, opr2);
result = Long.toString(value);
}
catch(Exception e) {
System.out.println("doMultiply : Exception: "+e);
result =""+e;
}
return result;
}

private String doSubtract(long opr1, long opr2) {
// TODO Auto-generated method stub
String result ="";
try {
long value = Math.subtractExact(opr1, opr2);
result = Long.toString(value);
}
catch(Exception e) {
System.out.println("doSubtract : Exception: "+e);
result =""+e;
}
return result;
}


private String doAdd(long operand1, long operand2) {

String result ="";
try {
long value = Math.addExact(operand1, operand2);
result = Long.toString(value);
}
catch(Exception e) {
System.out.println("doAdd : Exception: "+e);
result =""+e;
}
return result;
}



public String doMathOpertion(String operand1, int opr) {
resultantValue ="";
mathResulatantValue="";
try {
long opr1 = Long.parseLong(operand1, opr);
if(opr== 6){
mathResulatantValue =doSquareRootOf(opr1);
}else if(opr== 7){
mathResulatantValue =doLogOf(opr1);
}else if(opr== 8){
mathResulatantValue =doSineOf(opr1);
}else if(opr== 9){
mathResulatantValue =doInverseSineOf(opr1);
}else if(opr== 10){
mathResulatantValue =doCosOf(opr1);
}else if(opr== 11){
mathResulatantValue =doInverseCosOf(opr1);
}else if(opr== 12){
mathResulatantValue =doTanOf(opr1);
}else if(opr== 13){
mathResulatantValue =doInverseTanOf(opr1);
}else if(opr== 16){
mathResulatantValue =convertDecimaltoBinary(opr1);
}else if(opr== 17){
mathResulatantValue =convertBinarytoDecimal(operand1);
}else if(opr== 14){
mathResulatantValue =convertBinarytoDecimal(operand1);
}
}
catch(Exception e){

}
resultantValue = "operand1: "+operand1+" Result: "+mathResulatantValue;
return resultantValue;
}

private String convertBinarytoDecimal(String opr1) {
// TODO Auto-generated method stub
String result ="";
try {

result = Long.toString(Long.parseLong(opr1,2));
}
catch(Exception e) {
System.out.println("convertBinarytoDecimal : Exception: "+e);
result =""+e;
}
return result;
}

private String convertDecimaltoBinary(long opr1) {

String result ="";

try {

result = Long.toBinaryString(opr1);
}
catch(Exception e) {
System.out.println("convertDecimaltoBinary : Exception: "+e);
result =""+e;
}
return result;
}

private String doInverseTanOf(long opr1) {
// TODO Auto-generated method stub
String result ="";
try {
double value = Math.atan(opr1);
result = Double.toString(value);
}
catch(Exception e) {
System.out.println("doInverseTanOf : Exception: "+e);
result =""+e;
}
return result;
}


private String doTanOf(long opr1) {
// TODO Auto-generated method stub
String result ="";
try {
double value = Math.tan(opr1);
result = Double.toString(value);
}
catch(Exception e) {
System.out.println("doTanOf : Exception: "+e);
result =""+e;
}
return result;
}

private String doInverseCosOf(long opr1) {
// TODO Auto-generated method stub
String result ="";
try {
double value = Math.acos(opr1);
result = Double.toString(value);
}
catch(Exception e) {
System.out.println("doInverseCosOf : Exception: "+e);
result =""+e;
}
return result;
}

private String doCosOf(long opr1) {
// TODO Auto-generated method stub
String result ="";
try {
double value = Math.cos(opr1);
result = Double.toString(value);
}
catch(Exception e) {
System.out.println("doCosOf : Exception: "+e);
result =""+e;
}
return result;
}

private String doInverseSineOf(long opr1) {
// TODO Auto-generated method stub
String result ="";
try {
double value = Math.asin(opr1);
result = Double.toString(value);
}
catch(Exception e) {
System.out.println("doInverseSineOf : Exception: "+e);
result =""+e;
}
return result;
}

private String doSineOf(long opr1) {
// TODO Auto-generated method stub
String result ="";
try {
double value = Math.sin(opr1);
result = Double.toString(value);
}
catch(Exception e) {
System.out.println("doSineOf : Exception: "+e);
result =""+e;
}
return result;
}

private String doLogOf(long opr1) {
// TODO Auto-generated method stub
String result ="";
try {
double value = Math.log(opr1);
result = Double.toString(value);
}
catch(Exception e) {
System.out.println("doLogOf : Exception: "+e);
result =""+e;
}
return result;
}


private String doSquareRootOf(long opr1) {
// TODO Auto-generated method stub
String result ="";
try {
double value = Math.sqrt(opr1);
result = Double.toString(value);
}
catch(Exception e) {
System.out.println("doSquareRootOf : Exception: "+e);
result =""+e;
}
return result;
}

}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.