Kaynağa Gözat

updated commit

Joshua Chung 6 yıl önce
ebeveyn
işleme
a1af773a23
5 değiştirilmiş dosya ile 193 ekleme ve 130 silme
  1. 42
    22
      Calculator.java
  2. 2
    2
      Console.java
  3. 101
    59
      Main.java
  4. 11
    30
      Memory.java
  5. 37
    17
      package.bluej

+ 42
- 22
Calculator.java Dosyayı Görüntüle

@@ -6,47 +6,67 @@ import java.lang.Math;
6 6
  * @1.0   05/27/2018
7 7
  */
8 8
 
9
-public class Calculator{      
10
-    
9
+public class Calculator extends Main{         
10
+
11 11
         public double add(double x, double y){
12
-            return x + y;           
12
+            return x + y;        
13 13
         }
14
-        
14
+
15 15
         public double subtract(double x, double y){
16 16
             return x - y;
17 17
         }        
18
-        
18
+
19 19
         public double multiply(double x, double y){
20 20
             return x * y;
21 21
         }
22
-        
22
+
23 23
         public double divide(double x, double y){
24 24
             return x / y;
25 25
         }
26
-        
26
+
27
+        public double getPow(double x, double y){
28
+            return Math.pow(x, y);    
29
+        }
30
+            
27 31
         public double invert(double x){
28 32
             return x * (-1);
29 33
         }       
30
-        
34
+
31 35
         public double square(double x){
32 36
             return x * x;
33 37
         }
34
-        
38
+
35 39
         public double squareRoot(double x){
36 40
             return Math.sqrt(x);
37 41
         }
38
-        
39
-        public double getPow(double x, double y){
40
-            Scanner in = new Scanner(System.in);
41
-            System.out.println("Enter base value: ");
42
-            double first = in.nextInt();
43
-            System.out.println("Enter exponent value: ");
44
-            double second = in.nextInt();
45
-            return Math.pow(x, y);    
42
+
43
+        public double getInverse(double x){
44
+            return (1/x);           
46 45
         }
47
-        //public double getInverse(double x){
48
-            
49
-            
46
+
47
+        public double getCosine(double x){         
48
+            return Math.cos(Math.toRadians(x));
49
+        }
50
+
51
+        public double getSine(double x){
52
+            return Math.sin(Math.toRadians(x));
53
+        }
54
+
55
+        public double getTan(double x){
56
+            return Math.tan(Math.toRadians(x));
57
+        }
58
+
59
+        public double inverseSin(double x){
60
+            return Math.asin(x);
61
+        }
62
+
63
+        public double inverseCos(double x){
64
+            return Math.acos(x);
50 65
         }
51
-       
52
-    //}
66
+
67
+        public double inverseTan(double x){
68
+            return Math.atan(x);
69
+        }
70
+    }
71
+
72
+

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

@@ -27,7 +27,7 @@ public class Console {
27 27
     public static Double getDoubleInput(String prompt) {
28 28
         Scanner scanner = new Scanner(System.in);
29 29
         println(prompt);
30
-        double y = scanner.nextDouble();
31
-        return y;
30
+        double userInput = scanner.nextDouble();
31
+        return userInput;
32 32
     }
33 33
 }

+ 101
- 59
Main.java Dosyayı Görüntüle

@@ -7,72 +7,114 @@ public class Main{
7 7
 
8 8
         //Calls class Calculator, Console, and Memory
9 9
         Calculator calc = new Calculator();
10
-        Memory memory = new Memory();
10
+        Memory memo = new Memory();
11 11
 
12 12
         //User input
13 13
         Scanner reader = new Scanner(System.in);
14
-        System.out.println("Welcome to Calculator!");        
14
+        Console.println("Welcome to Calculator!");        
15 15
         double value = 0; 
16
+        double saveValue = 0;
16 17
 
17
-        while(true){
18
-            System.out.println("0");
19
-            Console.println("(-) to Invert");
20
-            double x = reader.nextDouble();
21
-            String toggleInvert = reader.next();
22
-            reader.nextLine();
23
-            
24
-            
25
-            switch(toggleInvert){
18
+                      
19
+        while(true){               
20
+            Console.println("Pick Operation: Clear  (-)  +  -  *  /  Sq  SqRt  Power\nPick Operation: Cos  Sin  Tan  -Cos  -Sin  -Tan");
21
+            String button = reader.nextLine();
22
+            double x = saveValue;
23
+            ArrayList<String> button1 = Arrays.asList("(-)", "Sq", "SqRt", "Cos", "Sin", "Tan", "-Cos", "-Sin", "-Tan");
24
+            ArrayList<String> button2 = Arrays.asList("+","-","*","/","Power");
25
+                                   
26
+            if(saveValue == 0){
27
+                 x = Console.getDoubleInput("Input number:");                
28
+            }else if(button.equalsIgnoreCase("Clear")){
29
+                 continue;
30
+            }else if(button1.contains(
31
+               
32
+                                                          
33
+            switch(button1){                
26 34
                 case "(-)":
27 35
                     value = calc.invert(x);
28
-                    System.out.print(value);
29
-                while(true){   
30
-                    
31
-                    Console.println("Choose an operation:OFF +  -  *  /");
32
-                    String button = reader.nextLine();
33
-                    reader.nextLine();
34
-                    System.out.println("");
35
-                    double y = reader.nextDouble();            
36
-
36
+                    saveValue = value;
37
+                    Console.println(Double.toString(value));
38
+                    break;
39
+                case "Sq":
40
+                    value = calc.square(x);
41
+                    saveValue = value;
42
+                    Console.println(Double.toString(value));
43
+                    break;
44
+                case "SqRt":
45
+                    value = calc.squareRoot(x);
46
+                    saveValue = value;
47
+                    Console.println(Double.toString(value));
48
+                    break;
49
+                case "Cos":
50
+                    value = calc.getCosine(x);
51
+                    saveValue = value;
52
+                    Console.println(Double.toString(value));
53
+                    break;
54
+                case "Sin":
55
+                    value = calc.getSine(x);
56
+                    saveValue = value;
57
+                    Console.println(Double.toString(value));
58
+                    break;
59
+                case "Tan":
60
+                    value = calc.getTan(x);
61
+                    saveValue = value;
62
+                    Console.println(Double.toString(value));
63
+                    break;
64
+                case "-Cos":
65
+                    value = calc.inverseCos(x);
66
+                    saveValue = value;
67
+                    Console.println(Double.toString(value));
68
+                    break;
69
+                case "-Sin":
70
+                    value = calc.inverseSin(x);
71
+                    saveValue = value;
72
+                    Console.println(Double.toString(value));
73
+                    break;
74
+                case "-Tan":
75
+                    value = calc.inverseTan(x);   
76
+                    saveValue = value;
77
+                    Console.println(Double.toString(value));
78
+                    break;
79
+               }
80
+            
81
+            double y = Console.getDoubleInput("Input Number:");            
82
+            
83
+            switch(button2){
84
+                case "Clear":
85
+                    value = 0; 
86
+                    break;                                     
87
+                case "+":
88
+                    value = calc.add(x, y);
89
+                    saveValue = value;
90
+                    Console.println(Double.toString(value));
91
+                    break;
92
+                case "-":
93
+                    value = calc.subtract(x, y);
94
+                    saveValue = value;
95
+                    Console.println(Double.toString(value));
96
+                    break;
97
+                case "*":
98
+                    value = calc.multiply(x, y);
99
+                    saveValue = value;
100
+                    Console.println(Double.toString(value));
101
+                    break;
102
+                case "/":
103
+                    value = calc.divide(x, y);
104
+                    saveValue = value;
105
+                    Console.println(Double.toString(value));
106
+                    break;                                                   
107
+                case "Power":
108
+                    value = calc.getPow(x, y);
109
+                    saveValue = value;
110
+                    Console.println(Double.toString(value));
111
+                    break;                  
112
+                default:
113
+                    Console.print("Err"); 
114
+                    break;                   
115
+            }   
37 116
             
38
-                    switch(button){
39
-                        case "OFF":
40
-                            return;
41
-                        case "+":
42
-                            value = calc.add(x, y);
43
-                            System.out.print(value);
44
-                            break;
45
-                        case "-":
46
-                            value = calc.subtract(x, y);
47
-                            System.out.print(value);
48
-                            break;
49
-                        case "*":
50
-                            value = calc.multiply(x, y);
51
-                            System.out.print(value);
52
-                            break;
53
-                        case "/":
54
-                            value = calc.divide(x, y);
55
-                            System.out.print(value);
56
-                            break;                    
57
-                        case "Sq":
58
-                            value = calc.square(x);
59
-                            System.out.print(value);
60
-                            break;
61
-                        case "SqRoot":
62
-                            value = calc.squareRoot(x);
63
-                            System.out.print(value);
64
-                            break;
65
-                        case "Power":
66
-                            value = calc.getPow(x, y);
67
-                            System.out.print(value);
68
-                            break;               
69
-                        default:
70
-                            System.out.println("Error");        
71
-                    }               
72
-                }
73
-
74
-            }
75
-
76 117
         }
77 118
     }
78
-}
119
+}
120
+

+ 11
- 30
Memory.java Dosyayı Görüntüle

@@ -1,33 +1,14 @@
1
-
2
-/**
3
- * Write a description of class Memory here.
4
- *
5
- * @author (your name)
6
- * @version (a version number or a date)
7
- */
8
-public class Memory
9
-{
10
-    // instance variables - replace the example below with your own
11
-    private int x;
12
-
13
-    /**
14
-     * Constructor for objects of class Memory
15
-     */
16
-    public Memory()
17
-    {
18
-        // initialise instance variables
19
-        x = 0;
1
+public class Memory{
2
+      
3
+    public void saveNumber(double value){
4
+        double saved = value;
20 5
     }
21
-
22
-    /**
23
-     * An example of a method - replace this comment with your own
24
-     *
25
-     * @param  y  a sample parameter for a method
26
-     * @return    the sum of x and y
27
-     */
28
-    public int sampleMethod(int y)
29
-    {
30
-        // put your code here
31
-        return x + y;
6
+    
7
+    public void eraseNumber(double saveNumber){
8
+        saveNumber = 0;
9
+    }
10
+    
11
+    public void recallNumber(double value){
12
+        double saved = value;
32 13
     }
33 14
 }

+ 37
- 17
package.bluej Dosyayı Görüntüle

@@ -1,23 +1,29 @@
1 1
 #BlueJ package file
2
-dependency1.from=MainApplication
3
-dependency1.to=Console
2
+dependency1.from=Main
3
+dependency1.to=Calculator
4 4
 dependency1.type=UsesDependency
5
-editor.fx.0.height=709
6
-editor.fx.0.width=800
7
-editor.fx.0.x=151
5
+dependency2.from=Main
6
+dependency2.to=Console
7
+dependency2.type=UsesDependency
8
+dependency3.from=Memory
9
+dependency3.to=Console
10
+dependency3.type=UsesDependency
11
+editor.fx.0.height=989
12
+editor.fx.0.width=1494
13
+editor.fx.0.x=1706
8 14
 editor.fx.0.y=23
9 15
 objectbench.height=80
10
-objectbench.width=489
11
-package.divider.horizontal=0.8280467445742905
16
+objectbench.width=352
17
+package.divider.horizontal=0.5993322203672788
12 18
 package.divider.vertical=0.8112798264642083
13 19
 package.editor.height=367
14 20
 package.editor.width=493
15
-package.editor.x=1280
16
-package.editor.y=559
21
+package.editor.x=1314
22
+package.editor.y=141
17 23
 package.frame.height=519
18 24
 package.frame.width=619
19
-package.numDependencies=1
20
-package.numTargets=2
25
+package.numDependencies=3
26
+package.numTargets=4
21 27
 package.showExtends=true
22 28
 package.showUses=true
23 29
 project.charset=UTF-8
@@ -27,16 +33,30 @@ readme.width=47
27 33
 readme.x=10
28 34
 readme.y=10
29 35
 target1.height=50
30
-target1.name=Console
36
+target1.name=Memory
31 37
 target1.showInterface=false
32 38
 target1.type=ClassTarget
33 39
 target1.width=80
34
-target1.x=80
40
+target1.x=260
35 41
 target1.y=200
36 42
 target2.height=50
37
-target2.name=MainApplication
43
+target2.name=Calculator
38 44
 target2.showInterface=false
39 45
 target2.type=ClassTarget
40
-target2.width=120
41
-target2.x=70
42
-target2.y=70
46
+target2.width=90
47
+target2.x=10
48
+target2.y=140
49
+target3.height=50
50
+target3.name=Console
51
+target3.showInterface=false
52
+target3.type=ClassTarget
53
+target3.width=80
54
+target3.x=310
55
+target3.y=90
56
+target4.height=50
57
+target4.name=Main
58
+target4.showInterface=false
59
+target4.type=ClassTarget
60
+target4.width=80
61
+target4.x=120
62
+target4.y=30