123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- import java.util.Scanner;
- import java.lang.*;
-
- public class Operations{
-
- Scanner stdin = new Scanner(System.in);
-
- public double chooseOp(double num){
-
- boolean isValid = false;
- int choice;
- double result = 0;
-
- while(!isValid){
- System.out.println("\n-------------------------------------------------");
- System.out.println(num);
- System.out.println("-------------------------------------------------");
- System.out.println("Select type of operation to perform (enter # 1-3)");
- System.out.println("(1) Core Functions");
- System.out.println("(2) Trigonometry");
- System.out.println("(3) Advanced Functions");
- System.out.println("-------------------------------------------------");
- System.out.print("> ");
- choice = stdin.nextInt();
-
- switch(choice){
- case 1: result = coreFunc(num);
- isValid = true;
- break;
- case 2: result = trigFunc(num);
- isValid = true;
- break;
- case 3: result = advFunc(num);
- isValid = true;
- break;
- default:
- break;
- }
- }
- return result;
- }
-
- public double coreFunc(double num){ //take displayNum as input parameter
-
- boolean isValid = false;
- int choice;
- double entNum, result = 0;
-
-
- while(!isValid){
- System.out.println("\n-------------------------------------------------");
- System.out.println(num);
- System.out.println("-------------------------------------------------");
- System.out.println("Select core function to perform (enter # 1-9)");
- System.out.println("(1) Add");
- System.out.println("(2) Subtract");
- System.out.println("(3) Multiply");
- System.out.println("(4) Divide");
- System.out.println("(5) Square");
- System.out.println("(6) Square Root");
- System.out.println("(7) Exponentiation");
- System.out.println("(8) Inverse (1/x)");
- System.out.println("(9) Change sign (+/-)");
- System.out.println("---------------------------------------------");
- System.out.print("> ");
- choice = stdin.nextInt();
-
- switch(choice){
- case 1: System.out.println("Enter number to add to " + num + ": ");
- System.out.print("> ");
- entNum = stdin.nextDouble();
- result = num + entNum;
- isValid = true;
- break;
- case 2: System.out.println("Enter number to subtract from " + num + ": ");
- System.out.print("> ");
- entNum = stdin.nextDouble();
- result = num - entNum;
- isValid = true;
- break;
- case 3: System.out.println("Enter number to multiply to " + num + ": ");
- System.out.print("> ");
- entNum = stdin.nextDouble();
- result = num * entNum;
- isValid = true;
- break;
- case 4: System.out.println("Enter number to divide from " + num + ": ");
- System.out.print("> ");
- entNum = stdin.nextDouble();
- result = num / entNum; //use Double.isNaN(x)
- isValid = true;
- break;
- case 5: System.out.println("Squaring number on display (" + num + "^2) ");
- System.out.print("> ");
- result = num * num;
- isValid = true;
- break;
- case 6: System.out.println("Taking square root of " + num);//cant take negative #
- System.out.print("> ");
- result = Math.sqrt(num);
- isValid = true;
- break;
- case 7: System.out.println("Enter exponent to raise " + num + " to (" + num + "^n)");
- System.out.print("> ");
- entNum = stdin.nextDouble();
- result = num * entNum;
- isValid = true;
- break;
- case 8: System.out.println("Calculating inverse of " + num + " (1/" + num + ")");
- System.out.print("> ");
- result = 1/num;
- isValid = true;
- break;
- case 9: System.out.println("Calculating negation of " + num + " (n -> -n , -n -> n)");
- System.out.print("> ");
- result = num * -1;
- isValid = true;
- break;
- default:
- break;
- }
- }
- return result;
- }
-
-
- public double trigFunc(double num){ //take displayNum as input parameter
-
- boolean isValid = false;
- int choice;
- double result = -0;
-
- while(!isValid){
- System.out.println("\n-------------------------------------------------");
- System.out.println(num);
- System.out.println("-------------------------------------------------");
- System.out.println("Select trigonemtric function (enter # 1-6)");
- System.out.println("(1) Sin");
- System.out.println("(2) Cos");
- System.out.println("(3) Tangent");
- System.out.println("(4) ArcSin");
- System.out.println("(5) ArcCos");
- System.out.println("(6) ArcTan");
- System.out.println("--------------------------------------------");
- System.out.print("> ");
- choice = stdin.nextInt();
-
- switch(choice){
- case 1: System.out.println("Calculating sine of " + num);
- result = Math.sin(num);
- isValid = true;
- break;
- case 2: System.out.println("Calculating cosine of " + num);
- result = Math.cos(num);
- isValid = true;
- break;
- case 3: System.out.println("Calculating tangent of " + num);
- result = Math.tan(num);
- isValid = true;
- break;
- case 4: System.out.println("Calculating inverse sine (arcSin) of " + num);
- result = Math.asin(num);
- isValid = true;
- break;
- case 5: System.out.println("Calculating inverse cosine (arcCos) of " + num);
- result = Math.acos(num);
- isValid = true;
- break;
- case 6: System.out.println("Calculating inverse tangent (arcTan) of " + num);
- result = Math.atan(num);
- isValid = true;
- break;
-
- }
- }
- return result;
- }
-
- public double advFunc(double num){ //take displayNum as input parameter
-
- boolean isValid = false;
- int choice;
- double result = 0;
-
- while(!isValid){
- System.out.println("\n-------------------------------------------------");
- System.out.println(num);
- System.out.println("-------------------------------------------------");
- System.out.println("Select advanced function to perform (enter # 1-7)");
- System.out.println("(1) Factorial");
- System.out.println("(2) Log");
- System.out.println("(3) Inverse log (10^x)");
- System.out.println("(4) Natural log (Ln x)");
- System.out.println("(5) Inverse Natural log (e^x)");
- System.out.println("(6) Cubed root"); //add custom 1
- System.out.println("(7) Absolute value");//add custom 2
- System.out.println("-------------------------------------------------");
- System.out.print("> ");
- choice = stdin.nextInt();
-
- switch(choice){
- case 1: System.out.println("Calculating factorial of " + num);
- int i;
- double fct = 1;
- for(i=2; i<num; i++){
- fct *= i;
- }
- result = fct;
- isValid = true;
- break;
- case 2: System.out.println("Calculating log of " + num);
- result = Math.log10(num);
- isValid = true;
- break;
- case 3: System.out.println("Calculating inverse log (10^x) of " + num);
- result = Math.pow(10 , Math.log10(num));
- isValid = true;
- break;
- case 4: System.out.println("Calculating natural log (Ln) of " + num);
- result = Math.log(num);
- isValid = true;
- break;
- case 5: System.out.println("Calculating inverese natural log (e^x) of " + num);
- result = Math.exp(num);
- isValid = true;
- break;
- case 6: System.out.println("Calculating cubed root of " + num);
- result = Math.cbrt(num);
- isValid = true;
- break;
- case 7: System.out.println("Calculating absolute value of " + num);
- result = Math.abs(num);
- isValid = true;
- break;
- default:
- break;
-
- }
- }
- return result;
- }
-
- }
|