Restore
This commit is contained in:
10
Labs/Lab9/Lab9_2230026071/Question2/.classpath
Normal file
10
Labs/Lab9/Lab9_2230026071/Question2/.classpath
Normal 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>
|
||||
17
Labs/Lab9/Lab9_2230026071/Question2/.project
Normal file
17
Labs/Lab9/Lab9_2230026071/Question2/.project
Normal 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>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
BIN
Labs/Lab9/Lab9_2230026071/Question2/bin/BadCarException.class
Normal file
BIN
Labs/Lab9/Lab9_2230026071/Question2/bin/BadCarException.class
Normal file
Binary file not shown.
BIN
Labs/Lab9/Lab9_2230026071/Question2/bin/BadDoorException.class
Normal file
BIN
Labs/Lab9/Lab9_2230026071/Question2/bin/BadDoorException.class
Normal file
Binary file not shown.
BIN
Labs/Lab9/Lab9_2230026071/Question2/bin/Car.class
Normal file
BIN
Labs/Lab9/Lab9_2230026071/Question2/bin/Car.class
Normal file
Binary file not shown.
BIN
Labs/Lab9/Lab9_2230026071/Question2/bin/Door.class
Normal file
BIN
Labs/Lab9/Lab9_2230026071/Question2/bin/Door.class
Normal file
Binary file not shown.
BIN
Labs/Lab9/Lab9_2230026071/Question2/bin/Start.class
Normal file
BIN
Labs/Lab9/Lab9_2230026071/Question2/bin/Start.class
Normal file
Binary file not shown.
@@ -0,0 +1,6 @@
|
||||
|
||||
public class BadCarException extends Exception {
|
||||
public BadCarException() {
|
||||
super("A car must have at least one door!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
public class BadDoorException extends Exception {
|
||||
public BadDoorException(int doorNumber) {
|
||||
super("Door " + doorNumber + " does not exist!");
|
||||
}
|
||||
}
|
||||
87
Labs/Lab9/Lab9_2230026071/Question2/src/Car.java
Normal file
87
Labs/Lab9/Lab9_2230026071/Question2/src/Car.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
27
Labs/Lab9/Lab9_2230026071/Question2/src/Door.java
Normal file
27
Labs/Lab9/Lab9_2230026071/Question2/src/Door.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
Labs/Lab9/Lab9_2230026071/Question2/src/Start.java
Normal file
9
Labs/Lab9/Lab9_2230026071/Question2/src/Start.java
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
public class Start {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Door.testDoor();
|
||||
Car.testCar();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user