diff --git a/lab/lab9/Question1/BadCarException.java b/lab/lab9/Question1/BadCarException.java new file mode 100644 index 0000000..69191a6 --- /dev/null +++ b/lab/lab9/Question1/BadCarException.java @@ -0,0 +1,16 @@ +/* + * BadCarException + */ + +public class BadCarException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadCarException.class.getName().hashCode(); + + public BadCarException() { + super(); + } + + public BadCarException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question1/BadDoorException.java b/lab/lab9/Question1/BadDoorException.java new file mode 100644 index 0000000..bbba861 --- /dev/null +++ b/lab/lab9/Question1/BadDoorException.java @@ -0,0 +1,16 @@ +/* + * BadDoorException + */ + +public class BadDoorException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadDoorException.class.getName().hashCode(); + + public BadDoorException() { + super(); + } + + public BadDoorException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question1/Car.java b/lab/lab9/Question1/Car.java new file mode 100644 index 0000000..3c560fd --- /dev/null +++ b/lab/lab9/Question1/Car.java @@ -0,0 +1,100 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Car { + private String name; + private Door[] doors; + + /** + * Constructor. + * + * @param name The name of the car. + * @param numberOfDoors The number of doors. + * @throws BadCarException If the number of doors is less than 1. + */ + public Car(String name, int numberOfDoors) throws BadCarException { + if (numberOfDoors < 1) { + throw new BadCarException("A car must have at least one door!"); + } + + this.name = name; + + // initialise the doors array + doors = new Door[numberOfDoors]; + for (int i = 0; i < numberOfDoors; i++) { + doors[i] = new Door(); + } + } + + /** + * Prints each door of the car. + */ + public void listDoors() { + for (Door door : doors) { + System.out.println(String.format("%s: door is %s", name, door.isOpen() ? "open" : "closed")); + } + } + + /** + * Count the numer of open doors. + * + * @return The number of open doors. + */ + public int countOpenDoors() { + int count = 0; + for (Door door : doors) { + if (door.isOpen()) { + count++; + } + } + return count; + } + + /** + * Open one door with number start from 1. + * + * @param doorNumber The number of the door to open start from 1. + * @throws BadDoorException If try to open a non-existed door. + */ + public void openOneDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber+1)); + } + + doors[doorNumber].open(); + } + + /** + * Test. + */ + public static void testCar() { + try { + Car brokencar = new Car("Broken", 0); + } catch (BadCarException ex) { + System.out.println(ex.getMessage() == "A car must have at least one door!"); + } + Car c = null; + try { + c = new Car("Biggy", 7); + } catch (BadCarException ex) { + System.out.println("BUG! This must never happen!"); + } + c.listDoors(); + System.out.println(c.countOpenDoors() == 0); + try { + c.openOneDoor(8); + } catch (BadDoorException ex) { + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + try { + c.openOneDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 1); + } +} diff --git a/lab/lab9/Question1/Door.java b/lab/lab9/Question1/Door.java new file mode 100644 index 0000000..234c14f --- /dev/null +++ b/lab/lab9/Question1/Door.java @@ -0,0 +1,54 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Door { + /** + * Indicator of whether the door is open or not. Default is closed + */ + private boolean isOpen; + + /** + * Constructor. + */ + public Door() { + isOpen = false; + } + + /** + * Get whether the door is open or not. + * + * @return true if the door is open, false otherwise + */ + public boolean isOpen() { + return isOpen; + } + + /** + * Open the door. + */ + public void open() { + isOpen = true; + } + + /** + * Close the door. + */ + public void close() { + isOpen = false; + } + + /** + * Test. + */ + public static void testDoor() { + Door d = new Door(); + System.out.println(d.isOpen() == false); + d.close(); + System.out.println(d.isOpen() == false); + d.open(); + System.out.println(d.isOpen() == true); + } +} diff --git a/lab/lab9/Question1/Start.java b/lab/lab9/Question1/Start.java new file mode 100644 index 0000000..ac29144 --- /dev/null +++ b/lab/lab9/Question1/Start.java @@ -0,0 +1,6 @@ +public class Start { + public static void main(String[] args) { + Door.testDoor(); + Car.testCar(); + } +} diff --git a/lab/lab9/Question2/BadCarException.java b/lab/lab9/Question2/BadCarException.java new file mode 100644 index 0000000..69191a6 --- /dev/null +++ b/lab/lab9/Question2/BadCarException.java @@ -0,0 +1,16 @@ +/* + * BadCarException + */ + +public class BadCarException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadCarException.class.getName().hashCode(); + + public BadCarException() { + super(); + } + + public BadCarException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question2/BadDoorException.java b/lab/lab9/Question2/BadDoorException.java new file mode 100644 index 0000000..bbba861 --- /dev/null +++ b/lab/lab9/Question2/BadDoorException.java @@ -0,0 +1,16 @@ +/* + * BadDoorException + */ + +public class BadDoorException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadDoorException.class.getName().hashCode(); + + public BadDoorException() { + super(); + } + + public BadDoorException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question2/Car.java b/lab/lab9/Question2/Car.java new file mode 100644 index 0000000..8dfa838 --- /dev/null +++ b/lab/lab9/Question2/Car.java @@ -0,0 +1,124 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Car { + private String name; + private Door[] doors; + + /** + * Constructor. + * + * @param name The name of the car. + * @param numberOfDoors The number of doors. + * @throws BadCarException If the number of doors is less than 1. + */ + public Car(String name, int numberOfDoors) throws BadCarException { + if (numberOfDoors < 1) { + throw new BadCarException("A car must have at least one door!"); + } + + this.name = name; + + // initialise the doors array + doors = new Door[numberOfDoors]; + for (int i = 0; i < numberOfDoors; i++) { + doors[i] = new Door(); + } + } + + /** + * Prints each door of the car. + */ + public void listDoors() { + for (Door door : doors) { + System.out.println(String.format("%s: door is %s", name, door.isOpen() ? "open" : "closed")); + } + } + + /** + * Count the numer of open doors. + * + * @return The number of open doors. + */ + public int countOpenDoors() { + int count = 0; + for (Door door : doors) { + if (door.isOpen()) { + count++; + } + } + return count; + } + + /** + * Open one door with number start from 1. + * + * @param doorNumber The number of the door to open start from 1. + * @throws BadDoorException If try to open a non-existed door. + */ + public void openOneDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1)); + } + + doors[doorNumber].open(); + } + + /** + * Open all the closed doors and closes all the open doors. + */ + public void changeAllDoors() { + for (Door door : doors) { + if (door.isOpen()) { + door.close(); + } else { + door.open(); + } + } + } + + /** + * Test. + */ + public static void testCar() { + try { + Car brokencar = new Car("Broken", 0); + } catch (BadCarException ex) { + System.out.println(ex.getMessage() == "A car must have at least one door!"); + } + Car c = null; + try { + c = new Car("Biggy", 7); + } catch (BadCarException ex) { + System.out.println("BUG! This must never happen!"); + } + c.listDoors(); + System.out.println(c.countOpenDoors() == 0); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 0); + try { + c.openOneDoor(8); + } catch (BadDoorException ex) { + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + try { + c.openOneDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 1); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 6); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 1); + c.listDoors(); + } +} diff --git a/lab/lab9/Question2/Door.java b/lab/lab9/Question2/Door.java new file mode 100644 index 0000000..234c14f --- /dev/null +++ b/lab/lab9/Question2/Door.java @@ -0,0 +1,54 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Door { + /** + * Indicator of whether the door is open or not. Default is closed + */ + private boolean isOpen; + + /** + * Constructor. + */ + public Door() { + isOpen = false; + } + + /** + * Get whether the door is open or not. + * + * @return true if the door is open, false otherwise + */ + public boolean isOpen() { + return isOpen; + } + + /** + * Open the door. + */ + public void open() { + isOpen = true; + } + + /** + * Close the door. + */ + public void close() { + isOpen = false; + } + + /** + * Test. + */ + public static void testDoor() { + Door d = new Door(); + System.out.println(d.isOpen() == false); + d.close(); + System.out.println(d.isOpen() == false); + d.open(); + System.out.println(d.isOpen() == true); + } +} diff --git a/lab/lab9/Question2/Start.java b/lab/lab9/Question2/Start.java new file mode 100644 index 0000000..ac29144 --- /dev/null +++ b/lab/lab9/Question2/Start.java @@ -0,0 +1,6 @@ +public class Start { + public static void main(String[] args) { + Door.testDoor(); + Car.testCar(); + } +} diff --git a/lab/lab9/Question3/BadCarException.java b/lab/lab9/Question3/BadCarException.java new file mode 100644 index 0000000..69191a6 --- /dev/null +++ b/lab/lab9/Question3/BadCarException.java @@ -0,0 +1,16 @@ +/* + * BadCarException + */ + +public class BadCarException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadCarException.class.getName().hashCode(); + + public BadCarException() { + super(); + } + + public BadCarException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question3/BadDoorException.java b/lab/lab9/Question3/BadDoorException.java new file mode 100644 index 0000000..bbba861 --- /dev/null +++ b/lab/lab9/Question3/BadDoorException.java @@ -0,0 +1,16 @@ +/* + * BadDoorException + */ + +public class BadDoorException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadDoorException.class.getName().hashCode(); + + public BadDoorException() { + super(); + } + + public BadDoorException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question3/Car.java b/lab/lab9/Question3/Car.java new file mode 100644 index 0000000..c77ab4a --- /dev/null +++ b/lab/lab9/Question3/Car.java @@ -0,0 +1,156 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Car { + private String name; + private Door[] doors; + + /** + * Constructor. + * + * @param name The name of the car. + * @param numberOfDoors The number of doors. + * @throws BadCarException If the number of doors is less than 1. + */ + public Car(String name, int numberOfDoors) throws BadCarException { + if (numberOfDoors < 1) { + throw new BadCarException("A car must have at least one door!"); + } + + this.name = name; + + // initialise the doors array + doors = new Door[numberOfDoors]; + for (int i = 0; i < numberOfDoors; i++) { + doors[i] = new Door(); + } + } + + /** + * Prints each door of the car. + */ + public void listDoors() { + for (Door door : doors) { + System.out.println(String.format("%s: door is %s", name, door.isOpen() ? "open" : "closed")); + } + } + + /** + * Count the numer of open doors. + * + * @return The number of open doors. + */ + public int countOpenDoors() { + int count = 0; + for (Door door : doors) { + if (door.isOpen()) { + count++; + } + } + return count; + } + + /** + * Open one door with number start from 1. + * + * @param doorNumber The number of the door to open start from 1. + * @throws BadDoorException If try to open a non-existed door. + */ + public void openOneDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1)); + } + + doors[doorNumber].open(); + } + + /** + * Open all the closed doors and closes all the open doors. + */ + public void changeAllDoors() { + for (Door door : doors) { + if (door.isOpen()) { + door.close(); + } else { + door.open(); + } + } + } + + /** + * Replaces the existing car door with a completely new door. + * + * @param doorNumber The number of the door to replace start from 1. + * @throws BadDoorException if replace a non-existed door. + */ + public void replaceDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1)); + } + + // replace the door. + doors[doorNumber] = new Door(); + } + + /** + * Test. + */ + public static void testCar() { + try { + Car brokencar = new Car("Broken", 0); + } catch (BadCarException ex) { + System.out.println(ex.getMessage() == "A car must have at least one door!"); + } + Car c = null; + try { + c = new Car("Biggy", 7); + } catch (BadCarException ex) { + System.out.println("BUG! This must never happen!"); + } + c.listDoors(); + System.out.println(c.countOpenDoors() == 0); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 0); + try { + c.openOneDoor(8); + } catch (BadDoorException ex) { + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + try { + c.openOneDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 1); + + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 6); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 1); + c.listDoors(); + try { + c.replaceDoor(8); + } catch (BadDoorException ex) { + + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + + try { + c.replaceDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 0); + + c.listDoors(); + } +} diff --git a/lab/lab9/Question3/Door.java b/lab/lab9/Question3/Door.java new file mode 100644 index 0000000..234c14f --- /dev/null +++ b/lab/lab9/Question3/Door.java @@ -0,0 +1,54 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Door { + /** + * Indicator of whether the door is open or not. Default is closed + */ + private boolean isOpen; + + /** + * Constructor. + */ + public Door() { + isOpen = false; + } + + /** + * Get whether the door is open or not. + * + * @return true if the door is open, false otherwise + */ + public boolean isOpen() { + return isOpen; + } + + /** + * Open the door. + */ + public void open() { + isOpen = true; + } + + /** + * Close the door. + */ + public void close() { + isOpen = false; + } + + /** + * Test. + */ + public static void testDoor() { + Door d = new Door(); + System.out.println(d.isOpen() == false); + d.close(); + System.out.println(d.isOpen() == false); + d.open(); + System.out.println(d.isOpen() == true); + } +} diff --git a/lab/lab9/Question3/Start.java b/lab/lab9/Question3/Start.java new file mode 100644 index 0000000..ac29144 --- /dev/null +++ b/lab/lab9/Question3/Start.java @@ -0,0 +1,6 @@ +public class Start { + public static void main(String[] args) { + Door.testDoor(); + Car.testCar(); + } +} diff --git a/lab/lab9/Question4/BadCarException.java b/lab/lab9/Question4/BadCarException.java new file mode 100644 index 0000000..69191a6 --- /dev/null +++ b/lab/lab9/Question4/BadCarException.java @@ -0,0 +1,16 @@ +/* + * BadCarException + */ + +public class BadCarException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadCarException.class.getName().hashCode(); + + public BadCarException() { + super(); + } + + public BadCarException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question4/BadDoorException.java b/lab/lab9/Question4/BadDoorException.java new file mode 100644 index 0000000..bbba861 --- /dev/null +++ b/lab/lab9/Question4/BadDoorException.java @@ -0,0 +1,16 @@ +/* + * BadDoorException + */ + +public class BadDoorException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadDoorException.class.getName().hashCode(); + + public BadDoorException() { + super(); + } + + public BadDoorException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question4/Car.java b/lab/lab9/Question4/Car.java new file mode 100644 index 0000000..02d76c5 --- /dev/null +++ b/lab/lab9/Question4/Car.java @@ -0,0 +1,170 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Car { + private String name; + private Door[] doors; + + /** + * Constructor. + * + * @param name The name of the car. + * @param numberOfDoors The number of doors. + * @throws BadCarException If the number of doors is less than 1. + */ + public Car(String name, int numberOfDoors) throws BadCarException { + if (numberOfDoors < 1) { + throw new BadCarException("A car must have at least one door!"); + } + + this.name = name; + + // initialise the doors array + doors = new Door[numberOfDoors]; + for (int i = 0; i < numberOfDoors; i++) { + doors[i] = new Door(); + } + } + + /** + * Prints each door of the car. + */ + public void listDoors() { + for (Door door : doors) { + System.out.println(String.format("%s: door is %s", name, door.isOpen() ? "open" : "closed")); + } + } + + /** + * Count the numer of open doors. + * + * @return The number of open doors. + */ + public int countOpenDoors() { + int count = 0; + for (Door door : doors) { + if (door.isOpen()) { + count++; + } + } + return count; + } + + /** + * Open one door with number start from 1. + * + * @param doorNumber The number of the door to open start from 1. + * @throws BadDoorException If try to open a non-existed door. + */ + public void openOneDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1)); + } + + doors[doorNumber].open(); + } + + /** + * Open all the closed doors and closes all the open doors. + */ + public void changeAllDoors() { + for (Door door : doors) { + if (door.isOpen()) { + door.close(); + } else { + door.open(); + } + } + } + + /** + * Replaces the existing car door with a completely new door. + * + * @param doorNumber The number of the door to replace start from 1. + * @throws BadDoorException if replace a non-existed door. + */ + public void replaceDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1)); + } + + // replace the door. + doors[doorNumber] = new Door(); + } + + /** + * Replace all doors. + */ + public void replaceAllDoors() { + for (int i = 0; i < doors.length; i++) { + doors[i] = new Door(); + } + } + + /** + * Test. + */ + public static void testCar() { + try { + Car brokencar = new Car("Broken", 0); + } catch (BadCarException ex) { + System.out.println(ex.getMessage() == "A car must have at least one door!"); + } + Car c = null; + try { + c = new Car("Biggy", 7); + } catch (BadCarException ex) { + System.out.println("BUG! This must never happen!"); + } + c.listDoors(); + System.out.println(c.countOpenDoors() == 0); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 0); + try { + c.openOneDoor(8); + } catch (BadDoorException ex) { + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + try { + c.openOneDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 1); + + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 6); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 1); + c.listDoors(); + try { + c.replaceDoor(8); + } catch (BadDoorException ex) { + + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + + try { + c.replaceDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 0); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.replaceAllDoors(); + System.out.println(c.countOpenDoors() == 0); + c.listDoors(); + } +} diff --git a/lab/lab9/Question4/Door.java b/lab/lab9/Question4/Door.java new file mode 100644 index 0000000..234c14f --- /dev/null +++ b/lab/lab9/Question4/Door.java @@ -0,0 +1,54 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Door { + /** + * Indicator of whether the door is open or not. Default is closed + */ + private boolean isOpen; + + /** + * Constructor. + */ + public Door() { + isOpen = false; + } + + /** + * Get whether the door is open or not. + * + * @return true if the door is open, false otherwise + */ + public boolean isOpen() { + return isOpen; + } + + /** + * Open the door. + */ + public void open() { + isOpen = true; + } + + /** + * Close the door. + */ + public void close() { + isOpen = false; + } + + /** + * Test. + */ + public static void testDoor() { + Door d = new Door(); + System.out.println(d.isOpen() == false); + d.close(); + System.out.println(d.isOpen() == false); + d.open(); + System.out.println(d.isOpen() == true); + } +} diff --git a/lab/lab9/Question4/Start.java b/lab/lab9/Question4/Start.java new file mode 100644 index 0000000..6e9b56f --- /dev/null +++ b/lab/lab9/Question4/Start.java @@ -0,0 +1,17 @@ +/** + * Answer to questions. + * + * - Q4: What happends if you try to use am enhanced for loop? + * + * - A: The original doors will not be replace. When you use for (Door door : + * doors), JVM will copy the value to door (or copy the address). If you set + * door = new Door(), the new door will be assigned to that copied object, + * istead of the object in array. + * + */ +public class Start { + public static void main(String[] args) { + Door.testDoor(); + Car.testCar(); + } +} diff --git a/lab/lab9/Question5/BadCarException.java b/lab/lab9/Question5/BadCarException.java new file mode 100644 index 0000000..69191a6 --- /dev/null +++ b/lab/lab9/Question5/BadCarException.java @@ -0,0 +1,16 @@ +/* + * BadCarException + */ + +public class BadCarException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadCarException.class.getName().hashCode(); + + public BadCarException() { + super(); + } + + public BadCarException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question5/BadDoorException.java b/lab/lab9/Question5/BadDoorException.java new file mode 100644 index 0000000..bbba861 --- /dev/null +++ b/lab/lab9/Question5/BadDoorException.java @@ -0,0 +1,16 @@ +/* + * BadDoorException + */ + +public class BadDoorException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadDoorException.class.getName().hashCode(); + + public BadDoorException() { + super(); + } + + public BadDoorException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question5/Car.java b/lab/lab9/Question5/Car.java new file mode 100644 index 0000000..f9ae0ef --- /dev/null +++ b/lab/lab9/Question5/Car.java @@ -0,0 +1,196 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Car { + private String name; + private Door[] doors; + + /** + * Constructor. + * + * @param name The name of the car. + * @param numberOfDoors The number of doors. + * @throws BadCarException If the number of doors is less than 1. + */ + public Car(String name, int numberOfDoors) throws BadCarException { + if (numberOfDoors < 1) { + throw new BadCarException("A car must have at least one door!"); + } + + this.name = name; + + // initialise the doors array + doors = new Door[numberOfDoors]; + for (int i = 0; i < numberOfDoors; i++) { + doors[i] = new Door(); + } + } + + /** + * Prints each door of the car. + */ + public void listDoors() { + for (Door door : doors) { + System.out.println(String.format("%s: door is %s", name, door.isOpen() ? "open" : "closed")); + } + } + + /** + * Count the numer of open doors. + * + * @return The number of open doors. + */ + public int countOpenDoors() { + int count = 0; + for (Door door : doors) { + if (door.isOpen()) { + count++; + } + } + return count; + } + + /** + * Open one door with number start from 1. + * + * @param doorNumber The number of the door to open start from 1. + * @throws BadDoorException If try to open a non-existed door. + */ + public void openOneDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1)); + } + + doors[doorNumber].open(); + } + + /** + * Open all the closed doors and closes all the open doors. + */ + public void changeAllDoors() { + for (Door door : doors) { + if (door.isOpen()) { + door.close(); + } else { + door.open(); + } + } + } + + /** + * Replaces the existing car door with a completely new door. + * + * @param doorNumber The number of the door to replace start from 1. + * @throws BadDoorException if replace a non-existed door. + */ + public void replaceDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1)); + } + + // replace the door. + doors[doorNumber] = new Door(); + } + + /** + * Replace all doors. + */ + public void replaceAllDoors() { + for (int i = 0; i < doors.length; i++) { + doors[i] = new Door(); + } + } + + /** + * Replace existing doors start from the first door with the number of doors. + * + * @param numOfDoorsToReplace The number of doors to replace. + */ + public void replaceManyDoors(int numOfDoorsToReplace) { + for (int i = 0; i < numOfDoorsToReplace; i++) { + doors[i] = new Door(); + } + } + + /** + * Test. + */ + public static void testCar() { + try { + Car brokencar = new Car("Broken", 0); + } catch (BadCarException ex) { + System.out.println(ex.getMessage() == "A car must have at least one door!"); + } + Car c = null; + try { + c = new Car("Biggy", 7); + } catch (BadCarException ex) { + System.out.println("BUG! This must never happen!"); + } + c.listDoors(); + System.out.println(c.countOpenDoors() == 0); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 0); + try { + c.openOneDoor(8); + } catch (BadDoorException ex) { + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + try { + c.openOneDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 1); + + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 6); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 1); + c.listDoors(); + try { + c.replaceDoor(8); + } catch (BadDoorException ex) { + + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + + try { + c.replaceDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 0); + + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.replaceAllDoors(); + System.out.println(c.countOpenDoors() == 0); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.replaceManyDoors(4); + System.out.println(c.countOpenDoors() == 3); + c.listDoors(); + try { + c.replaceManyDoors(20); + } catch (ArrayIndexOutOfBoundsException ex) { + System.out.println(ex.getMessage().equals("Index 7 out of bounds for length 7")); + } + + System.out.println(c.countOpenDoors() == 0); + c.listDoors(); + } +} diff --git a/lab/lab9/Question5/Door.java b/lab/lab9/Question5/Door.java new file mode 100644 index 0000000..234c14f --- /dev/null +++ b/lab/lab9/Question5/Door.java @@ -0,0 +1,54 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Door { + /** + * Indicator of whether the door is open or not. Default is closed + */ + private boolean isOpen; + + /** + * Constructor. + */ + public Door() { + isOpen = false; + } + + /** + * Get whether the door is open or not. + * + * @return true if the door is open, false otherwise + */ + public boolean isOpen() { + return isOpen; + } + + /** + * Open the door. + */ + public void open() { + isOpen = true; + } + + /** + * Close the door. + */ + public void close() { + isOpen = false; + } + + /** + * Test. + */ + public static void testDoor() { + Door d = new Door(); + System.out.println(d.isOpen() == false); + d.close(); + System.out.println(d.isOpen() == false); + d.open(); + System.out.println(d.isOpen() == true); + } +} diff --git a/lab/lab9/Question5/Start.java b/lab/lab9/Question5/Start.java new file mode 100644 index 0000000..fb3b803 --- /dev/null +++ b/lab/lab9/Question5/Start.java @@ -0,0 +1,21 @@ +/** + * Answer to questions. + * + * - Q4: What happends if you try to use am enhanced for loop? + * + * - A: The original doors will not be replace. When you use for (Door door : + * doors), JVM will copy the value to door (or copy the address). If you set + * door = new Door(), the new door will be assigned to that copied object, + * istead of the object in array. + * + * - Q5: What happends if you try to replace more doors than the car has? + * + * - A: It will throw ArrayIndexOutOfBoundsException. + * + */ +public class Start { + public static void main(String[] args) { + Door.testDoor(); + Car.testCar(); + } +} diff --git a/lab/lab9/Question6/BadCarException.java b/lab/lab9/Question6/BadCarException.java new file mode 100644 index 0000000..69191a6 --- /dev/null +++ b/lab/lab9/Question6/BadCarException.java @@ -0,0 +1,16 @@ +/* + * BadCarException + */ + +public class BadCarException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadCarException.class.getName().hashCode(); + + public BadCarException() { + super(); + } + + public BadCarException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question6/BadDoorException.java b/lab/lab9/Question6/BadDoorException.java new file mode 100644 index 0000000..bbba861 --- /dev/null +++ b/lab/lab9/Question6/BadDoorException.java @@ -0,0 +1,16 @@ +/* + * BadDoorException + */ + +public class BadDoorException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadDoorException.class.getName().hashCode(); + + public BadDoorException() { + super(); + } + + public BadDoorException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question6/Car.java b/lab/lab9/Question6/Car.java new file mode 100644 index 0000000..d0db317 --- /dev/null +++ b/lab/lab9/Question6/Car.java @@ -0,0 +1,220 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Car { + private String name; + private Door[] doors; + + /** + * Constructor. + * + * @param name The name of the car. + * @param numberOfDoors The number of doors. + * @throws BadCarException If the number of doors is less than 1. + */ + public Car(String name, int numberOfDoors) throws BadCarException { + if (numberOfDoors < 1) { + throw new BadCarException("A car must have at least one door!"); + } + + this.name = name; + + // initialise the doors array + doors = new Door[numberOfDoors]; + for (int i = 0; i < numberOfDoors; i++) { + doors[i] = new Door(); + } + } + + /** + * Prints each door of the car. + */ + public void listDoors() { + for (Door door : doors) { + System.out.println(String.format("%s: door is %s", name, door.isOpen() ? "open" : "closed")); + } + } + + /** + * Count the numer of open doors. + * + * @return The number of open doors. + */ + public int countOpenDoors() { + int count = 0; + for (Door door : doors) { + if (door.isOpen()) { + count++; + } + } + return count; + } + + /** + * Open one door with number start from 1. + * + * @param doorNumber The number of the door to open start from 1. + * @throws BadDoorException If try to open a non-existed door. + */ + public void openOneDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1)); + } + + doors[doorNumber].open(); + } + + /** + * Open all the closed doors and closes all the open doors. + */ + public void changeAllDoors() { + for (Door door : doors) { + if (door.isOpen()) { + door.close(); + } else { + door.open(); + } + } + } + + /** + * Replaces the existing car door with a completely new door. + * + * @param doorNumber The number of the door to replace start from 1. + * @throws BadDoorException if replace a non-existed door. + */ + public void replaceDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.length || doors[doorNumber] == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1)); + } + + // replace the door. + doors[doorNumber] = new Door(); + } + + /** + * Replace all doors. + */ + public void replaceAllDoors() { + for (int i = 0; i < doors.length; i++) { + doors[i] = new Door(); + } + } + + /** + * Replace existing doors start from the first door with the number of doors. + * + * @param numOfDoorsToReplace The number of doors to replace. + */ + public void replaceManyDoors(int numOfDoorsToReplace) { + for (int i = 0; i < numOfDoorsToReplace; i++) { + doors[i] = new Door(); + } + } + + /** + * Adds two new doors to the car. + */ + public void expandCar() { + Door[] newDoors = new Door[doors.length + 2]; + + // copy the old doors to the new array. + for (int i = 0; i < doors.length; i++) { + newDoors[i] = doors[i]; + } + + // initialise the new doors. + for (int i = doors.length; i < newDoors.length; i++) { + newDoors[i] = new Door(); + } + + doors = newDoors; + } + + /** + * Test. + */ + public static void testCar() { + try { + Car brokencar = new Car("Broken", 0); + } catch (BadCarException ex) { + System.out.println(ex.getMessage() == "A car must have at least one door!"); + } + Car c = null; + try { + c = new Car("Biggy", 7); + } catch (BadCarException ex) { + System.out.println("BUG! This must never happen!"); + } + c.listDoors(); + System.out.println(c.countOpenDoors() == 0); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 0); + try { + c.openOneDoor(8); + } catch (BadDoorException ex) { + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + try { + c.openOneDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 1); + + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 6); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 1); + c.listDoors(); + try { + c.replaceDoor(8); + } catch (BadDoorException ex) { + + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + + try { + c.replaceDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 0); + + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.replaceAllDoors(); + System.out.println(c.countOpenDoors() == 0); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.replaceManyDoors(4); + System.out.println(c.countOpenDoors() == 3); + c.listDoors(); + try { + c.replaceManyDoors(20); + } catch (ArrayIndexOutOfBoundsException ex) { + System.out.println(ex.getMessage().equals("Index 7 out of bounds for length 7")); + } + + System.out.println(c.countOpenDoors() == 0); + c.listDoors(); + + c.expandCar(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 9); + c.listDoors(); + } +} diff --git a/lab/lab9/Question6/Door.java b/lab/lab9/Question6/Door.java new file mode 100644 index 0000000..234c14f --- /dev/null +++ b/lab/lab9/Question6/Door.java @@ -0,0 +1,54 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Door { + /** + * Indicator of whether the door is open or not. Default is closed + */ + private boolean isOpen; + + /** + * Constructor. + */ + public Door() { + isOpen = false; + } + + /** + * Get whether the door is open or not. + * + * @return true if the door is open, false otherwise + */ + public boolean isOpen() { + return isOpen; + } + + /** + * Open the door. + */ + public void open() { + isOpen = true; + } + + /** + * Close the door. + */ + public void close() { + isOpen = false; + } + + /** + * Test. + */ + public static void testDoor() { + Door d = new Door(); + System.out.println(d.isOpen() == false); + d.close(); + System.out.println(d.isOpen() == false); + d.open(); + System.out.println(d.isOpen() == true); + } +} diff --git a/lab/lab9/Question6/Start.java b/lab/lab9/Question6/Start.java new file mode 100644 index 0000000..fb3b803 --- /dev/null +++ b/lab/lab9/Question6/Start.java @@ -0,0 +1,21 @@ +/** + * Answer to questions. + * + * - Q4: What happends if you try to use am enhanced for loop? + * + * - A: The original doors will not be replace. When you use for (Door door : + * doors), JVM will copy the value to door (or copy the address). If you set + * door = new Door(), the new door will be assigned to that copied object, + * istead of the object in array. + * + * - Q5: What happends if you try to replace more doors than the car has? + * + * - A: It will throw ArrayIndexOutOfBoundsException. + * + */ +public class Start { + public static void main(String[] args) { + Door.testDoor(); + Car.testCar(); + } +} diff --git a/lab/lab9/Question7/BadCarException.java b/lab/lab9/Question7/BadCarException.java new file mode 100644 index 0000000..69191a6 --- /dev/null +++ b/lab/lab9/Question7/BadCarException.java @@ -0,0 +1,16 @@ +/* + * BadCarException + */ + +public class BadCarException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadCarException.class.getName().hashCode(); + + public BadCarException() { + super(); + } + + public BadCarException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question7/BadDoorException.java b/lab/lab9/Question7/BadDoorException.java new file mode 100644 index 0000000..bbba861 --- /dev/null +++ b/lab/lab9/Question7/BadDoorException.java @@ -0,0 +1,16 @@ +/* + * BadDoorException + */ + +public class BadDoorException extends Exception { + // generate serial version ID by class name + private static final long serialVersionUID = BadDoorException.class.getName().hashCode(); + + public BadDoorException() { + super(); + } + + public BadDoorException(String message) { + super(message); + } +} diff --git a/lab/lab9/Question7/Car.java b/lab/lab9/Question7/Car.java new file mode 100644 index 0000000..0007863 --- /dev/null +++ b/lab/lab9/Question7/Car.java @@ -0,0 +1,212 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +import java.util.ArrayList; + +public class Car { + private String name; + private ArrayList doors; + + /** + * Constructor. + * + * @param name The name of the car. + * @param numberOfDoors The number of doors. + * @throws BadCarException If the number of doors is less than 1. + */ + public Car(String name, int numberOfDoors) throws BadCarException { + if (numberOfDoors < 1) { + throw new BadCarException("A car must have at least one door!"); + } + + this.name = name; + + // initialise the doors array + doors = new ArrayList(); + for (int i = 0; i < numberOfDoors; i++) { + doors.add(new Door()); + } + } + + /** + * Prints each door of the car. + */ + public void listDoors() { + for (int i = 0; i < doors.size(); i++) { + Door door = (Door) doors.get(i); + System.out.println(String.format("%s: door is %s", name, door.isOpen() ? "open" : "closed")); + } + } + + /** + * Count the numer of open doors. + * + * @return The number of open doors. + */ + public int countOpenDoors() { + int count = 0; + for (int i = 0; i < doors.size(); i++) { + Door door = (Door) doors.get(i); + if (door.isOpen()) { + count++; + } + } + return count; + } + + /** + * Open one door with number start from 1. + * + * @param doorNumber The number of the door to open start from 1. + * @throws BadDoorException If try to open a non-existed door. + */ + public void openOneDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.size() || doors.get(doorNumber) == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1)); + } + + Door door = (Door) doors.get(doorNumber); + door.open(); + } + + /** + * Open all the closed doors and closes all the open doors. + */ + public void changeAllDoors() { + for (int i = 0; i < doors.size(); i++) { + Door door = (Door) doors.get(i); + if (door.isOpen()) { + door.close(); + } else { + door.open(); + } + } + } + + /** + * Replaces the existing car door with a completely new door. + * + * @param doorNumber The number of the door to replace start from 1. + * @throws BadDoorException if replace a non-existed door. + */ + public void replaceDoor(int doorNumber) throws BadDoorException { + doorNumber--; + if (doorNumber < 0 || doorNumber >= doors.size() || doors.get(doorNumber) == null) { + throw new BadDoorException(String.format("Door %d does not exist!", doorNumber + 1)); + } + + // replace the door. + doors.set(doorNumber, new Door()); + } + + /** + * Replace all doors. + */ + public void replaceAllDoors() { + for (int i = 0; i < doors.size(); i++) { + doors.set(i, new Door()); + } + } + + /** + * Replace existing doors start from the first door with the number of doors. + * + * @param numOfDoorsToReplace The number of doors to replace. + */ + public void replaceManyDoors(int numOfDoorsToReplace) { + for (int i = 0; i < numOfDoorsToReplace; i++) { + doors.set(i, new Door()); + } + } + + /** + * Adds two new doors to the car. + */ + public void expandCar() { + doors.add(new Door()); + doors.add(new Door()); + } + + /** + * Test. + */ + public static void testCar() { + + try { + Car brokencar = new Car("Broken", 0); + } catch (BadCarException ex) { + System.out.println(ex.getMessage() == "A car must have at least one door!"); + } + Car c = null; + try { + c = new Car("Biggy", 7); + } catch (BadCarException ex) { + System.out.println("BUG! This must never happen!"); + } + c.listDoors(); + System.out.println(c.countOpenDoors() == 0); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 0); + try { + c.openOneDoor(8); + } catch (BadDoorException ex) { + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + try { + c.openOneDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 1); + + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 6); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 1); + c.listDoors(); + try { + c.replaceDoor(8); + } catch (BadDoorException ex) { + + System.out.println(ex.getMessage().equals("Door 8 does not exist!")); + } + try { + c.replaceDoor(3); + } catch (BadDoorException ex) { + System.out.println("BUG! This must never happen!"); + } + System.out.println(c.countOpenDoors() == 0); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.replaceAllDoors(); + System.out.println(c.countOpenDoors() == 0); + c.listDoors(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 7); + c.listDoors(); + c.replaceManyDoors(4); + System.out.println(c.countOpenDoors() == 3); + c.listDoors(); + try { + c.replaceManyDoors(20); + } catch (IndexOutOfBoundsException ex) { + System.out.println(ex.getMessage().equals("Index 7 out of bounds for length 7")); + } + System.out.println(c.countOpenDoors() == 0); + c.listDoors(); + c.expandCar(); + c.changeAllDoors(); + System.out.println(c.countOpenDoors() == 9); + c.listDoors(); + } +} diff --git a/lab/lab9/Question7/Door.java b/lab/lab9/Question7/Door.java new file mode 100644 index 0000000..234c14f --- /dev/null +++ b/lab/lab9/Question7/Door.java @@ -0,0 +1,54 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-21 + * Description: This is the class of Door. + */ + +public class Door { + /** + * Indicator of whether the door is open or not. Default is closed + */ + private boolean isOpen; + + /** + * Constructor. + */ + public Door() { + isOpen = false; + } + + /** + * Get whether the door is open or not. + * + * @return true if the door is open, false otherwise + */ + public boolean isOpen() { + return isOpen; + } + + /** + * Open the door. + */ + public void open() { + isOpen = true; + } + + /** + * Close the door. + */ + public void close() { + isOpen = false; + } + + /** + * Test. + */ + public static void testDoor() { + Door d = new Door(); + System.out.println(d.isOpen() == false); + d.close(); + System.out.println(d.isOpen() == false); + d.open(); + System.out.println(d.isOpen() == true); + } +} diff --git a/lab/lab9/Question7/Start.java b/lab/lab9/Question7/Start.java new file mode 100644 index 0000000..fb3b803 --- /dev/null +++ b/lab/lab9/Question7/Start.java @@ -0,0 +1,21 @@ +/** + * Answer to questions. + * + * - Q4: What happends if you try to use am enhanced for loop? + * + * - A: The original doors will not be replace. When you use for (Door door : + * doors), JVM will copy the value to door (or copy the address). If you set + * door = new Door(), the new door will be assigned to that copied object, + * istead of the object in array. + * + * - Q5: What happends if you try to replace more doors than the car has? + * + * - A: It will throw ArrayIndexOutOfBoundsException. + * + */ +public class Start { + public static void main(String[] args) { + Door.testDoor(); + Car.testCar(); + } +}