Browse Source

updated print to console and added error for div by 0

Lauren Green 6 years ago
parent
commit
ad1a9d47c6
3 changed files with 49 additions and 32 deletions
  1. 24
    12
      Basic.java
  2. 20
    15
      Trig.java
  3. 5
    5
      package.bluej

+ 24
- 12
Basic.java View File

@@ -1,4 +1,4 @@
1
-import java.util.Scanner;
1
+
2 2
 
3 3
 /**
4 4
  * Basic class to perform basic functions for our Graphing Calculator
@@ -14,45 +14,57 @@ public class Basic
14 14
     
15 15
     public Basic()
16 16
     {
17
+        //a check to make sure user input is valid.
17 18
         boolean validInput = false;
18 19
         
19 20
         while(!validInput) {
20 21
         //Get input of first number
21 22
         input1 = Console.getIntegerInput("What is the first number?");
22 23
             
23
-        //Get input of type of function
24
+        //Get input of operation
24 25
         String operation = Console.getStringInput("Which operation would you like to perform? \n +, -, *, /");
25 26
   
26 27
         //Get input of second number
27 28
         input2 = Console.getIntegerInput("What is the second number?");
28 29
         
29
-        //Do the math depending on the input.
30
+        //Do the math depending on the inputs.
31
+        //addition
30 32
         if (operation.equals("+")) {
31 33
             answer = input1 + input2;
32
-            System.out.println(answer);
34
+            Console.println(Double.toString(answer));
33 35
             validInput = true;
34
-            
36
+        
37
+        //subtraction    
35 38
         } else if (operation.equals("-")) {
36 39
             answer = input1 - input2;
37
-            System.out.println(answer);
40
+            Console.println(Double.toString(answer));
38 41
             validInput = true;
39
-            
42
+        
43
+        //multiplication
40 44
         } else if (operation.equals("*")) {
41 45
             answer = input1 * input2;
42
-            System.out.println(answer);
46
+            Console.println(Double.toString(answer));
43 47
             validInput = true;
44 48
             
49
+        //division    
45 50
         } else if (operation.equals("/")) {
51
+            //error message when trying to divide by 0.
52
+            if (input2 == 0) {
53
+                Console.println("undefined");
54
+                //Insert return to main menu.
55
+            } else {
46 56
             answer = input1 / input2;
47
-            System.out.println(answer);
57
+            Console.println(Double.toString(answer));
48 58
             validInput = true;
49
-            
59
+        }
50 60
         } else {
51
-            System.out.println("Error, try again \n");
61
+            //error if wrong input was entered
62
+            Console.println("Error, try again \n");
52 63
             
53 64
     }
65
+    
54 66
 }
55
-    //Insert return to main menu.
67
+         //Insert return to main menu.
56 68
 }
57 69
 }
58 70
 

+ 20
- 15
Trig.java View File

@@ -1,5 +1,4 @@
1 1
 
2
-import java.util.Scanner;
3 2
 
4 3
 /**
5 4
  * Trig class to perform trig function for our Graphing Calculator
@@ -14,6 +13,7 @@ public class Trig
14 13
     
15 14
     public Trig()
16 15
     {
16
+        //a check to make sure user input is valid
17 17
         boolean validInput = false;
18 18
         
19 19
         while(!validInput) {
@@ -25,58 +25,63 @@ public class Trig
25 25
         int input = Console.getIntegerInput("What number would you like to find the " + operation + " of?");
26 26
         
27 27
         //Do the math depending on the input.
28
+        //SIN
28 29
         if (operation.equals("sin")) {
29 30
             answer = Math.sin(input);
30
-            System.out.println(answer);
31
+            Console.println(Double.toString(answer));
31 32
             validInput = true;
32 33
             
34
+        //COS
33 35
         } else if (operation.equals("cos")) {
34 36
             answer = Math.cos(input);
35
-            System.out.println(answer);
37
+            Console.println(Double.toString(answer));
36 38
             validInput = true;
37 39
             
40
+        //TAN
38 41
         } else if (operation.equals("tan")) {
39 42
             answer = Math.tan(input);
40
-            System.out.println(answer);
43
+            Console.println(Double.toString(answer));
41 44
             validInput = true;
42 45
             
46
+        //ARCSIN
43 47
         } else if (operation.equals("arcsin")) {
44 48
             answer = Math.asin(input);
45
-            System.out.println(answer);
49
+            Console.println(Double.toString(answer));
46 50
             validInput = true;
47 51
             
52
+        //ARCCOS
48 53
         } else if (operation.equals("arccos")) {
49 54
             answer = Math.acos(input);
50
-            System.out.println(answer);
55
+            Console.println(Double.toString(answer));
51 56
             validInput = true;
52 57
             
58
+        //ARCTAN
53 59
         } else if (operation.equals("arctan")) {
54 60
             answer = Math.atan(input);
55
-            System.out.println(answer);
61
+            Console.println(Double.toString(answer));
56 62
             validInput = true;
57
-            
63
+           
64
+        //Error message if input does not match options, loops back to the top so they can try again.
58 65
         } else {
59
-            System.out.println("Error, try again \n");
66
+            Console.println("Error, try again \n");
60 67
             
61 68
     }
62 69
 }
63 70
         //Ask if they would like to convert.
64
-        System.out.println("Would you like to convert your answer from radian to degrees?  Y or N");
65
-        Scanner in3 = new Scanner(System.in);
66
-        String response = in3.nextLine();
71
+        String response = Console.getStringInput("Would you like to convert your answer from radian to degrees?  Y or N");
67 72
         response = response.toLowerCase();
68 73
         if (response.equals("y")) {
69 74
             toDegrees(answer);
70 75
     } else {
71
-        System.out.println("Have a nice day!");
76
+        Console.println("Have a nice day!");
72 77
         //Insert Return to Main Menu
73 78
 }
74 79
     }
75
-
80
+    //Converting from Radians to Degrees
76 81
     public void toDegrees(double answer) 
77 82
     {
78 83
         double conversion = Math.toDegrees(answer);
79
-        System.out.println(conversion);
84
+        Console.println(Double.toString(conversion));
80 85
         //Insert Return to Main Menu
81 86
     }
82 87
 }

+ 5
- 5
package.bluej View File

@@ -2,21 +2,21 @@
2 2
 dependency1.from=MainApplication
3 3
 dependency1.to=Console
4 4
 dependency1.type=UsesDependency
5
-dependency2.from=Trig
5
+dependency2.from=AdvCalc
6 6
 dependency2.to=Console
7 7
 dependency2.type=UsesDependency
8
-dependency3.from=Basic
8
+dependency3.from=Adv
9 9
 dependency3.to=Console
10 10
 dependency3.type=UsesDependency
11
-dependency4.from=AdvCalc
11
+dependency4.from=Basic
12 12
 dependency4.to=Console
13 13
 dependency4.type=UsesDependency
14
-dependency5.from=Adv
14
+dependency5.from=Trig
15 15
 dependency5.to=Console
16 16
 dependency5.type=UsesDependency
17 17
 editor.fx.0.height=958
18 18
 editor.fx.0.width=904
19
-editor.fx.0.x=201
19
+editor.fx.0.x=759
20 20
 editor.fx.0.y=23
21 21
 objectbench.height=214
22 22
 objectbench.width=706