lab 9 close #7

This commit is contained in:
2022-04-23 00:21:33 +08:00
parent 941b3d26d4
commit 0ced996a6e
35 changed files with 1878 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

100
lab/lab9/Question1/Car.java Normal file
View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,6 @@
public class Start {
public static void main(String[] args) {
Door.testDoor();
Car.testCar();
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

124
lab/lab9/Question2/Car.java Normal file
View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,6 @@
public class Start {
public static void main(String[] args) {
Door.testDoor();
Car.testCar();
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

156
lab/lab9/Question3/Car.java Normal file
View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,6 @@
public class Start {
public static void main(String[] args) {
Door.testDoor();
Car.testCar();
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

170
lab/lab9/Question4/Car.java Normal file
View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

196
lab/lab9/Question5/Car.java Normal file
View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

220
lab/lab9/Question6/Car.java Normal file
View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

212
lab/lab9/Question7/Car.java Normal file
View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}