Problems

  1. Red, White, and Blue (10 points)

    Problem 6.16 (p 471): Write the code for an applet that has three buttons labeled, Red, White, and Blue. When a button is clicked, the background of the applet changes to the color named on the button.

  2. Light (10 points)

    Problem 6.17 (p 471): Create an applet Light that simulates a simple light. Create one button labeled On/Off. As you click this button, the background color will change between DARK_GRAY and YELLOW.

  3. Inventory (20 points)

    Write a class Item that stores information about an object in a store. It should have variables for:

    • the name of the item
    • the full price of the item
    • the sales price of the item


    Each price of the item should be stored as a double.

    Write two constructors for this class: one which has the name of the item, the full price, and the sales price as parameters, and one which only has the name of the item and the full price as parameters. In the second case, the sales price should be set to half the full price.

    Write a static method called format which takes a double as its parameter and returns a string representing the double as a price. Specifically, put a $ in front of the double value, and truncate (or round) the value to have exactly 2 decimal places. If the double value is less than 0, return $0.00. If the double value only has one decimal place, add a 0 at the end (ex. 6.7 becomes $6.70)

    Write a method toString() which displays information about the item in the following format:

    Item: item_name Full price: formatted_full_price Sales price: formatted_sales_price

    where formatted_full_price is the full price of the item, formatted by calling the method format on it, and formatted_sales_price is the sale the sales price of the item, formatted by calling the method format on it.

    Write a driver program that instantiates several objects of type Item and demonstrates the class. It should test the method format by calling it on at least 3 different cases of doubles.

Submitting Your Problem Set

You should submit all problems for grading by the deadline (see class schedule for date). To submit a program, you need to demonstrate (during office hours or lab) your program and explain how it works.