Java

OOP GUIDE / WORK

 

ROOM CLASS

 

Topics

  • Instance variables, constructors and instance methods
  • Data encapsulation

 

 

TASK – PART 1 – ROOM CLASS

 

You will create a class named Room that will represent a room in a school.  Here is the class specification:

 

INSTANCE VARIABLES

 

You need to determine the best type for each of the following private instance variables:

 

  • roomNo – The number of the room.

  • roomType – A one-word description of the room such as classroom, washroom or office.

  • roomArea – The area of the room in square meters.

  • hasVentilation – Holds whether the room as ventilation or not.


CONSTRUCTOR

 

  • There is a constructor that gets a value for each instance variable.

  • Optional: There is a 2nd constructor that gets five parameters.  It is similar to the first constructor but instead of getting a value for roomArea, it gets two parameters for width and length of the room.  The area is then calculated.

 

INSTANCE METHODS

 

Implement the following instance methods:

 

  • toString()

    Returns a string similar to

         "Room 101, classroom, 56 sqmeters, ventilated"

    or

         "Room 188, office, 9 sqmeters, not ventilated"

  • Set methods for all four instance variables.  The setArea method will only change the area if the provided value is positive.  So a negative value will be ignored.

  • Get methods for all four instance variables.

 

 

TASK – PART 2 – ROOMTESTER CLASS

 

Create a RoomTester class.  Inside main, create a few Room objects.  Test all of the methods in the Room class.