Tools: VSCode | Source Code: GitHub
The SmartDoorLock program is designed to determine whether it is safe to enter a room based on the status of a smart door lock and the number of people inside. The program is encapsulated within the SmartDoorLock class, which contains three private instance variables: locked, peopleInside, and comeIn. The locked variable represents whether the door is locked or not, while peopleInside counts the number of individuals currently in the room. The comeIn variable indicates whether it is safe for a new person to enter.
In the constructor of the SmartDoorLock class, the door's locked status and the number of people inside are initialized, allowing the program to create a door object with specific attributes. The main functionality of the program is found in the isSafeToEnter() method. This method checks two conditions: whether the door is unlocked (not locked) and whether the number of people inside is less than five. If both conditions are met, it sets comeIn to true, indicating it is safe to enter. If the door is locked or there are five or more people inside, it sets comeIn to false.
The main method serves as the entry point for the program, where a SmartDoorLock object is created with the door unlocked and three people inside. The program then calls the isSafeToEnter() method and prints the result, which indicates whether it is safe to enter.
Overall, this program demonstrates key concepts of object-oriented programming in Java, including class definition, object instantiation, and method implementation. It also highlights the use of conditional statements to evaluate specific criteria for safety, emphasizing logical operations with boolean values. This project reinforces the understanding of encapsulation and showcases how to structure code effectively to handle specific tasks related to assessing safety for entering a space.
public class SmartDoorLock {
private boolean locked;
private int peopleInside;
private boolean comeIn;
public SmartDoorLock(boolean locked, int peopleInside) {
this.locked = locked;
this.peopleInside = peopleInside;
}
public boolean isSafeToEnter() {
boolean doorTrueFalse = this.locked;
int peopleInsideDoor = this.peopleInside;
if (doorTrueFalse == false && peopleInsideDoor < 5) {
comeIn = true;
} else if (doorTrueFalse == true || peopleInsideDoor >= 5) {
comeIn = false;
}
return comeIn;
}
public static void main(String[] args) {
SmartDoorLock door = new SmartDoorLock(false, 3);
System.out.println(door.isSafeToEnter());
}
}