The behavior of the light includes turning it on or off. But we can also represent changing the batteries or the bulb. Some flashlights have controls to vary the amount of light or controls to focus the light beam.
Some of the properties of the object are available to the user of the object. In this example, it would include the on-off state and whether light came out. It would not include the power levels in the batteries or whether the reason no light came out was because the bulb was bad or the batteries were dead. A class is a description of an object. It specifies the kinds of data and behavior that one of these things might have. The class doesn't represent and actual flashlight, just the general nature of flashlights. All flashlights have a bulb and batteries and a way to switch it on and off. But there are a wide variety of ways to accomplish this. When we construct an actual flashlight object, we produce an instance of a flashlight.
One of the difficulties of object oriented programming and design is to decide what the objects are. In the case of a flashlight, is it one object or several? Maybe I need separate objects for bulbs and batteries? This seemed likely since I said there were several different kinds. But then what about switches and cases? Let's look at some example code implementing one solution to the problems described here.
/** * This represents a light bulb * * @author Kent Archie * @version 1.0 */ public class Bulb { public final static int TUNGSTEN=0; // ordinary tungsten type bulb public final static int KRYPTON=1; // special krypton bulb private final int DEFAULTRATING=6000; // lifetime in minutes private int type; // what kind of bulb is this private boolean status; // is it good (true) or bad (false) private int runtime; // how long has it been running private int rating; // how long is it good for /** * Constructor for objects of class Bulb */ public Bulb(int ntype) { type = ntype; status = true; runtime = 0; switch(type) { case TUNGSTEN: rating = DEFAULTRATING; break; case KRYPTON: rating = 3000; break; default: rating = DEFAULTRATING; break; } // esac } // Bulb /** * Constructor for objects of class Bulb */ public Bulb() { type = TUNGSTEN; status = true; runtime = 0; rating = DEFAULTRATING; } // Bulb /** * Get the bulbs type * * @return the type */ public int getType() { return type; } // getType /** * Get the bulbs status * * @return the status */ public boolean getStatus() { return status; } // getStatus /** * Get the bulb status as a string * * @return the status as a string */ public String statusString() { return status ? "good" : "bad"; } // statusString /** return string form of the Bulb type */ public String toString() { switch(type) { case TUNGSTEN : return("Tungsten"); // break; case KRYPTON : return("Krypton"); // break; default : return("iduno"); // break; } // esac } // toString /** set the status to false to indicate the bulb is burned out */ private void burnout() { status = false; } // burnout public boolean checkBulb(int time) { runtime += time; if(runtime > rating) { burnout(); return false; } else return true; } // checkBulb } // BulbThe above is the declaration of a class. It is used inside another class like this.
import Bulb; import Switch; import Battery; /** * This represents a flashlight * * @author Kent Archie * @version 1.0 */ public class Light { private final int DEFAULTBATTS=2; private Bulb bulb; private Switch toggle; private Battery[] battery; private int numbatts; // how many batteries private int battype; // what kind are they /** * This is a multi-argument constructor * * @param bulbtype Bulb type * @param switchtype Switch type * @param nbatts Number of batteries * @param batts Kind of battery */ public Light(int bulbtype, int switchtype, int nbatts, int type) { numbatts = nbatts; battype = type; bulb = new Bulb(bulbtype); toggle = new Switch(switchtype); changeBatteries(); } // Light /** * Constructor for objects of class Light */ public Light() { numbatts = DEFAULTBATTS; bulb = new Bulb(Bulb.TUNGSTEN); toggle = new Switch(Switch.SLIDE); changeBatteries(); } // Light /** toggle the light switch */ public void pressSwitch() { toggle.toggle(); } // pressSwitch /** replace the batteries with new ones of the same type also used to initially put batteries in The GC takes care of the old one */ public void changeBatteries() { battery = new Battery[numbatts]; for(int i=0; i < numbatts; i++) battery[i] = new Battery(battype); } // changeBatteries /** return string form of the Light type */ public String toString() { return( "The bulb type is " + bulb + "\nthe switch type is " + toggle + "\nthere are " + numbatts + " " + battery[0] + " batteries \nit is " + toggle.statusString()); } // toString } // LightThe line
bulb = new Bulb(bulbtype);is the definition of an instance called bulb. Each instance of a class has a new instantiation of the data variables. If a data variable is declared static, then all the instances share the value of that variable.