Browse Source

added display modes to switch statement and wrote some of display mode methods

Margaret Pierse 6 years ago
parent
commit
fbd84e3da2
2 changed files with 92 additions and 17 deletions
  1. 37
    7
      Calculator.java
  2. 55
    10
      MathMethods.java

+ 37
- 7
Calculator.java View File

@@ -1,13 +1,14 @@
1 1
 import java.util.*;
2
-class Calculator{
2
+public class Calculator{
3
+public static double memory = 0;
3 4
     public static void main(String args[]){
4
-
5
+        
5 6
         String toDo;                //what the user wants to do
6 7
         double num1 = 0;                //The user's first input
7 8
         double num2 = 0;            //The user's second input
8 9
         double result = 0;          //The user's answer
9 10
         boolean off = false;        //The user typed quit, becomes true
10
-        double memory;
11
+
11 12
         boolean error = false;
12 13
         MathMethods calc = new MathMethods();
13 14
 
@@ -21,13 +22,25 @@ class Calculator{
21 22
                 System.out.println("Goodbye!!");
22 23
 
23 24
             } else {
24
-
25
+                switch (toDo) {
25 26
                 //System.out.println("toDO is: " + toDo);
26
-
27
+                    case "add":
28
+                    case "subtract":
29
+                    case "multiply":
30
+                    case "divide":
31
+                    case "exponent":
32
+                    case "cosine" :
33
+                    case "inverse cosine" :
34
+                    case "square root" :
35
+                    case "invert" :
36
+                    case "sine" :
37
+                    case "inverse sine" :
38
+                    case "degree to radian" :
39
+                    case "radian to degree" :
27 40
                 System.out.println(toDo + " -- Enter first number:");
28 41
                 num1 = input1.nextDouble();
29 42
                 //System.out.println("num1 is: " + num1);
30
-
43
+            }
31 44
                 switch (toDo){
32 45
                     case "add":
33 46
                     case "subtract":
@@ -118,9 +131,26 @@ class Calculator{
118 131
                     memory = result;
119 132
                     calc.printAns(String.valueOf(result));
120 133
                     break;
121
-                    
134
+
122 135
                     case "fortune cookie":
136
+                    calc.printAns(calc.fortuneCookie());
137
+                    break;
138
+
139
+                    case "display mode decimal":
140
+                    calc.printAns(String.valueOf(calc.switchDisplayMode("decimal")));
141
+                    break;
123 142
                     
143
+                    case "display mode hexadecimal":
144
+                    calc.printAns(calc.switchDisplayMode("hexadecimal"));
145
+                    
146
+                    case "display mode octal":
147
+                    calc.printAns(String.valueOf(calc.switchDisplayMode("octal")));
148
+                    break;
149
+                    
150
+                    case "display mode binary":
151
+                    calc.printAns(String.valueOf(calc.switchDisplayMode("binary")));
152
+                    break;
153
+
124 154
                 }
125 155
             }
126 156
 

+ 55
- 10
MathMethods.java View File

@@ -1,4 +1,5 @@
1 1
 import java.util.*;
2
+import java.lang.*;
2 3
 /**
3 4
  * Write a description of class Calculator here.
4 5
  *
@@ -70,15 +71,63 @@ public class MathMethods
70 71
     }
71 72
 
72 73
     public static String fortuneCookie() {
73
-     Random rand = new Random();
74
+        Random rand = new Random();
74 75
         String[] wiseWords = new String[] {"You're a hard worker and you will do great on all the labs!", 
75
-         "Look how much java you learned this week! That's awesome!", "Keep up the good work!" ,
76
-        "You're cushing it!", "You're a coding rockstar!", 
77
-        "Keep coding!"};  
78
-        int arrIndex = rand.nextInt(6)+1;
76
+                "Look how much java you learned this week! That's awesome!", 
77
+                "Keep up the good work!" ,
78
+                "You're cushing it!", "You're a coding rockstar!", 
79
+                "Keep coding!"};  
80
+        int arrIndex = rand.nextInt(6);
79 81
         return wiseWords[arrIndex];
80 82
     }
81
-    
83
+
84
+    public static String switchDisplayMode(String mode){
85
+        Calculator display = new Calculator();
86
+        //Double d = new Double(display.memory);
87
+        String answer = "";
88
+        switch (mode) {
89
+            case "binary":
90
+            answer = "";
91
+            break;
92
+
93
+            case "octal":
94
+            answer = "";
95
+            break;
96
+
97
+            case "decimal":
98
+            answer = "";
99
+            break;
100
+
101
+            case "hexadecimal":
102
+            answer = Double.toHexString(display.memory);
103
+            break;
104
+
105
+        }
106
+        return answer;
107
+    }
108
+
109
+    // public static double switchDisplayMode(){
110
+        // int i = 4;
111
+        // double answer = 0;
112
+
113
+        // if (i%4 == 0){ 
114
+            // answer = switchDisplayMode("binary");
115
+            // i++;
116
+        // } 
117
+        // else if (i%4 == 1){
118
+            // answer = switchDisplayMode("octal");
119
+            // i++;
120
+        // } 
121
+        // else if (i%4 == 2) {
122
+            // answer = switchDisplayMode("hecadecimal");
123
+            // i++;
124
+        // } else {
125
+            // answer = switchDisplayMode("decimal");
126
+            // i++;
127
+        // }
128
+        // return answer;
129
+    // }
130
+
82 131
     public static void printAns(String printAnswer){
83 132
         System.out.println("ANSWER: " + printAnswer);
84 133
 
@@ -87,7 +136,3 @@ public class MathMethods
87 136
 }
88 137
 
89 138
 
90
-    
91
-    
92
-    
93
-