For introductory Java, I'm creating a Door class and a DoorTester class. Essentially, we're experimenting with instance variables and creating public methods. I've made the door class as follows, but my DoorTester returns "null" when it looks for .getState
Door.java
public class Door {
// Create instance variables of type String
private String name;
private String state;
// Declare method 'open' and 'close'
public void open() {
state = "open";
}
public void close() {
state = "closed";
}
// Add a constructor for the Door class
public Door(String name, String state) {
}
// Create an accessor of 'state'
public String getState() {
return name;
}
// Set the state
public void setState(String newState) {
state = newState;
}
}
DoorTester.java
public class DoorTester {
public static void main(String[] args) {
Door frontDoor = new Door("Front", "open");
System.out.println("The front door is " + frontDoor.getState());
System.out.println("Expected: open");
Door backDoor = new Door("Back", "closed");
System.out.println("Expected: closed");
// Use the mutator to change the state variable
backDoor.setState("open");
System.out.println("The back door is " + backDoor.getState());
System.out.println("Expected: open");
// Add code to test the setName mutator here
}
}