0

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 
  }

}

mshades
  • 55
  • 7

3 Answers3

1

You will have to modify your constructor of Door class like

public Door(String name, String state) {
this.name=name;
this.state=state;
}

Actually name and state are not getting initialized. Also see this What is the meaning of "this" in Java?


Modified Code Snippet:

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) {
 this.name=name;
 this.state=state;
}

// Create an accessor of 'state'
public String getState() {
    return state;            //<<<<<<<----------also make an Edit here 
}

// Set the state
public void setState(String newState) {
    state = newState;
}
}
Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
0

Your getState() method doesn't return state, it returns name:

// Create an accessor of 'state'
public String getState() {
    return name; // <-- Simply change this
}

Also, your constructor doesn't set the fields. You need to do something like this:

// Add a constructor for the Door class
public Door(String name, String state) {
    this.name = name;
    this.state = state;
}
Keppil
  • 45,603
  • 8
  • 97
  • 119
0

There are issues in your Door class

public class Door {
private String name;
private String state;

public void open() {
    state = "open";
}

public void close() {
    state = "closed";
}

public Door(String name, String state) { // argument passed here need to set
// set like
  this.name=name;
  this.state=state;
}

public String getState() {
    return name;
}

public void setState(String state) { 
    state = state; // you need to use this.state=state
 }
}

Importance of this keyword in Java

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115