Spaces:
Running
Running
| Write a class SecretAgent, which has the following features: | |
| A constructor that takes as parameters a name (string) and a code (string) | |
| Get methods getName and getCode. | |
| 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" | |
| A static method static boolean codeOk(String code), which returns true if the code is according to the rules | |
| The code 'follows the rules' if it has exactly 3 numbers and no other characters. | |
| The first 2 numbers must be zeros. | |
| import java.util.Random; | |
| public class Test{ | |
| public static void main(String[] args){ | |
| final Random r = new Random(); | |
| System.out.println("Testing the class SecretAgent..."); | |
| String[] en = "James Jane John Jill Jim Janet George Sarah Oliver Emma".split(" "); | |
| String[] sn = "Bond Pond Smith Johnson Clarke Wilson Taylor Morris".split(" "); | |
| String[] k = "500 505 040 00A 00X XYZ X0X 700 777 0X0X 0007 0003 0070".split(" "); | |
| for (int test = 1; test <=2; test++) { | |
| System.out.println("Test " + test); | |
| String name = en[r.nextInt(en.length)] + " " + sn[r.nextInt(sn.length)]; | |
| String code = "00" + (r.nextInt(9) + 1); | |
| SecretAgent sa = new SecretAgent(name, code); | |
| System.out.println("Object created with parameters (" + name + ", " + code + ")"); | |
| System.out.println("getName() returns " + sa.getName()); | |
| System.out.println("getCode() returns " + sa.getCode()); | |
| System.out.println("Attempting to change code to a valid one..."); | |
| for (int iteration=1; iteration<= 2; iteration++) { | |
| code = "00" + (r.nextInt(9) + 1); | |
| System.out.println("Attempting with code " + code); | |
| sa.setCode(code); | |
| System.out.println("getCode() returns " + sa.getCode()); | |
| } | |
| System.out.println("Attempting to change code to an invalid one..."); | |
| code = k[r.nextInt(k.length)]; | |
| System.out.println("Attempting with code " + code); | |
| sa.setCode(code); | |
| System.out.println("getCode() returns " + sa.getCode()); | |
| } | |
| System.out.println("Test 3: Calling the static method"); | |
| String[] codes = "007 004 003 008 0009 070 00A 600 888".split(" "); | |
| for (String code : codes) { | |
| System.out.println("Testing code " + code + ", valid: " + SecretAgent.codeOk(code)); | |
| } | |
| } | |
| } | |
| //ADD | |
| class SecretAgent { | |
| private String name; | |
| private String code; | |
| public SecretAgent(String name, String code) { | |
| this.name = name; | |
| this.code = code; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public String getCode() { | |
| return code; | |
| } | |
| public void setCode(String code) { | |
| if (codeOk(code)) { | |
| this.code = code; | |
| } else { | |
| this.code = "000"; | |
| } | |
| } | |
| // https://www.geeksforgeeks.org/java-string-matches-method-in-java-with-examples/ | |
| // https://stackoverflow.com/questions/28145881/how-does-d-work-in-java | |
| public static boolean codeOk(String code) { | |
| return code.matches("00\\d"); | |
| } | |
| } | |
| Testing the class SecretAgent... | |
| Test 1 | |
| Object created with parameters (George Taylor, 005) | |
| getName() returns George Taylor | |
| getCode() returns 005 | |
| Attempting to change code to a valid one... | |
| Attempting with code 003 | |
| getCode() returns 003 | |
| Attempting with code 003 | |
| getCode() returns 003 | |
| Attempting to change code to an invalid one... | |
| Attempting with code 500 | |
| getCode() returns 000 | |
| Test 2 | |
| Object created with parameters (Emma Taylor, 002) | |
| getName() returns Emma Taylor | |
| getCode() returns 002 | |
| Attempting to change code to a valid one... | |
| Attempting with code 002 | |
| getCode() returns 002 | |
| Attempting with code 006 | |
| getCode() returns 006 | |
| Attempting to change code to an invalid one... | |
| Attempting with code 700 | |
| getCode() returns 000 | |
| Test 3: Calling the static method | |
| Testing code 007, valid: true | |
| Testing code 004, valid: true | |
| Testing code 003, valid: true | |
| Testing code 008, valid: true | |
| Testing code 0009, valid: false | |
| Testing code 070, valid: false | |
| Testing code 00A, valid: false | |
| Testing code 600, valid: false | |
| Testing code 888, valid: false | |