Przeglądaj źródła

added factorial method

Michelle DiMarino 6 lat temu
rodzic
commit
c4d9abf6d7
4 zmienionych plików z 106 dodań i 99 usunięć
  1. 4
    1
      Console.java
  2. 9
    1
      RealAdvanced.java
  3. 8
    0
      RealAdvancedTest.java
  4. 85
    97
      package.bluej

+ 4
- 1
Console.java Wyświetl plik

@@ -118,8 +118,11 @@ public class Console {
118 118
                     advAnswer = advCalc.absoluteValue(advInput1);
119 119
                 }else if (mode.equals("6")){
120 120
                     advAnswer = advCalc.sqrt(advInput1);
121
+                }else if (mode.equals("7")){
122
+                    advAnswer = advCalc.factorial(advInput1);
123
+             
121 124
 
122
-                }else if (mode.equals("7")) {
125
+                }else if (mode.equals("8")) {
123 126
                     break;
124 127
                 }else{
125 128
                     Console.println("Invalid input");

+ 9
- 1
RealAdvanced.java Wyświetl plik

@@ -20,7 +20,8 @@ public class RealAdvanced
20 20
             "4: Switch Sign of x" + "\n"+
21 21
             "5: Absolute Value of x" + "\n"+
22 22
             "6: Square Root of x" + "\n"+
23
-            "7: Return to Main Menu");
23
+            "7: Factorial x!"+"\n"+
24
+            "8: Return to Main Menu");
24 25
 
25 26
     }
26 27
     // method to find x^2
@@ -46,4 +47,11 @@ public class RealAdvanced
46 47
 public double sqrt(double x){
47 48
     return Math.sqrt(x);
48 49
 }
50
+public double factorial(double x){
51
+    double answer = 1;
52
+    for (double i = x; i >0; i--){
53
+        answer = answer * i;
54
+    }
55
+    return answer;
56
+}
49 57
 }

+ 8
- 0
RealAdvancedTest.java Wyświetl plik

@@ -81,6 +81,13 @@ public class RealAdvancedTest
81 81
         RealAdvanced realAdva1 = new RealAdvanced();
82 82
         assertEquals(5.0, realAdva1.sqrt(25), 0.1);
83 83
     }
84
+
85
+    @Test
86
+    public void factorialTest()
87
+    {
88
+        RealAdvanced realAdva1 = new RealAdvanced();
89
+        assertEquals(120.0, realAdva1.factorial(5), 0.1);
90
+    }
84 91
 }
85 92
 
86 93
 
@@ -88,3 +95,4 @@ public class RealAdvancedTest
88 95
 
89 96
 
90 97
 
98
+

+ 85
- 97
package.bluej Wyświetl plik

@@ -1,104 +1,71 @@
1 1
 #BlueJ package file
2
-dependency1.from=MainMenu
2
+dependency1.from=Basic
3 3
 dependency1.to=Console
4 4
 dependency1.type=UsesDependency
5 5
 dependency10.from=Console
6
-<<<<<<< HEAD
7
-dependency10.to=RealAdvanced
6
+dependency10.to=MainMenu
8 7
 dependency10.type=UsesDependency
9 8
 dependency11.from=Console
10
-dependency11.to=SciCalc
9
+dependency11.to=Basic
11 10
 dependency11.type=UsesDependency
12 11
 dependency12.from=Console
13
-dependency12.to=MemoryFunc
12
+dependency12.to=RealAdvanced
14 13
 dependency12.type=UsesDependency
15 14
 dependency13.from=Console
16
-dependency13.to=SwitchDisplay
15
+dependency13.to=SciCalc
17 16
 dependency13.type=UsesDependency
18 17
 dependency14.from=Console
19
-dependency14.to=Trig
20
-dependency14.type=UsesDependency
21
-dependency15.from=SwitchDisplay
22
-dependency15.to=Console
23
-dependency15.type=UsesDependency
24
-dependency2.from=SciCalc
25
-dependency2.to=Console
26
-dependency2.type=UsesDependency
27
-dependency3.from=MainApplication
28
-=======
29
-dependency10.to=Basic
30
-dependency10.type=UsesDependency
31
-dependency11.from=Console
32
-dependency11.to=RealAdvanced
33
-dependency11.type=UsesDependency
34
-dependency12.from=Console
35
-dependency12.to=SciCalc
36
-dependency12.type=UsesDependency
37
-dependency13.from=Console
38
-dependency13.to=MemoryFunc
39
-dependency13.type=UsesDependency
40
-dependency14.from=Console
41
-dependency14.to=SwitchDisplay
18
+dependency14.to=MemoryFunc
42 19
 dependency14.type=UsesDependency
43 20
 dependency15.from=Console
44
-dependency15.to=Trig
21
+dependency15.to=SwitchDisplay
45 22
 dependency15.type=UsesDependency
46
-dependency16.from=RealAdvancedTest
47
-dependency16.to=RealAdvanced
23
+dependency16.from=Console
24
+dependency16.to=Trig
48 25
 dependency16.type=UsesDependency
49
-dependency2.from=SciCalc
50
-dependency2.to=Console
26
+dependency17.from=RealAdvancedTest
27
+dependency17.to=RealAdvanced
28
+dependency17.type=UsesDependency
29
+dependency2.from=TrigTest
30
+dependency2.to=Trig
51 31
 dependency2.type=UsesDependency
52
-dependency3.from=MemoryFunc
53
->>>>>>> 49d5555735b5e26b8076429d9dd1666b4918738c
54
->>>>>>> 3fa2b863a93d1b5548a82c34b3920f059a52833d
55
-dependency3.to=Console
32
+dependency3.from=BasicTest
33
+dependency3.to=Basic
56 34
 dependency3.type=UsesDependency
57
-dependency4.from=Trig
35
+dependency4.from=MainMenu
58 36
 dependency4.to=Console
59 37
 dependency4.type=UsesDependency
60
-dependency5.from=Basic
38
+dependency5.from=SciCalc
61 39
 dependency5.to=Console
62 40
 dependency5.type=UsesDependency
63
-<<<<<<< HEAD
64
-dependency6.from=RealAdvanced
65
-dependency6.to=Console
66
-dependency6.type=UsesDependency
67
-dependency7.from=MemoryFunc
68
-=======
69
-dependency6.from=SwitchDisplay
41
+dependency6.from=MemoryFunc
70 42
 dependency6.to=Console
71 43
 dependency6.type=UsesDependency
72
-dependency7.from=MainMenu
73
->>>>>>> 3fa2b863a93d1b5548a82c34b3920f059a52833d
44
+dependency7.from=Trig
74 45
 dependency7.to=Console
75 46
 dependency7.type=UsesDependency
76
-dependency8.from=Console
77
-dependency8.to=MainMenu
47
+dependency8.from=SwitchDisplay
48
+dependency8.to=Console
78 49
 dependency8.type=UsesDependency
79
-dependency9.from=Console
80
-<<<<<<< HEAD
81
-dependency9.to=Basic
82
-=======
83
-dependency9.to=MainMenu
84
->>>>>>> 3fa2b863a93d1b5548a82c34b3920f059a52833d
50
+dependency9.from=RealAdvanced
51
+dependency9.to=Console
85 52
 dependency9.type=UsesDependency
86
-editor.fx.0.height=749
53
+editor.fx.0.height=709
87 54
 editor.fx.0.width=1215
88
-editor.fx.0.x=144
89
-editor.fx.0.y=72
90
-objectbench.height=214
55
+editor.fx.0.x=60
56
+editor.fx.0.y=24
57
+objectbench.height=198
91 58
 objectbench.width=595
92 59
 package.divider.horizontal=0.6
93
-package.divider.vertical=0.6847360912981455
94
-package.editor.height=473
60
+package.divider.vertical=0.685099846390169
61
+package.editor.height=439
95 62
 package.editor.width=493
96 63
 package.editor.x=35
97
-package.editor.y=59
98
-package.frame.height=759
64
+package.editor.y=23
65
+package.frame.height=709
99 66
 package.frame.width=619
100
-package.numDependencies=15
101
-package.numTargets=9
67
+package.numDependencies=17
68
+package.numTargets=12
102 69
 package.showExtends=true
103 70
 package.showUses=true
104 71
 project.charset=UTF-8
@@ -114,59 +81,80 @@ target1.type=ClassTarget
114 81
 target1.width=80
115 82
 target1.x=320
116 83
 target1.y=180
84
+target10.height=50
85
+target10.name=Trig
86
+target10.showInterface=false
87
+target10.type=ClassTarget
88
+target10.width=80
89
+target10.x=320
90
+target10.y=70
91
+target11.height=50
92
+target11.name=SwitchDisplay
93
+target11.showInterface=false
94
+target11.type=ClassTarget
95
+target11.width=110
96
+target11.x=80
97
+target11.y=350
98
+target12.height=50
99
+target12.name=MainApplication
100
+target12.showInterface=false
101
+target12.type=ClassTarget
102
+target12.width=120
103
+target12.x=70
104
+target12.y=70
117 105
 target2.height=50
118
-target2.name=SciCalc
106
+target2.name=TrigTest
119 107
 target2.showInterface=false
120
-target2.type=ClassTarget
108
+target2.type=UnitTestTargetJunit4
121 109
 target2.width=80
122
-target2.x=10
123
-target2.y=280
110
+target2.x=200
111
+target2.y=70
124 112
 target3.height=50
125
-target3.name=RealAdvanced
113
+target3.name=BasicTest
126 114
 target3.showInterface=false
127
-target3.type=ClassTarget
128
-target3.width=110
129
-target3.x=170
130
-target3.y=10
115
+target3.type=UnitTestTargetJunit4
116
+target3.width=90
117
+target3.x=160
118
+target3.y=130
131 119
 target4.height=50
132
-target4.name=MemoryFunc
120
+target4.name=RealAdvancedTest
133 121
 target4.showInterface=false
134
-target4.type=ClassTarget
135
-target4.width=110
136
-target4.x=210
137
-target4.y=350
122
+target4.type=UnitTestTargetJunit4
123
+target4.width=140
124
+target4.x=100
125
+target4.y=230
138 126
 target5.height=50
139
-target5.name=Console
127
+target5.name=MainMenu
140 128
 target5.showInterface=false
141 129
 target5.type=ClassTarget
142
-target5.width=80
130
+target5.width=90
143 131
 target5.x=70
144
-target5.y=170
132
+target5.y=10
145 133
 target6.height=50
146
-target6.name=Trig
134
+target6.name=SciCalc
147 135
 target6.showInterface=false
148 136
 target6.type=ClassTarget
149 137
 target6.width=80
150
-target6.x=320
151
-target6.y=70
138
+target6.x=10
139
+target6.y=280
152 140
 target7.height=50
153
-target7.name=SwitchDisplay
141
+target7.name=RealAdvanced
154 142
 target7.showInterface=false
155 143
 target7.type=ClassTarget
156 144
 target7.width=110
157
-target7.x=80
158
-target7.y=350
145
+target7.x=170
146
+target7.y=10
159 147
 target8.height=50
160
-target8.name=MainMenu
148
+target8.name=Console
161 149
 target8.showInterface=false
162 150
 target8.type=ClassTarget
163
-target8.width=90
151
+target8.width=80
164 152
 target8.x=70
165
-target8.y=10
153
+target8.y=170
166 154
 target9.height=50
167
-target9.name=MainApplication
155
+target9.name=MemoryFunc
168 156
 target9.showInterface=false
169 157
 target9.type=ClassTarget
170
-target9.width=120
171
-target9.x=70
172
-target9.y=70
158
+target9.width=110
159
+target9.x=210
160
+target9.y=350