Question
Write a subclass of Product called Lightbulb. In addition to description and price, the constructor takes an int parameter, lumens which is an indication of
Write a subclass of Product called Lightbulb. In addition to description and price, the constructor takes an int parameter, lumens which is an indication of how bright the light produced by the bulb is. You will write the whole class - no starter. Add a method getLumens() to get the number of lumens produced by this bulb. Add a new method isReadingLight() which determines if the light is bright enough to read by. If so it returns true. Otherwise, false. To be considered a reading light, the lightbulb must produce 1000 lumens or more. Define and use a public constant called READING_LIGHT Override the getDescription() method to include the value of the new instance variable (lumens). Cautions: In Lightbulb's getDescription method, remember about super Remember not to redefine the instance variables from Product You should not change Product class in any way
WRITE THE CODE IN JAVA
Use the following files:
LightbulbTester.java
public class LightbulbTester { public static void main(String[] args) { Lightbulb light1 = new Lightbulb("GE", 20.95, 1000); //if this does not pass Codecheck, //Lightbulb is not a subclass of Product System.out.println("Is subclass: " + (light1 instanceof Product)); System.out.println("Expected: true"); //if this gets a comoile error, you need to //define the constant in Lightbulb System.out.println("Constant: " + Lightbulb.READING_LIGHT); System.out.println("Expected: 1000"); System.out.println("Description: " + light1.getDescription()); System.out.println("Expected: GE 1000"); System.out.println("Lumens: " + light1.getLumens()); System.out.println("Expected: 1000"); System.out.println("Reading: " + light1.isReadingLight()); System.out.println("Expected: true"); light1 = new Lightbulb("Sylvania", 21.95, 800); System.out.println("Description: " + light1.getDescription()); System.out.println("Expected: Sylvania 800"); System.out.println("Lumens: " + light1.getLumens()); System.out.println("Expected: 800"); System.out.println("Reading: " + light1.isReadingLight()); System.out.println("Expected: false"); light1 = new Lightbulb("Home Depot", 18.95, 1100); System.out.println("Description: " + light1.getDescription()); System.out.println("Expected: Home Depot 1100"); System.out.println("Lumens: " + light1.getLumens()); System.out.println("Expected: 1100"); System.out.println("Reading: " + light1.isReadingLight()); System.out.println("Expected: true"); } }
Product.java
/** * Models a Product that can increase and decrease in price */ public class Product { private double price; private String description; /** * Constructs a Product with a price and a description * @param thePrice the price of this Product * @param theDescription - the description of this product */ public Product(String theDescription, double thePrice) { price = thePrice; description = theDescription; } /** * Gets the price * @return the price of this Product object */ public double getPrice() { return price; } /** * Gets the description * @return the description of the Product object */ public String getDescription() { return description; } /** * Reduces the price of this product by the give percentage * @param percent the percentage to reduce the priice by */ public void reducePrice(double percent) { double reduction = price * percent / 100; price = price - reduction; } /** * Increases the price by the given percent * @param percent the percent to increase the price by */ public void increasePrice(double percent) { double increase = price * percent / 100; price = price + increase; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started