Parcourir la source

create multiply method useing get number to check for erros

Jonathan Hinds il y a 6 ans
Parent
révision
c9eccc187e
3 fichiers modifiés avec 35 ajouts et 50 suppressions
  1. 20
    12
      Calculations.java
  2. 4
    2
      MainApplication.java
  3. 11
    36
      package.bluej

+ 20
- 12
Calculations.java Voir le fichier

6
 {
6
 {
7
     public static Console console = new Console();
7
     public static Console console = new Console();
8
     public static ArrayList<String> library = new ArrayList<String>();
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
     public Calculations(){
11
     public Calculations(){
11
    
12
    
28
         
29
         
29
     }
30
     }
30
 
31
 
31
-    public static void getCommand(String userInput)
32
+    public static String getCommand(String userInput)
32
     {
33
     {
33
         boolean foundCommand = false;
34
         boolean foundCommand = false;
34
         String command = "";
35
         String command = "";
76
                  Clear();
77
                  Clear();
77
             break;
78
             break;
78
         }
79
         }
79
-        System.out.println(result);
80
-        //return result;
80
+        return result;
81
     }
81
     }
82
     
82
     
83
     public static ArrayList<Double> getNumbers(String userInput){
83
     public static ArrayList<Double> getNumbers(String userInput){
84
         ArrayList<Double> results = new ArrayList<Double>();
84
         ArrayList<Double> results = new ArrayList<Double>();
85
+        
85
         //takes in a string
86
         //takes in a string
86
         //finds the first occurence of a number
87
         //finds the first occurence of a number
87
         //store it as a double ( left )
88
         //store it as a double ( left )
92
         //if the size of the arraylist is 2
93
         //if the size of the arraylist is 2
93
             //return the array list
94
             //return the array list
94
         //else return null'
95
         //else return null'
96
+        
95
         Pattern pattern = Pattern.compile("\\s?\\.?\\d+\\.?(\\d+)?\\s?");
97
         Pattern pattern = Pattern.compile("\\s?\\.?\\d+\\.?(\\d+)?\\s?");
96
         Matcher matcher = pattern.matcher(userInput);
98
         Matcher matcher = pattern.matcher(userInput);
97
         
99
         
101
             results.add(deci);
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
     public static String Multiply(String userInput)
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
     public static String Divide(String userInput)
127
     public static String Divide(String userInput)

+ 4
- 2
MainApplication.java Voir le fichier

18
     }
18
     }
19
     
19
     
20
     public void Run(){
20
     public void Run(){
21
-        Scanner scanner = new Scanner(System.in);       
21
+        Scanner scanner = new Scanner(System.in); 
22
+        String result = "";
22
         while(this.on == true)
23
         while(this.on == true)
23
         {
24
         {
24
             String userInput = scanner.nextLine();
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 Voir le fichier

8
 dependency3.from=Calculations
8
 dependency3.from=Calculations
9
 dependency3.to=Console
9
 dependency3.to=Console
10
 dependency3.type=UsesDependency
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
 editor.fx.0.x=0
13
 editor.fx.0.x=0
30
 editor.fx.0.y=0
14
 editor.fx.0.y=0
31
-editor.fx.0.height=717
32
 objectbench.height=200
15
 objectbench.height=200
33
 objectbench.width=595
16
 objectbench.width=595
34
 package.divider.horizontal=0.6
17
 package.divider.horizontal=0.6
35
 package.divider.vertical=0.6854103343465046
18
 package.divider.vertical=0.6854103343465046
36
-package.editor.height=444
19
+package.editor.height=428
37
 package.editor.width=493
20
 package.editor.width=493
38
 package.editor.x=35
21
 package.editor.x=35
39
 package.editor.y=23
22
 package.editor.y=23
50
 readme.x=10
33
 readme.x=10
51
 readme.y=10
34
 readme.y=10
52
 target1.height=50
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
 target1.name=Calculations
36
 target1.name=Calculations
60
 target1.showInterface=false
37
 target1.showInterface=false
61
 target1.type=ClassTarget
38
 target1.type=ClassTarget
62
 target1.width=100
39
 target1.width=100
63
-target1.x=120
64
-target1.y=150
40
+target1.x=310
41
+target1.y=260
65
 target2.height=50
42
 target2.height=50
66
 target2.name=Console
43
 target2.name=Console
67
 target2.showInterface=false
44
 target2.showInterface=false
68
 target2.type=ClassTarget
45
 target2.type=ClassTarget
69
 target2.width=80
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
 target3.name=MainApplication
50
 target3.name=MainApplication
76
 target3.showInterface=false
51
 target3.showInterface=false
77
 target3.type=ClassTarget
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