Преглед изворни кода

updated print to console and added error for div by 0

Lauren Green пре 6 година
родитељ
комит
ad1a9d47c6
3 измењених фајлова са 49 додато и 32 уклоњено
  1. 24
    12
      Basic.java
  2. 20
    15
      Trig.java
  3. 5
    5
      package.bluej

+ 24
- 12
Basic.java Прегледај датотеку

1
-import java.util.Scanner;
1
+
2
 
2
 
3
 /**
3
 /**
4
  * Basic class to perform basic functions for our Graphing Calculator
4
  * Basic class to perform basic functions for our Graphing Calculator
14
     
14
     
15
     public Basic()
15
     public Basic()
16
     {
16
     {
17
+        //a check to make sure user input is valid.
17
         boolean validInput = false;
18
         boolean validInput = false;
18
         
19
         
19
         while(!validInput) {
20
         while(!validInput) {
20
         //Get input of first number
21
         //Get input of first number
21
         input1 = Console.getIntegerInput("What is the first number?");
22
         input1 = Console.getIntegerInput("What is the first number?");
22
             
23
             
23
-        //Get input of type of function
24
+        //Get input of operation
24
         String operation = Console.getStringInput("Which operation would you like to perform? \n +, -, *, /");
25
         String operation = Console.getStringInput("Which operation would you like to perform? \n +, -, *, /");
25
   
26
   
26
         //Get input of second number
27
         //Get input of second number
27
         input2 = Console.getIntegerInput("What is the second number?");
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
         if (operation.equals("+")) {
32
         if (operation.equals("+")) {
31
             answer = input1 + input2;
33
             answer = input1 + input2;
32
-            System.out.println(answer);
34
+            Console.println(Double.toString(answer));
33
             validInput = true;
35
             validInput = true;
34
-            
36
+        
37
+        //subtraction    
35
         } else if (operation.equals("-")) {
38
         } else if (operation.equals("-")) {
36
             answer = input1 - input2;
39
             answer = input1 - input2;
37
-            System.out.println(answer);
40
+            Console.println(Double.toString(answer));
38
             validInput = true;
41
             validInput = true;
39
-            
42
+        
43
+        //multiplication
40
         } else if (operation.equals("*")) {
44
         } else if (operation.equals("*")) {
41
             answer = input1 * input2;
45
             answer = input1 * input2;
42
-            System.out.println(answer);
46
+            Console.println(Double.toString(answer));
43
             validInput = true;
47
             validInput = true;
44
             
48
             
49
+        //division    
45
         } else if (operation.equals("/")) {
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
             answer = input1 / input2;
56
             answer = input1 / input2;
47
-            System.out.println(answer);
57
+            Console.println(Double.toString(answer));
48
             validInput = true;
58
             validInput = true;
49
-            
59
+        }
50
         } else {
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 Прегледај датотеку

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

+ 5
- 5
package.bluej Прегледај датотеку

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