Java

OOP GUIDE / WORK

 

LAMP CLASS SOLUTIONS

 

 

TASK – PART 1 – LAMP CLASS WITHOUT ENCAPSULATION

 

Solution:

 

 

public class Lamp

{

     //INSTANCE VARIABLES (DATA FIELDS)

     public boolean isOn;

     public int onCounter;

    

     //CONSTRUCTOR(S)

     public Lamp()

     {

           isOn = false;

           onCounter = 0;

     }

    

     //INSTANCE METHOD(S)

     public String toString()

     {

           String part1;

           if (isOn == true)

                part1 = "The lamp is ON.  ";

           else

                part1 = "The lamp is OFF.  ";

          

           String part2 = "It has been on " + onCounter + " times.";

                     

           return part1 + part2;

     }

}

 

 

 

 

TASK – PART 2 – TESTING THE LAMP CLASS

 

Solution:

 

 

public class LampTester

{

     public static void main(String[] args)

     {

           Lamp pixar = new Lamp();

           System.out.println(pixar);

          

           pixar.isOn = true;

           pixar.onCounter++;

          

           pixar.isOn = false;

          

           pixar.isOn = true;

           pixar.onCounter++;

          

           pixar.isOn = true;  //was already true, so no counter increase

          

           //Note that this is tricky.  We have to remember to

           //increase onCounter when we set isOn to true (unless

           //it was already true before).  Yuck!

          

           System.out.println(pixar);

          

           //There must be a better way!!!  Hint hint!  :)

     }

}

 

 

 

TASK – PART 3 – SETUP

 

No work to show

 

 

TASK – PART 4 – ENCAPSULATED LAMP CLASS

 

Solution for Lamp class:

 

 

public class Lamp

{

     //INSTANCE VARIABLES (DATA FIELDS)

     private boolean isOn;

     private int onCounter;

    

     //CONSTRUCTOR(S)

     public Lamp()

     {

           isOn = false;

           onCounter = 0;

     }

    

     //INSTANCE METHOD(S)

     public String toString()

     {

           String part1;

           if (isOn == true)

                part1 = "The lamp is ON.  ";

           else

                part1 = "The lamp is OFF.  ";

          

           String part2 = "It has been on " + onCounter + " times.";

                     

           return part1 + part2;

     }

    

     public void switchOn()

     {

           if (isOn == false)

           {

                isOn = true;

                onCounter++;

           }

     }

    

     public void switchOff()

     {

           isOn = false;

     }

    

     public boolean getIsOn()

     {

           return isOn;

     }

    

     public int getOnCounter()

     {

           return onCounter;

     }

    

     public String getState()

     {

           if (isOn == true)

                return "ON";

           else

                return "OFF";

     }

}

 

 

Solution for LampTester class:

 

 

public class LampTester

{

     public static void main(String[] args)

     {

           Lamp pixar = new Lamp();

           System.out.println(pixar);

          

           pixar.switchOn();

           pixar.switchOn();   //does nothing

           pixar.switchOff();

           pixar.switchOn();

          

           System.out.println(pixar);

          

           System.out.println(pixar.getIsOn());       

           System.out.println(pixar.getOnCounter());       

           System.out.println(pixar.getState());

     }

}