Code
//Robert Kotcher
//0902 Arrays of Objects
FoodItem groceries[]; // DECLARE THE ARRAY OF OBJECTS
int maxNGroceries; // Will be used to keep the size of the array.
int nGroceries; // Indicates how many are actually used/shown.
Refrigerator fridge;
//=====================================================
void setup(){
size(150,200);
smooth();
fridge = new Refrigerator();
maxNGroceries = 10;
nGroceries = 5;
// ALLOCATE MEMORY FOR THE ARRAY
groceries = new FoodItem[maxNGroceries];
for (int i=0; i < maxNGroceries; i++){
// CREATE THE INDIVIDUAL ELEMENTS OF THE ARRAY
float tallness = 10+i*5;
groceries[i] = new FoodItem(tallness);
}
}
//=====================================================
void draw(){
background(100,50,0);
fridge.render();
}
//=====================================================
void keyPressed() {
switch(key) {
case 'o':
fridge.openDoor();
break;
case 'c':
fridge.closeDoor();
break;
case '+':
fridge.addItem();
break;
case '-':
fridge.removeItem();
break;
}
}
//=====================================================
class Refrigerator {
boolean bDoorOpen;
FoodItem groceries[];
int nFoodItems;
int maxFoodItems;
Refrigerator(){
nFoodItems = 6;
maxFoodItems = 9;
bDoorOpen = false;
// ALLOCATE MEMORY FOR THE ARRAY
groceries = new FoodItem[maxFoodItems];
for (int i=0; i < nFoodItems; i++){
// CREATE THE INDIVIDUAL ELEMENTS OF THE ARRAY
float tallness = 10+i*5;
groceries[i] = new FoodItem(tallness);
println("Food item stored at index " + i + " is : " + groceries[i].tallness+ " pixels tall");
}
println("There are " + nFoodItems + " food items");
//println("Food item stored at index " + nFoodItems + " is : " + groceries[nFoodItems].tallness+ " pixels tall");
}
void render(){
if (fridge.isDoorOpen()) {
fill(100);
rect(50,40,50,150);
// DO SOMETHING WITH EACH ELEMENT OF THE ARRAY
for (int i=0; i < nFoodItems; i++){
float x = 60 + 20*(i%2);
float y = 70 + int(i/2)*38;
groceries[i].draw(x,y);
}
}
else {
fill(255);
rect(50,40,50,150);
ellipse(55,115,5,5);
}
}
boolean isDoorOpen(){
if(bDoorOpen == false) {
println("The door is closed");
}
else{
println("The door is open");
}
return bDoorOpen;
}
void openDoor(){
bDoorOpen = true;
}
void closeDoor(){
bDoorOpen = false;
}
int getNItems(){
println("There are " + nFoodItems + "items in the fridge");
return nFoodItems;
}
void addItem(){
if(bDoorOpen == true && nFoodItems < maxFoodItems-1) {
groceries[nFoodItems] = new FoodItem(10+(nFoodItems)*5);
nFoodItems++;
//nGroceries++;
}
else {
println("Door must be open to add an item");
}
}
void removeItem(){
if((bDoorOpen == true) && (nFoodItems > 0) && (nFoodItems > 3)) {
for(int i = 3; i < nFoodItems-1; i++) {
groceries[i] = groceries[i+1];
}
nFoodItems--;
//nGroceries--;
}
else if ((bDoorOpen == true) && (nFoodItems <= 0)) {
println("There are no items in the fridge to take away");
}
else{
println("Door must be open to remove an item");
}
}
}
//=====================================================
class FoodItem {
float posx, posy;
float tallness;
color chroma;
int type;
//-----------------------------------
FoodItem (float t){
tallness = t;
chroma = color(random(100,255), random(128,255), random(0,100));
type = (int)random(0,3);
}
//-----------------------------------
void draw (float x, float y){
posx = x;
posy = y;
stroke(0);
if (isMouseHovering()){
strokeWeight(4);
}
else {
strokeWeight(1);
}
fill(chroma);
switch(type){
case 0: // RECT
rect(posx-6,posy-tallness, 12,tallness);
break;
case 1: // ELLIPSE
ellipse(posx,posy-tallness/2, 12,tallness);
break;
case 2: // TRIANGLE
triangle(posx-6,posy, posx+6,posy, posx,posy-tallness);
break;
}
}
//-----------------------------------
boolean isMouseHovering(){
return (
(mouseX > posx-6) &&
(mouseX < posx+6) &&
(mouseY < posy) &&
(mouseY > posy-tallness));
}
}
0902 - Arrays of Objects: Keeping items packed: deletion without gaps
Statement:In 0901, whenever we added a FoodItem, it was added to the end of the list. We did this by simply changing the number of displayed FoodItems. In this exercise, we will deal with the following complications:
- It should be possible to remove items from somewhere in the middle of the array. This ought to leave a gap... so,
- When this happens, the items to the right of the removed item shift down in the array (one place to the left), so that there are no remaining gaps between adjacent food items.