Spaces:
Running
Running
if (staticMethod2(param1)) {...}
Browse files
Week 6: Methods of OO Programming/02. Class SecretAgent
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write a class SecretAgent, which has the following features:
|
| 2 |
+
A constructor that takes as parameters a name (string) and a code (string)
|
| 3 |
+
Get methods getName and getCode.
|
| 4 |
+
Set method setCode(String code), which sets the code attribute if the given parameter follows the rules (rules below); otherwise, the code is set to "000"
|
| 5 |
+
A static method static boolean codeOk(String code), which returns true if the code is according to the rules
|
| 6 |
+
The code 'follows the rules' if it has exactly 3 numbers and no other characters.
|
| 7 |
+
The first 2 numbers must be zeros.
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
import java.util.Random;
|
| 15 |
+
|
| 16 |
+
public class Test{
|
| 17 |
+
public static void main(String[] args){
|
| 18 |
+
final Random r = new Random();
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
System.out.println("Testing the class SecretAgent...");
|
| 22 |
+
|
| 23 |
+
String[] en = "James Jane John Jill Jim Janet George Sarah Oliver Emma".split(" ");
|
| 24 |
+
String[] sn = "Bond Pond Smith Johnson Clarke Wilson Taylor Morris".split(" ");
|
| 25 |
+
String[] k = "500 505 040 00A 00X XYZ X0X 700 777 0X0X 0007 0003 0070".split(" ");
|
| 26 |
+
|
| 27 |
+
for (int test = 1; test <=2; test++) {
|
| 28 |
+
System.out.println("Test " + test);
|
| 29 |
+
|
| 30 |
+
String name = en[r.nextInt(en.length)] + " " + sn[r.nextInt(sn.length)];
|
| 31 |
+
String code = "00" + (r.nextInt(9) + 1);
|
| 32 |
+
SecretAgent sa = new SecretAgent(name, code);
|
| 33 |
+
|
| 34 |
+
System.out.println("Object created with parameters (" + name + ", " + code + ")");
|
| 35 |
+
|
| 36 |
+
System.out.println("getName() returns " + sa.getName());
|
| 37 |
+
System.out.println("getCode() returns " + sa.getCode());
|
| 38 |
+
|
| 39 |
+
System.out.println("Attempting to change code to a valid one...");
|
| 40 |
+
|
| 41 |
+
for (int iteration=1; iteration<= 2; iteration++) {
|
| 42 |
+
code = "00" + (r.nextInt(9) + 1);
|
| 43 |
+
System.out.println("Attempting with code " + code);
|
| 44 |
+
sa.setCode(code);
|
| 45 |
+
System.out.println("getCode() returns " + sa.getCode());
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
System.out.println("Attempting to change code to an invalid one...");
|
| 49 |
+
|
| 50 |
+
code = k[r.nextInt(k.length)];
|
| 51 |
+
System.out.println("Attempting with code " + code);
|
| 52 |
+
sa.setCode(code);
|
| 53 |
+
System.out.println("getCode() returns " + sa.getCode());
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
System.out.println("Test 3: Calling the static method");
|
| 57 |
+
String[] codes = "007 004 003 008 0009 070 00A 600 888".split(" ");
|
| 58 |
+
for (String code : codes) {
|
| 59 |
+
System.out.println("Testing code " + code + ", valid: " + SecretAgent.codeOk(code));
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
//ADD
|
| 69 |
+
class SecretAgent {
|
| 70 |
+
private String name;
|
| 71 |
+
private String code;
|
| 72 |
+
|
| 73 |
+
public SecretAgent(String name, String code) {
|
| 74 |
+
this.name = name;
|
| 75 |
+
this.code = code;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
public String getName() {
|
| 79 |
+
return name;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
public String getCode() {
|
| 83 |
+
return code;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
public void setCode(String code) {
|
| 87 |
+
if (codeOk(code)) {
|
| 88 |
+
this.code = code;
|
| 89 |
+
} else {
|
| 90 |
+
this.code = "000";
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
// https://www.geeksforgeeks.org/java-string-matches-method-in-java-with-examples/
|
| 95 |
+
// https://stackoverflow.com/questions/28145881/how-does-d-work-in-java
|
| 96 |
+
public static boolean codeOk(String code) {
|
| 97 |
+
return code.matches("00\\d");
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
Testing the class SecretAgent...
|
| 105 |
+
Test 1
|
| 106 |
+
Object created with parameters (George Taylor, 005)
|
| 107 |
+
getName() returns George Taylor
|
| 108 |
+
getCode() returns 005
|
| 109 |
+
Attempting to change code to a valid one...
|
| 110 |
+
Attempting with code 003
|
| 111 |
+
getCode() returns 003
|
| 112 |
+
Attempting with code 003
|
| 113 |
+
getCode() returns 003
|
| 114 |
+
Attempting to change code to an invalid one...
|
| 115 |
+
Attempting with code 500
|
| 116 |
+
getCode() returns 000
|
| 117 |
+
Test 2
|
| 118 |
+
Object created with parameters (Emma Taylor, 002)
|
| 119 |
+
getName() returns Emma Taylor
|
| 120 |
+
getCode() returns 002
|
| 121 |
+
Attempting to change code to a valid one...
|
| 122 |
+
Attempting with code 002
|
| 123 |
+
getCode() returns 002
|
| 124 |
+
Attempting with code 006
|
| 125 |
+
getCode() returns 006
|
| 126 |
+
Attempting to change code to an invalid one...
|
| 127 |
+
Attempting with code 700
|
| 128 |
+
getCode() returns 000
|
| 129 |
+
Test 3: Calling the static method
|
| 130 |
+
Testing code 007, valid: true
|
| 131 |
+
Testing code 004, valid: true
|
| 132 |
+
Testing code 003, valid: true
|
| 133 |
+
Testing code 008, valid: true
|
| 134 |
+
Testing code 0009, valid: false
|
| 135 |
+
Testing code 070, valid: false
|
| 136 |
+
Testing code 00A, valid: false
|
| 137 |
+
Testing code 600, valid: false
|
| 138 |
+
Testing code 888, valid: false
|
| 139 |
+
|