The goal for this exercise is to create your class, that has per-object memory (i.e., that has instance variables).
What you need to do to prepare for this exercise:
Within the provided starter project, there is already a class named Instance_Variables
. You should create
a class named Refrigerator
below it, in the space indicated.
Within the Refrigerator
class, we will define some variables that describe the state of a particular
refrigerator. For example, all refrigerators have a currentInternalTemperature
, which is the temperature
that the inside of the refrigerator is currently at (as opposed to the temperature of the outside surface of
the refrigerator). It makes sense that all refrigerators have some minimumInternalTemperature
, which
is the lowest temperature that the refrigerator can achieve. Furthermore, any given refrigerator is either turned
on (and actively drawing power, and refrigerating), or it’s not (perhaps it’s being defrosted). You should choose
an appropriate name (and appropriate data type) to store this last piece of data.
For all of these attributes, you should define an instance variable (a.k.a. a data member) within the class to store the needed information. For now, just make everything public, so that you can get to it easily. For example, you might put
public double currentInternalTemperature;
inside the Refrigerator
class (but OUTSIDE of any methods).
Once you’ve done that (and that all compiles), you should go back to Instance_Variables.RunExercise
,
and create (at least) two instances of the Refrigerator
class. We’re creating more than one specifically
to make sure that you’re clear on the fact that instance variables are per-object memory: each
instance that you create will have it’s own, individual copy of all of the instance variables that you’ve defined
for the class. So your first refrigerator can have a minimum temperature of -10 degrees, a current temperature
of +3 degrees, and is currently switched on, while the refrigerator next to it can have a minimum temperature
of -30 degrees, a current temperature of 50 degrees, and it’s switched off.
So, you’ll need to set the values of all of the instance variables, for each of the instances that you’ve created.
Once you’ve done that, you should then print them all out so the user can see those values, using a Print()
method
that you write.
Create the Refrigerator
class.
Add the instance variables to it that are described above.
Within Instance_Variables.RunExercise
, create (at least) 2 instances of the Refrigerator
class.
Remember that you will need to call the Instance_Variables.RunExercise
method from the Main
function in the Program class.
Within Instance_Variables.RunExercise
, assign reasonable values to each of the attributes
on each of the instances that you’ve created
Within Instance_Variables.RunExercise
, print out all of the attributes of each of the instances
that you’ve created
Within Instance_Variables.RunExercise
, call the Print()
method on all the instances that you’ve created, which prints out all the instance variables to the Console.