123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
-
- /**
- * Write a description of class Operations here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- import java.util.Scanner;
- public class Operations
- {
- /**
- * prompting a user for a command solving for that command
- *
- */
- public static void printHelp(){
- System.out.println("\n? | + | - | * | / |");
- System.out.println("^ | sqrt | inverse |");
- System.out.println("cos | sine | tan | cos-1 | sin-1 | m+ | m- | MRC");
- System.out.println("log | ln | ex |");
- System.out.println("To exit the program enter exit");
- System.out.println("to clear display enter clrDisplay\n");
- }
-
- public static double runCommand()
- {
- // put your code here
-
- Scanner input = new Scanner(System.in);
- double result = 0.0;
- //String userInput = scanner.nextLine();
- scienticFunctions ScientificFunctions = new scienticFunctions();
- Memory memory = new Memory();
- DiplayMode displayMode = new DiplayMode();
- bonusFunctions bonusFunctions = new bonusFunctions();
- calculatorCoreFunctions calculatorCoreFunctions = new calculatorCoreFunctions();
- // Calculator calc = new ....
- while (true) {
- System.out.print("Enter a command: ");
- String command = input.next();
- if (command.equalsIgnoreCase("exit")) {
- System.out.print("Thanks for calculating with us!");
- break;
- }
- else if (command.equalsIgnoreCase("+")){
- if (result != 0){
- double numInput = Console.getDoubleInput("Enter number");
- result = calculatorCoreFunctions.add(result, numInput);
- }
- else{
- double firstNumber = Console.getDoubleInput("Enter first number");
- double secondNumber = Console.getDoubleInput("Enter second number");
- result = calculatorCoreFunctions.add(firstNumber, secondNumber);
- }
- Console.printResult(result);
-
- }
- else if (command.equalsIgnoreCase("-")){
- if (result != 0){
- double numInput = Console.getDoubleInput("Enter number");
- result = calculatorCoreFunctions.subtract(result, numInput);
- }
- else{
- double firstNumber = Console.getDoubleInput("Enter first number");
- double secondNumber = Console.getDoubleInput("Enter second number");
- result = calculatorCoreFunctions.subtract(firstNumber, secondNumber);;
- }
- Console.printResult(result);
- }
- else if (command.equalsIgnoreCase("*")){
- if (result != 0){
- double numInput = Console.getDoubleInput("Enter number");
- result = calculatorCoreFunctions.multiply(result, numInput);
- }
- else{
- double firstNumber = Console.getDoubleInput("Enter first number");
- double secondNumber = Console.getDoubleInput("Enter second number");
- result = calculatorCoreFunctions.multiply(firstNumber, secondNumber);;
- }
- Console.printResult(result);
- }
- else if (command.equalsIgnoreCase("/")){
- if (result != 0){
- double numInput = Console.getDoubleInput("Enter number");
- result = calculatorCoreFunctions.divide(result, numInput);
- }
- else{
- double firstNumber = Console.getDoubleInput("Enter first number");
- double secondNumber = Console.getDoubleInput("Enter second number");
- result = calculatorCoreFunctions.divide(firstNumber, secondNumber);;
- }
- Console.printResult(result);
- }
- else if (command.equalsIgnoreCase("^")){
- if (result != 0){
- double numInput = Console.getDoubleInput("Enter number");
- result = calculatorCoreFunctions.exponent(result, numInput);
- }
- else{
- double firstNumber = Console.getDoubleInput("Enter first number");
- double secondNumber = Console.getDoubleInput("Enter second number");
- result = calculatorCoreFunctions.exponent(firstNumber, secondNumber);;
- }
- Console.printResult(result);
- }
- else if (command.equalsIgnoreCase("sqrt")) {
- if (result != 0){
- result = calculatorCoreFunctions.sqrt(result);
- }
- else{
- result = calculatorCoreFunctions.sqrt(Console.getDoubleInput("Enter number"));
- }
- Console.printResult(result);
- }
- else if (command.equalsIgnoreCase("inverse")) {
- if (result != 0){
- result = calculatorCoreFunctions.inverse(result);
- }
- else{
- result = calculatorCoreFunctions.inverse(Console.getDoubleInput("Enter number"));
- }
- Console.printResult(result);
- }
- else if (command.equalsIgnoreCase("sine")) {
- if (result != 0){
- result = ScientificFunctions.sine(result);
- }
- else{
- result = ScientificFunctions.sine(Console.getDoubleInput("Enter number"));
- }
- Console.printResult(result);
- }
- else if (command.equalsIgnoreCase("cos")) {
- if (result != 0){
- result = ScientificFunctions.cosine(result);
- }
- else{
- result = ScientificFunctions.cosine(Console.getDoubleInput("Enter number"));
- }
- Console.printResult(result);
- }
- else if (command.equalsIgnoreCase("tan")) {
- if (result != 0){
- result = ScientificFunctions.tangent(result);
- }
- else{
- result = ScientificFunctions.tangent(Console.getDoubleInput("Enter number"));
- }
- Console.printResult(result);
- }
- else if (command.equalsIgnoreCase("cos-1")) {
- if (result != 0){
- result = ScientificFunctions.inverseCosine(result);
- }
- else{
- result = ScientificFunctions.inverseCosine(Console.getDoubleInput("Enter number"));
- }
- Console.printResult(result);
- }
- else if (command.equalsIgnoreCase("sin-1")) {
- if (result != 0){
- result = ScientificFunctions.inverseSine(result);
- }
- else{
- result = ScientificFunctions.inverseSine(Console.getDoubleInput("Enter number"));
- }
- Console.printResult(result);
- }
- else if (command.equalsIgnoreCase("tan-1")) {
- if (result != 0){
- result = ScientificFunctions.inverseTangent(result);
- }
- else{
- result = ScientificFunctions.inverseTangent(Console.getDoubleInput("Enter number"));
- }
- Console.printResult(result);
- }
- else if(command.equalsIgnoreCase("M+")){
- memory.storeInMemory(result);
- Console.printResult(result);
- }
-
- else if (command.equalsIgnoreCase("M-")){
- Console.printResult(memory.clearMemory());
- }
-
- else if (command.equalsIgnoreCase("MRC")){
- Console.printResult(memory.retrieveFromMemory());
- }
-
- else if (command.equalsIgnoreCase("switchmode")){
- displayMode.setResult(result);
- String mode = displayMode.enterMode();
- //System.out.println(mode);
- displayMode.switchDisplayMode(mode);
-
- }
- else if (command.equalsIgnoreCase("log")){
- Console.printResult(bonusFunctions.LogBaseTenOfAnumber(result));
- }
- else if (command.equalsIgnoreCase("ln")){
- Console.printResult(bonusFunctions.naturalLogLn(result));
- }
- else if (command.equalsIgnoreCase("ex")){
- Console.printResult(bonusFunctions.exponential(result));
- }
- else if(command.equalsIgnoreCase("?")){
- printHelp();
- }
- else if(command.equalsIgnoreCase("clrDisplay")){
- result = 0.0;
- }
- else{
- Console.println("Invalid command please press ? for help");
- }
- //firstNumber = result;
- // and on and on
- }
- return result;
-
- }
-
- //Add CommentCollapse
-
- }
-
|