Code
Refrigerator fridge = new Refrigerator();
PImage fridgeClosed;
PImage fridgeOpen;
PImage apple;
void setup(){
size(400,500);
fridgeClosed = loadImage("closed.jpg");
fridgeOpen = loadImage("open.jpg");
apple = loadImage("apple.jpg");
image(fridgeClosed, 0, 0);
}
void draw(){
fridge.render();
}
void keyPressed(){
switch(key){
case 'o':
fridge.openDoor();
break;
case 'c':
fridge.closeDoor();
break;
case '-':
fridge.removeItem();
break;
case '+':
fridge.addItem();
break;
}
}
//===============================================
class Refrigerator {
int nItems;
boolean bDoorOpen;
Refrigerator(){
nItems = 0;
bDoorOpen = false;
}
void render(){
if (isDoorOpen()){
image(fridgeOpen, 0, 0);
for (int i=0; i<nItems; i++){
fill(200,0,0);
noStroke();
image(apple,142+(22*i),320);
}
}
else{
image(fridgeClosed, 0, 0);
}
}
boolean isDoorOpen(){
return bDoorOpen;
}
void openDoor(){
bDoorOpen=true;
}
void closeDoor(){
bDoorOpen=false;
}
int getNItems(){
return nItems;
}
void addItem(){
if(isDoorOpen()){
if(nItems<5){
nItems++;
}
else{
println("BUY MORE SHELVES.");
}
}
else{
println("DOOR MUST BE OPEN TO ADD AN ITEM.");
}
}
void removeItem(){
if(isDoorOpen()){
if(nItems>0){
nItems--;
}
else{
println("NOTHING IN FRIDGE TO REMOVE.");
}
}
else{
println("DOOR MUST BE OPEN TO REMOVE AN ITEM.");
}
}
}
0801 - More class exercises: Refrigerator revisited
Statement:Let's revisit this problem, to make sure we really understand how to make and use classes. Below is some code for a Refrigerator class. Your objective is to fill out this code, and invoke the methods of the refrigerator class, in order to accommodate the following list of requests:
- You should correctly declare, create and make requests to an instance of the Refrigerator class.
- You should be able to ask the fridge to display itself. If the fridge's door is closed, we should see it from the outside. But if the fridge's door is open, we should see all the contents inside the fridge.
- The user of your applet should also be able to open/close the door with the 'o' and 'c' keys.
- If the fridge's door is open, the user should also be able to add an item to the fridge, or take an item out of the fridge, by using the '-' and '+' keys. It should not be possible to add or remove items from the fridge if its door is closed.
//====================================================
// DECLARE A GLOBAL INSTANCE OF A REFRIGERATOR HERE.
void setup(){
size(300,300);
// CREATE THE GLOBAL INSTANCE OF A REFRIGERATOR HERE.
}
void draw(){
background(180);
// ASK THE FRIDGE INSTANCE TO DRAW ITSELF.
}
void keyPressed(){
switch(key){
case 'o':
// ASK THE FRIDGE TO OPEN THE DOOR.
break;
case 'c':
// ASK THE FRIDGE TO CLOSE THE DOOR.
break;
case '-':
// ASK THE FRIDGE TO REMOVE AN ITEM.
break;
case '+':
// ASK THE FRIDGE TO ADD AN ITEM.
break;
}
}
//===============================================
class Refrigerator {
int nItems;
boolean bDoorOpen;
Refrigerator(){
nItems = 0;
bDoorOpen = false;
}
void render(){
// IF THE DOOR IS CLOSED, SHOW THE OUTSIDE OF THE FRIDGE.
// BUT IF THE DOOR IS OPEN, SHOW THE STUFF INSIDE -- NAMELY,
// A CORRECTLY-DISPLAYED NUMBER OF ITEMS.
// WHAT GOES HERE?
}
boolean isDoorOpen(){
// ACCESSOR.
// WHAT GOES HERE?
}
void openDoor(){
// MUTATOR.
// WHAT GOES HERE?
}
void closeDoor(){
// MUTATOR.
// WHAT GOES HERE?
}
int getNItems(){
// ACCESSOR.
// WHAT GOES HERE?
}
void addItem(){
// MUTATOR.
// IT SHOULD ONLY BE POSSIBLE TO ADD AN ITEM
// IF THE DOOR IS ALREADY OPEN.
// OTHERWISE, PRINT OUT AN ERROR MESSAGE:
// "DOOR MUST BE OPEN TO ADD AN ITEM."
// WHAT GOES HERE?
}
void removeItem(){
// MUTATOR.
// IT SHOULD ONLY BE POSSIBLE TO REMOVE AN ITEM
// IF THE DOOR IS ALREADY OPEN.
// OTHERWISE, PRINT OUT AN ERROR MESSAGE:
// "DOOR MUST BE OPEN TO REMOVE AN ITEM."
// WHAT GOES HERE?
// DID YOU CHECK THAT nItems CAN'T BE LESS THAN ZERO?
}
}
hide statement