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>Question7</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,152 @@
import java.util.ArrayList;
public class Car {
//instance variable
private String name;
private ArrayList<Door> doors; //use the generic of ArrayList
//constructor
public Car(String name, int numberOfDoors) throws BadCarException {
this.name = name;
if(numberOfDoors > 1) {
this.doors = new ArrayList<Door>(); //create ArrayList<Door>
for(int i = 0; i < numberOfDoors; ++i)
this.doors.add(new Door()); //initiate
}
else throw new BadCarException(); //exception
}
//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.size())
this.doors.get(doorNumber - 1).open(); //get method of ArrayList
else throw new BadDoorException(doorNumber);
}
public void changeAllDoors() {
for(int i = 0; i < doors.size(); ++i) {
//get method of ArrayList
if(this.doors.get(i).isOpen()) this.doors.get(i).close(); //close
else this.doors.get(i).open(); //open
}
}
public void replaceDoor(int doorNumber) throws BadDoorException {
if(doorNumber > 0 && doorNumber <= this.doors.size())
this.doors.set(doorNumber - 1, new Door()); //set method of ArrayList
else throw new BadDoorException(doorNumber);
}
public void replaceAllDoors() {
for(int i = 0; i < this.doors.size(); ++i) {
this.doors.set(i, new Door());
}
}
public void replaceManyDoors(int numOfDoorsToReplace) {
for(int i = 0; i < numOfDoorsToReplace; ++i) {
this.doors.set(i, new Door());
}
}
public void expandCar() {
ArrayList<Door> tmp = new ArrayList<Door>(); //create a temporary ArrayList to store the original
for(int i = 0; i < this.doors.size()+2; ++i) { //loop 2 more
tmp.add(i, new Door()); //create
if(i < this.doors.size())
tmp.set(i, this.doors.get(i)); //initiate if has original
}
this.doors = tmp; //copy
}
//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,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();
}
}