Problem #3: Lamps (40 Points) For this problem, you will be creating a derived c
ID: 3710182 • Letter: P
Question
Problem #3: Lamps (40 Points)
For this problem, you will be creating a derived class, base class and an interface. No Driver is required, but I have attached one to test with. A UML diagram for this problem has been provided to assist you.
Lamp Class:
Attribute:
Color of light emitted by Lamp
Methods:
get and set methods for attribute
toString(): returns a String describing the Lamp
constructor that takes in the Lamp's color of light
------------------------------------------------------------------
Flammable Interface: Flammable should be its own interface which has a:
Method header:
boolean startOnFire(): returns if the lamp has started on fire. Returns true if the lamp is on fire, false otherwise.
------------------------------------------------------------------
LavaLamp: LavaLamp should be its own class and should inherit from Lamp and implement Flammable. LavaLamp should have:
Attributes:
number of color blobs
Methods:
get and set methods for attribute
toString(): returns a String describing the LavaLamp
constructor which takes in the LavaLamp's light color
constructor which takes in the LavaLamp's light color and the number of color blobs in the LavaLamp
Flammable() returns true 3% of the time (must use Random object to generate this)
Grading Breakdown:
10 -- Correctly creating your Interface
10 -- Correctly creating your Base Object
10 -- Correctly creating your Derived Object
10 -- Correctly connecting your data using the correct keywords, etc.
Explanation / Answer
//Lamp.java
package lavalamp;
public class Lamp {
private String color;
public Lamp(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String value) {
color = value;
}
public String toString() {
return "This Lamp emits " + color + " color when on.";
}
}
//Flammable.java
package lavalamp;
public interface Flammable {
boolean startOnFire();
}
//LavaLamp.java
package lavalamp;
import java.util.Random;
public class LavaLamp extends Lamp implements Flammable {
private int noOfColorBlobs;
public LavaLamp(String color){
super(color);
}
public LavaLamp(String color, int noOfColorBlobs){
super(color);
setNoOfColorBlobs(noOfColorBlobs);
}
public int getNoOfColorBlobs() {
return noOfColorBlobs;
}
public void setNoOfColorBlobs(int value) {
noOfColorBlobs = value;
}
@Override
public String toString() {
return "This is a Lava Lamp which emits " + getColor() + " color when on fire.";
}
@Override
public boolean startOnFire() {
Random rand = new Random();
return rand.nextBoolean();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Lamp lamp = new Lamp("Yello");
System.out.println(lamp.toString());
LavaLamp lavaLamp = new LavaLamp("Maroon", 5);
System.out.println(lavaLamp.toString());
System.out.println(lavaLamp.startOnFire());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.