The Whole Program

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View The Whole Program as PDF for free.

More details

  • Words: 947
  • Pages: 7
The Whole Program---Again Here is the entire program, including everything we've learned so far: package lights; public interface Luminous { public abstract void turnOn(); public abstract void turnOff(); } package lights; import lights.Luminous; public class Lamp extends Object implements Luminous { /* Define a lamp that can turn on and off, report its state, report its state changes, have a wattage, have and report a name, and make sure it's off on creation. The class itself remembers the maximum possible wattage of any lamp and the default wattage of each lamp. */ //this lamp is either on or off private boolean lampIsOn; //this lamp may have a name private String name; //this lamp has a wattage private int wattage; //all lamps have a maximum possible wattage public final static int MAXIMUM_WATTAGE = 500; //all lamps have a default wattage public final static int DEFAULT_WATTAGE = 60; public Lamp() { /* Set this lamp's initial state as being a lamp.DEFAULT_WATTAGE-watt lamp in the off position. Give this lamp a generic name. */ lampIsOn = false; wattage = Lamp.DEFAULT_WATTAGE;

name = "Lamp"; } public Lamp(int wattage) { /* Set this lamp's initial state as being in the off position. Set this lamp's wattage to the given wattage. Do not let any lamp have a wattage higher than Lamp.MAXIMUM_WATTAGE. Give this lamp a generic name. */ lampIsOn = false; if (wattage > Lamp.MAXIMUM_WATTAGE) this.wattage = Lamp.MAXIMUM_WATTAGE; else this.wattage = wattage; name = "Lamp"; } public void turnOn() { /* Turn this lamp on. */ lampIsOn = true; reportState(); } public void turnOff() { /* Turn this lamp off. */ lampIsOn = false; reportState(); } public boolean isOn() { /* Report whether this lamp is on or off. */ return lampIsOn; } public void reportState() { /* Report this lamp's status.

*/ if (this.isOn()) { System.out.println(this.getName() + " is on"); System.out.println("My wattage is: " + wattage); } else System.out.println(this.getName() + " is off"); } public void setName(String name) { /* Name this lamp. */ this.name = name; } public String getName() { /* Report this lamp's name. */ return name; } public String toString() { /* Report this lamp's type and name. */

}

return "Lamp[" + this.getName() + "]"; }

package lights; import lights.Lamp; public class DimmerControlledLamp extends Lamp { /* Define a type of Lamp that can brighten and darken in stages, and compare its brightness with another lamp. */ //this lamp has a certain brightness, if it's on private int brightness; //maximum initial brightness for each dimmer-controlled lamp public final static int INITIAL_BRIGHTNESS = 3;

public DimmerControlledLamp() { /* Initialize this lamp's superclass portion, then initialize this lamp's brightness. */ super(); this.setBrightness(0); } public DimmerControlledLamp(int wattage) { /* Initialize this lamp's superclass portion, then initialize this lamp's brightness. */ super(wattage); this.setBrightness(0); } public void turnOn() { /* Turn this lamp on gradually until it is at DimmerControlledLamp.INITIAL_BRIGHTNESS. This method overrides the same method in the superclass. */ this.setBrightness(1); brighten(DimmerControlledLamp.INITIAL_BRIGHTNESS - 1); } public void turnOff() { /* Turn this lamp off gradually. This method overrides the same method in the superclass. */ darken(this.getBrightness()); } public boolean isOn() { /* Report whether this lamp is on. This method overrides the same method in the superclass. */ return (this.getBrightness() > 0); }

public void brighten() { /* If this lamp is on, make it brighter. */ if (this.isOn()) this.setBrightness(this.getBrightness() + 1); reportState(); } public void darken() { /* If this lamp is on, make it darker. */ if (this.isOn()) this.setBrightness(this.getBrightness() - 1); reportState(); } public void brighten(int brightnessIncrease) { /* Make this lamp brighter in steps. */ for (int index = 1; index <= brightnessIncrease; index++) brighten(); } public void darken(int brightnessDecrease) { /* Make this lamp darker in steps. */ for (int index = 1; index <= brightnessDecrease; index++) darken(); } protected void setBrightness(int brightness) { /* Let another lamp, any lamps of extended classes, or any objects in this package set this lamp's brightness. */ this.brightness = brightness; } protected int getBrightness() {

/* Report this lamp's brightness to another lamp, to any lamps of extended classes, or to any objects in this package. */ return brightness; } public boolean isBrighterThan(DimmerControlledLamp lamp) { /* Report whether this lamp is brighter than the given lamp. */ return (this.getBrightness() > lamp.getBrightness()); } public void reportState() { /* Report this lamp's status. This method overrides the same method in the superclass. */ if (this.isOn()) { System.out.println(this.getName() + " is on"); System.out.println("My brightness is: " + this.getBrightness()); } else System.out.println(this.getName() + " is off"); } public String toString() { /* Report this lamp's type and name. This method overrides the same method in the superclass. */

}

return "DimmerControlledLamp[" + this.getName() + "]"; }

import lights.Luminous; import lights.Lamp; import lights.DimmerControlledLamp; public class FiddleWithLights { /*

Create some lights and fiddle with them. */ public final static int NUMBER_OF_LAMPS = 2; public final static int NUMBER_OF_LIGHTS = 2; public static void main(String[] parameters) { //create an array of lamps reference variables Lamp[] lampArray; lampArray = new Lamp[FiddleWithLights.NUMBER_OF_LAMPS]; //create a 100-watt lamp lampArray[0] = new Lamp(100); lampArray[0].setName("Lamp number 0"); //for every other Lamp reference variable in the array, //create a new DimmerControlledLamp and name it for (int index = 1; index < lampArray.length; index++) { lampArray[index] = new DimmerControlledLamp(); lampArray[index].setName("Lamp number " + index); } //switch each lamp on and off for (int index = 0; index < lampArray.length; index++) { System.out.println(lampArray[index]); lampArray[index].turnOn(); lampArray[index].turnOff(); System.out.println(); } //create an array of Luminous reference variables Luminous[] lightsArray; lightsArray = new Luminous[FiddleWithLights.NUMBER_OF_LIGHTS]; //create some Luminous objects lightsArray[0] = new Lamp(); lightsArray[1] = new DimmerControlledLamp();

}

//switch each light on and off for (int index = 0; index < lightsArray.length; index++) { System.out.println(lightsArray[index]); lightsArray[index].turnOn(); lightsArray[index].turnOff(); System.out.println(); } }

Related Documents

The Whole Program
June 2020 1
Educating The Whole Student
December 2019 13
The Whole Truth?
May 2020 5
Bite The Whole Apple
November 2019 18
The Program
June 2020 3