Kaynağa Gözat

create multiply method useing get number to check for erros

Jonathan Hinds 6 yıl önce
ebeveyn
işleme
c9eccc187e
3 değiştirilmiş dosya ile 35 ekleme ve 50 silme
  1. 20
    12
      Calculations.java
  2. 4
    2
      MainApplication.java
  3. 11
    36
      package.bluej

+ 20
- 12
Calculations.java Dosyayı Görüntüle

@@ -6,6 +6,7 @@ public class Calculations
6 6
 {
7 7
     public static Console console = new Console();
8 8
     public static ArrayList<String> library = new ArrayList<String>();
9
+    public static String invalidArgumentsAmountError = "You have entered an invalid amount of arguments. Please only use two arguments, or switch to PEMDAS";
9 10
     
10 11
     public Calculations(){
11 12
    
@@ -28,7 +29,7 @@ public class Calculations
28 29
         
29 30
     }
30 31
 
31
-    public static void getCommand(String userInput)
32
+    public static String getCommand(String userInput)
32 33
     {
33 34
         boolean foundCommand = false;
34 35
         String command = "";
@@ -76,12 +77,12 @@ public class Calculations
76 77
                  Clear();
77 78
             break;
78 79
         }
79
-        System.out.println(result);
80
-        //return result;
80
+        return result;
81 81
     }
82 82
     
83 83
     public static ArrayList<Double> getNumbers(String userInput){
84 84
         ArrayList<Double> results = new ArrayList<Double>();
85
+        
85 86
         //takes in a string
86 87
         //finds the first occurence of a number
87 88
         //store it as a double ( left )
@@ -92,6 +93,7 @@ public class Calculations
92 93
         //if the size of the arraylist is 2
93 94
             //return the array list
94 95
         //else return null'
96
+        
95 97
         Pattern pattern = Pattern.compile("\\s?\\.?\\d+\\.?(\\d+)?\\s?");
96 98
         Matcher matcher = pattern.matcher(userInput);
97 99
         
@@ -101,19 +103,25 @@ public class Calculations
101 103
             results.add(deci);
102 104
         }
103 105
         
104
-        return results;
106
+        if(results.size() == 2)
107
+        { 
108
+            return results;
109
+        }
110
+        return null;
105 111
     }
106 112
     
107 113
     public static String Multiply(String userInput)
108 114
     {
109
-        //get getNumbers
110
-        //perform the operation
111
-        //convert to string
112
-        //return string
113
-        
114
-        
115
-        
116
-        return null;
115
+        String result = "";
116
+        ArrayList<Double> results = getNumbers(userInput);
117
+        if(results != null)
118
+        {
119
+            Double product = results.get(0) * results.get(1);
120
+            result = String.valueOf(product);
121
+            return result;
122
+        } else {
123
+            return invalidArgumentsAmountError;
124
+        }
117 125
     }
118 126
 
119 127
     public static String Divide(String userInput)

+ 4
- 2
MainApplication.java Dosyayı Görüntüle

@@ -18,11 +18,13 @@ public class MainApplication {
18 18
     }
19 19
     
20 20
     public void Run(){
21
-        Scanner scanner = new Scanner(System.in);       
21
+        Scanner scanner = new Scanner(System.in); 
22
+        String result = "";
22 23
         while(this.on == true)
23 24
         {
24 25
             String userInput = scanner.nextLine();
25
-            calculator.getCommand(userInput); 
26
+            result = calculator.getCommand(userInput);
27
+            System.out.println(result);
26 28
         }
27 29
     }
28 30
     

+ 11
- 36
package.bluej Dosyayı Görüntüle

@@ -8,32 +8,15 @@ dependency2.type=UsesDependency
8 8
 dependency3.from=Calculations
9 9
 dependency3.to=Console
10 10
 dependency3.type=UsesDependency
11
-editor.fx.0.height=716
12
-editor.fx.0.height=709
13
-editor.fx.0.width=800
14
-editor.fx.0.x=480
15
-editor.fx.0.y=23
16
-objectbench.height=198
17
-objectbench.width=595
18
-package.divider.horizontal=0.6
19
-package.divider.vertical=0.685099846390169
20
-package.editor.height=439
21
-package.editor.width=493
22
-package.editor.x=115
23
-package.editor.y=24
24
-package.frame.height=709
25
-package.frame.width=619
26
-package.numDependencies=0
27
-package.numTargets=2
28
-editor.fx.0.width=0
11
+editor.fx.0.height=800
12
+editor.fx.0.width=1280
29 13
 editor.fx.0.x=0
30 14
 editor.fx.0.y=0
31
-editor.fx.0.height=717
32 15
 objectbench.height=200
33 16
 objectbench.width=595
34 17
 package.divider.horizontal=0.6
35 18
 package.divider.vertical=0.6854103343465046
36
-package.editor.height=444
19
+package.editor.height=428
37 20
 package.editor.width=493
38 21
 package.editor.x=35
39 22
 package.editor.y=23
@@ -50,31 +33,23 @@ readme.width=47
50 33
 readme.x=10
51 34
 readme.y=10
52 35
 target1.height=50
53
-target1.name=OrderOfOperations
54
-target1.showInterface=false
55
-target1.type=ClassTarget
56
-target1.width=140
57
-target1.x=70
58
-target1.y=70
59 36
 target1.name=Calculations
60 37
 target1.showInterface=false
61 38
 target1.type=ClassTarget
62 39
 target1.width=100
63
-target1.x=120
64
-target1.y=150
40
+target1.x=310
41
+target1.y=260
65 42
 target2.height=50
66 43
 target2.name=Console
67 44
 target2.showInterface=false
68 45
 target2.type=ClassTarget
69 46
 target2.width=80
70
-target2.x=80
71
-target2.y=200
72
-target2.x=20
73
-target2.y=150
74
-target3.height=50
47
+target2.x=190
48
+target2.y=160
49
+target3.height=120
75 50
 target3.name=MainApplication
76 51
 target3.showInterface=false
77 52
 target3.type=ClassTarget
78
-target3.width=120
79
-target3.x=70
80
-target3.y=70
53
+target3.width=230
54
+target3.x=320
55
+target3.y=20