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,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Question2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

Binary file not shown.

Binary file not shown.

Binary file not shown.

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