Code

//So the code works, but since I am using P3D, the stroke weight when the mouse is hovering
//over food does not appear. Also I realize that the "food" goes beyond the fridge space
//when there are over 6 items, I'm pretty sure I need a %6 somewhere but I never figured
//out how to get it to work

//Ok, so instead of following the instructions like what they actually said 
//I did what I thought they said
//and that was to give all the items a random tallness and then sort the tallness
//so using my code from 0901 I just changed the tallness to not be in order and give it
//a random tallness from 10 to 50 and then I used quickSort to sort all of the tallnesses
//and then it gives the groceries the sorted tallnesses so this gives it the same exact appearance as
//0901 rather and does not include the functionality of 0902

// DECLARE A GLOBAL INSTANCE OF A REFRIGERATOR HERE.
Refrigerator fridge;


void setup(){
  size(300,300, P3D);


  // CREATE THE GLOBAL INSTANCE OF A REFRIGERATOR HERE.
  fridge = new Refrigerator();



}

void draw(){
  background(180);
  // ASK THE FRIDGE INSTANCE TO DRAW ITSELF.
  fridge.render();
}

void keyPressed(){
  switch(key){
  case 'o': 
    // ASK THE FRIDGE TO OPEN THE DOOR.
    fridge.openDoor();
    break;
  case 'c':
    // ASK THE FRIDGE TO CLOSE THE DOOR.
    fridge.closeDoor();
    break;
  case '-':
    // ASK THE FRIDGE TO REMOVE AN ITEM.
    //if (fridge.bDoorOpen == true){
    fridge.removeItem();
    //}
    break;
  case '+':
    // ASK THE FRIDGE TO ADD AN ITEM.
    //if (fridge.bDoorOpen == true){
    fridge.addItem();
    //}
    break;
  }
}

//===============================================
class Refrigerator {

  boolean bDoorOpen;
  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.
  int tallness[];



  Refrigerator(){
    maxNGroceries = 10;
    nGroceries = 6;
    bDoorOpen = false;
    // ALLOCATE MEMORY FOR THE ARRAY
    groceries = new FoodItem[maxNGroceries]; 
    tallness = new int[maxNGroceries];
    for (int i=0; i < maxNGroceries; i++){
      // CREATE THE INDIVIDUAL ELEMENTS OF THE ARRAY
      tallness[i] = (int)random(10,50);
    }
    quickSort (tallness, 0, maxNGroceries-1);
    for (int i=0; i < maxNGroceries; i++){
       groceries[i] = new FoodItem(tallness[i]); 
    }
    
  }

  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?
    if (bDoorOpen == false){

      beginShape(QUADS);
      fill (255, 200, 200); 
      vertex ((width/4), (height/10), 0);
      fill (200, 100, 100); 
      vertex (((3*width)/4), (height/10), 0);
      fill (200, 100, 100); 
      vertex (((3*width)/4), ((3*height)/8), 0);
      fill (255, 200, 200); 
      vertex ((width/4), ((3*height)/8), 0);
      fill (200, 100, 100); 
      vertex (((3*width)/4), ((3*height)/8), 0);
      fill (255, 200, 200); 
      vertex ((width/4), ((3*height)/8), 0);
      fill (255, 200, 200); 
      vertex ((width/4), ((7*height)/8), 0);
      fill (200, 100, 100); 
      vertex (((3*width)/4), ((7*height)/8), 0);
      endShape(CLOSE);
    }
    else {
      beginShape(QUADS);
      fill (255, 200, 200); 
      vertex ((width/4), (height/10), 0);
      fill (200, 100, 100); 
      vertex (((3*width)/4), (height/10), 0);
      fill (200, 100, 100); 
      vertex (((3*width)/4), ((3*height)/8), 0);
      fill (255, 200, 200); 
      vertex ((width/4), ((3*height)/8), 0);
      fill (200, 100, 100); 
      vertex (((3*width)/4), ((3*height)/8), 0);
      fill (255, 200, 200); 
      vertex ((width/4), ((3*height)/8), 0);
      fill (255, 200, 200); 
      vertex ((width/4), ((7*height)/8), 0);
      fill (200, 100, 100); 
      vertex (((3*width)/4), ((7*height)/8), 0);
      endShape(CLOSE);
      fill (255); 
      rect ((width/4)+10, ((3*height)/8)+10, width/2-20, height/2-20);
      fill (255, 200, 200); 
      rect (((3*width)/4), ((3*height)/8), width/2, height/2);
      //==========================
      /* old code
       for (int i = 0; i < nItems; i++){
       //if (i%6!=0 || i==0){ //this is not it, 
       //but I'm not sure how to take the height mod 6 to get "rows"
       if (i < 6){
       ellipse ((width/4)+(20*i)+25, (((3*height)/8)+30), 10, 10);
       }
       else if (i > 6){ //works but only because nItems < 10
       ellipse ((width/4)+(20*(i%6))+5, (((3*height)/8)+30)+20, 10, 10);
       }
       //}
       }
       */
      // DO SOMETHING WITH EACH ELEMENT OF THE ARRAY
      for (int i=0; i < nGroceries; i++){
        float x = 100+i*20;
        float y = 160;
        groceries[i].draw(x,y);
      }




    }
  }





  //=========================

  boolean isDoorOpen(){
    // ACCESSOR.
    // WHAT GOES HERE?
    if (bDoorOpen == false){
      return false;
    }
    else{
      return true;
    }
  }


  void openDoor(){
    // MUTATOR.
    // WHAT GOES HERE?
    bDoorOpen = true;

  }

  void closeDoor(){
    // MUTATOR.
    // WHAT GOES HERE?
    bDoorOpen = false;
  }

  int getNGroceries(){
    // ACCESSOR. 
    // WHAT GOES HERE?
    return nGroceries;
  }

  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?
    if (bDoorOpen == true && nGroceries < maxNGroceries){
      nGroceries++;
    }
    else if (bDoorOpen == false){
      println ("Door must be open to add an item.");
    }
    else if (nGroceries >= maxNGroceries){
      println ("The fridge is full, take some items out to make space");
    }
  }

  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?
    if (bDoorOpen == true && nGroceries >= 0){
      nGroceries--;
    }
    else if (bDoorOpen == false){
      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));
  }
}

void quickSort (int data[], int leftIndex, int rightIndex){
  int mid;
  int temp;
 
  int i = leftIndex;
  int j = rightIndex;
  mid = data[(leftIndex + rightIndex)/2];
  do {
    while (data[i] < mid){
      i++;
    }
    while (mid < data[j]){
      j--;
    }
    if (i<=j){
      temp = data[i];
      data[i] = data[j];
      data[j] = temp;
      i++;
      j--;
    }
  } while (i<=j);
  if (leftIndex < j){
    quickSort (data, leftIndex, j);
  }
  if (i < rightIndex){
    quickSort (data, i, rightIndex); 
  }
}

0903 - Arrays of Objects: Keeping items sorted: Insertion

Statement:In this exercise, modify your previous example so that:

  • When the user presses the key to add an item to the fridge, a new FoodItem is created with a random height;
  • This new (randomly-sized) FoodItem is inserted into the array in the right place, so as to maintain the sorted order;
  • Pre-existing FoodItems which happen to be larger than the newly inserted FoodItem must be shifted down one place in the array (to the right);
  • If the array is full and a new item is inserted, decide whether to discard one of the items, or disallow the insertion...


hide statement