JOF 7-5
Sat Dec 07 2024 02:39:57 GMT+0000 (Coordinated Universal Time)
Saved by @John_Almer
package prisontest;
/**
*
* @author Almer
*/
public class Prisontest {
public static void main(String[] args){
cell cellA1 = new cell("A1", false, 1234);
prisoner bubba = new prisoner("Bubba", 2.08, 4, cellA1);
bubba.display();
cellA1.setIsOpen(1111);
cellA1.setIsOpen(1234);
cellA1.setIsOpen(1234);
}
}
package prisontest;
/**
*
* @author Almer
*/
public class prisoner {
private String name;
private double height;
private int sentence;
private cell cell;
//Constructor
public prisoner(String name, double height, int sentence, cell cell){
this.name = name;
this.height = height;
this.sentence = sentence;
this.cell = cell;
}
//Methods
public void think(){
System.out.println("I'll have my revenge.");
}
public void display(){
System.out.println("Name: " +getName());
System.out.println("Height: " +getHeight());
System.out.println("Sentence: " +getSentence());
System.out.println("Cell: " +getCell().getName());
}
public String getName() {
return name;
}
public double getHeight() {
return height;
}
public int getSentence() {
return sentence;
}
public cell getCell() {
return cell;
}
//Setters
public void setName(String name) {
this.name = name;
}
public void setHeight(double height) {
this.height = height;
}
public void setSentence(int sentence) {
this.sentence = sentence;
}
public void setCell(cell cell) {
this.cell = cell;
}
}
package prisontest;
public class cell {
private String name;
private boolean isOpen;
private int securityCode;
public cell(String name, boolean isOpen, int securityCode){
this.name = name;
this.isOpen = isOpen;
this.securityCode = securityCode;
}
public String getName(){
return name;
}
public boolean getIsOpen(){
return isOpen;
}
public void setIsOpen(int code){
if(code != securityCode){
System.out.println("Incorrect code");
}
else{
if(isOpen == true){
isOpen = false;
System.out.println("Cell " +name +" Closed");
}
else{
isOpen = true;
System.out.println("Cell " +name +" Open");
}
}
}
}



Comments