This commit is contained in:
ldy
2026-03-01 23:18:55 -05:00
commit 67f753a5d1
3087 changed files with 218259 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
public class BadCarException extends Exception {
public BadCarException() {
super("A car must have at least one door!");
}
}

View File

@@ -0,0 +1,6 @@
public class BadDoorException extends Exception {
public BadDoorException(int doorNumber) {
super("Door " + doorNumber + " does not exist!");
}
}

View File

@@ -0,0 +1,137 @@
public class Car {
//instance variable
private String name;
private Door[] doors;
//constructor
public Car(String name, int numberOfDoors) throws BadCarException {
this.name = name;
if(numberOfDoors > 1) {
this.doors = new Door[numberOfDoors];
for(int i = 0; i < this.doors.length; ++i)
doors[i] = new Door();
}
else throw new BadCarException();
}
//methods
public void listDoors() {
for(Door d: doors) {
if(d.isOpen())
System.out.println(this.name + ": door is open.");
else
System.out.println(this.name + ": door is closed.");
}
}
public int countOpenDoors() {
int cnt = 0;
for(Door d: doors)
if(d.isOpen()) ++cnt;
return cnt;
}
public void openOneDoor(int doorNumber) throws BadDoorException {
if(doorNumber > 0 && doorNumber <= this.doors.length)
this.doors[doorNumber - 1].open();
else throw new BadDoorException(doorNumber);
}
public void changeAllDoors() {
for(int i = 0; i < this.doors.length; ++i) {
if(this.doors[i].isOpen()) this.doors[i].close();
else this.doors[i].open();
}
}
public void replaceDoor(int doorNumber) throws BadDoorException {
if(doorNumber > 0 && doorNumber <= this.doors.length)
this.doors[doorNumber - 1] = new Door();
else throw new BadDoorException(doorNumber);
}
public void replaceAllDoors() {
for(int i = 0; i < this.doors.length; ++i) {
this.doors[i] = new Door();
}
}
public void replaceManyDoors(int numOfDoorsToReplace) {
//IndexOutOfBoundsException may occur when replace number > door number
for(int i = 0; i < numOfDoorsToReplace; ++i) {
this.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,27 @@
public class Door {
//instance variable
private boolean isOpen;
//constructor
public Door() {
this.isOpen = false;
}
//method
public boolean isOpen() {return this.isOpen;}
public void open() {this.isOpen = true;}
public void close() {this.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,9 @@
public class Start {
public static void main(String[] args) {
Door.testDoor();
Car.testCar();
}
}