This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCalculator.java
62 lines (55 loc) · 2.09 KB
/
Calculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* https://secure.ecs.soton.ac.uk/noteswiki/w/COMP1202/Labs/7
* Stub class from University of Southampton, finished by myself
* @author University of Southampton ECS, OscarVanL
*
*/
public class Calculator {
Double x;
/*
* Chops up input on ' ' then decides whether to add or multiply.
* If the string does not contain a valid format returns null.
*/
public Double x(String x){
/*
* If the operator is +, sets the attribute x to the first number, then calls the x() method with an
* argument of Double (the second number). The returned value is cast to a string and set
* to local variable x. This is later returned as a Double.
*/
if (x.split(" ")[1].equals("+")) {
this.x = new Double(x.split(" ")[0]);
x = this.x(new Double(x.split(" ")[2])).toString();
return new Double(x);
}
/*
* If the operator is x, sets the attribute x to the first number, then calls the x() method with an
* argument of double (the second number). The returned value is cast to a string and set
* to local variable x. This is later returned as a Double.
*/
else if (x.split(" ")[1].equals("x")) {
this.x = new Double(x.split(" ")[0]);
x = this.x(Double.parseDouble(x.split(" ")[2])).toString();
return new Double(x);
}
/*
* Else statement for invalid mathematical operators.
*/
else {
return null;
}
}
/*
* Adds the parameter x to the instance variable x and returns the answer as a Double.
*/
public Double x(Double x){
System.out.println("== Adding ==");
return new Double(this.x + x);
}
/*
* Multiplies the parameter x by instance variable x and return the value as a Double.
*/
public Double x(double x){
System.out.println("== Multiplying ==");
return new Double(this.x * x);
}
}