CoolFace
Apppublic

KaiquanMah/TurkuBasicOOPinJava

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
02. Class SecretAgent140 linesDownload Raw Back to Week 6: Methods of OO Programming
1Write a class SecretAgent, which has the following features:2A constructor that takes as parameters a name (string) and a code (string)3Get methods getName and getCode.4Set 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"5A static method static boolean codeOk(String code), which returns true if the code is according to the rules6The code 'follows the rules' if it has exactly 3 numbers and no other characters. 7The first 2 numbers must be zeros.8 9 10 11 12 13 14import java.util.Random;15 16public 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//ADD69class 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-java96    public static boolean codeOk(String code) {97        return code.matches("00\\d");98    }99}100 101 102 103 104Testing the class SecretAgent...105Test 1106Object created with parameters (George Taylor, 005)107getName() returns George Taylor108getCode() returns 005109Attempting to change code to a valid one...110Attempting with code 003111getCode() returns 003112Attempting with code 003113getCode() returns 003114Attempting to change code to an invalid one...115Attempting with code 500116getCode() returns 000117Test 2118Object created with parameters (Emma Taylor, 002)119getName() returns Emma Taylor120getCode() returns 002121Attempting to change code to a valid one...122Attempting with code 002123getCode() returns 002124Attempting with code 006125getCode() returns 006126Attempting to change code to an invalid one...127Attempting with code 700128getCode() returns 000129Test 3: Calling the static method130Testing code 007, valid: true131Testing code 004, valid: true132Testing code 003, valid: true133Testing code 008, valid: true134Testing code 0009, valid: false135Testing code 070, valid: false136Testing code 00A, valid: false137Testing code 600, valid: false138Testing code 888, valid: false139 140