Classes within a class?

edited October 2017 in Questions about Code

Hello,

I don't really know how to create a class within another class. For example:

class Houses  {  
  color colour;  
  int size;  
  int housenumber;  

  class Rooms  {  
   color colour;  
   int windows;  
   boolean wifi;  
  }  

  void build()  {  
    // ...  
  }  
}  

// Now I want to something like this:  
MyHouse.Bathroom.wifi = true;  
MyHouse.colour = #FFFFFF;  
MyHouse.build();  
HouseOfMyNeighbour.housenumber = 5;`  

I still have read many examples but all didn't work. There were problems in declaring the objects of the classes.

Answers

  • edited October 2017

    https://Forum.Processing.org/two/discussion/8042/processing-forum-rules-and-advices

    • Under Java's naming conventions, variables should follow the lowerCamelCase.
    • So housenumber should be instead houseNumber, MyHouse be myHouse, HouseOfMyNeighbour be houseOfMyNeighbour, etc.
    • Classes & interfaces follow instead the UpperCamelCase naming convention.
    • Besides that, in general, classes & interfaces should represent a single entity.
    • So instead of Houses, rename it to House. Instead of Rooms, call it Room.
    • Although it's possible to nest classes & interfaces inside a class, that's not very appropriate for the majority of cases.
    • You're much better off defining your classes as their own.
    • Then use a technique called inheritance by composition.
    • That is, a House is composed of 1 or more Room objects.
    • Then you can declare a container field in your House class to represent that composition: Room[] rooms;

    class House {
      color colour;
      int size;
      int houseNumber;
    
      Room[] rooms;
    }
    
    class Room {
      color colour;
      int windows;
      boolean wifi;
    }
    
  • edited October 2017
    class Room {  
      color colour;  
      int windows;  
      boolean wifi;  
    }  
    
    class House {  
      color colour;  
      int size;  
      int houseNumber;  
    
      Room[] bathroom;  
      Room[] bedroom;  
    }  
    
    House myHouse = new House();  
    myHouse.bathroom.wifi = true;  
    

    Thank you! But now it says "The global variable 'wifi' does not exist".

  • edited October 2017

    Your bathroom is not a Room - it is an array of Rooms. This is not what you want.

    Think of an array as a list, like a shopping list. The name of the array is the list itself. "Dairy products". "Meats". The items on the list are specific objects. "Cheese". "Steak".

    You've labeled a list of Rooms as "bathroom". But this list has no Rooms on it!

    What you want is for your House to have a list of Rooms - called rooms - and then a specific item on this list is going to be the bathroom. if bathroom is 0, then rooms[bathroom] is rooms[0] which is the fist room on the list.

    Or, you can forget about your House having a list of rooms, and just have specific rooms. Room bathroom; Room bedroom; Room hallway;

    Or you can have lists of bathrooms and bedrooms. Maybe it is a three bedroom, two bathroom house:

    Room[] bathrooms = new Room[2];
    Room[] bedrooms = new Room[3];
    bedrooms[1].wifi = true;
    
  • edited October 2017

    Thank you for your help! But if I run the following code it says NullPointerException in the last two lines:

    class Room {  
      color colour;  
      int windows;  
      boolean wifi;  
    }  
    
    class House {  
      color colour;  
      int size;  
      int houseNumber;  
    
      Room bathroom;  
    }  
    
    House myHouse = new House();  
    myHouse.bathroom.wifi = true;  
    println(myHouse.bathroom.wifi);
    
  • Answer ✓

    That is because you have created a new House but you have not create the Bathroom. Change the last line to

    Room bathroom = new Room();

  • edited October 2017 Answer ✓

    You want an array of rooms

    You need setup and draw as functions

    You can give the rooms in the array names

    Have you read the tutorial on objects and arrays?

    https://www.processing.org/tutorials/objects/

    Missing

    myHouse.bathroom=new Room(); 
    

    is also missing (when it’s not an array yet)

    Do this in function setup()

  • Thank you so much for your help! Now it works.

  • Could you please post your entire code for others?

  • I will try. But the problem is that I used the example with the houses and rooms instead of other things in a quite difficult code. So it will take some time.

  • edited October 2017

    here is my version with an array of rooms which makes it easier if you have more than 2 rooms in a house:

    House myHouse = new House();
    
    void setup() {
    
      size (700, 800);
    
      myHouse.rooms[0].name="Bathroom";
      myHouse.rooms[0].wifi = true;
      myHouse.rooms[0].isDefined = true;
    
      myHouse.rooms[1].name="Bedroom";
      myHouse.rooms[1].wifi = false;
      myHouse.rooms[1].isDefined = true;
      myHouse.rooms[1].colour=color(5, 0, 255);   
    
      myHouse.rooms[2].name="Child 1";
      myHouse.rooms[2].wifi = false;
      myHouse.rooms[2].isDefined = true;
      myHouse.rooms[2].windows=5;
      myHouse.rooms[2].colour=color(5, 210, 255);   
    
      background(9);
    }//function
    
    void draw() {
      background(9);
      myHouse.display();
    }//function
    
    // ==========================================
    
    class House {  
    
      // default values for a house
      color colour=color(0, 255, 0);  
      int size=310;  
      int houseNumber=16;  
      String street="Mallow Road";
      String name="House of family Stone";
    
      // array of rooms in the house
      Room[] rooms = new Room [22];
    
      // constructor
      House () {
        // pre init rooms 
        for (int i =0; i< rooms.length; i++) {
          rooms[i] = new Room();
        }
      }//constructor
    
      // method
      void display() {
        // display house
        fill(colour);
        text(name+"\n"+street+" "+houseNumber, 44, 44);
    
        float verticalDistanceRooms = 60; 
    
        int i=0;
        for (Room currentRoom : rooms) {
    
          if (currentRoom.isDefined) {
            currentRoom.display(244, i*verticalDistanceRooms+88 );
    
            stroke(255);
            // horizontal line
            line (66, i*verticalDistanceRooms+88, 
              210, i*verticalDistanceRooms+88); 
            // vertical line
            line (66, 0*verticalDistanceRooms+88-20, 
              66, i*verticalDistanceRooms+88); 
    
            i++;
          }
        }//for
      }//method
      //
    }//class 
    
    // =====================================================
    
    class Room {  
    
      // default values for a room 
      color colour=color(255, 0, 0);   
      int windows=2;  
      String name="";
    
      boolean wifi=false;
      boolean isDefined=false;
    
      // no constructor
    
      // method 
      void display(float x, float y) {
        // display room
    
        // check wifi 
        String hasWifiText = "has no wifi"; // default text
        if (wifi) {
          hasWifiText = "has wifi"; // text for room that has wifi
        } 
    
        // display room using color, name and wifi text etc.
        fill(colour);
        text(name
          +"\n"
          +hasWifiText
          +"; has "
          +windows
          +" windows.", 
          x, y);
      }
      //
    } //class
    //
    
Sign In or Register to comment.