Pages

PA11-APCSA-17-18

Due Date, Submitting Your Work, Grading

This is an individual assignment. It is due on Saturday, 10 March 2018 at 11:59pm. Source code for your fully functioning program should be emailed to jspurgeon@vcstudent.org. The subject of your email should be Programming Assignment 11. A printed copy of the code you submit, including sample output, must be turned in no later than Friday, 16 March 2018. You will receive a literacy and a correctness score for this assignment; each will be worth 100 points.

Complete the following program.

Do not modify the code below. Add to it as described on the next page.
public class Order
{
    private static final Orderable[] menu =
    {
        new Hotdog(50),   // regular
        new Hotdog(75),   // foot-long
        new Popcorn(25),  // small
        new Popcorn(40),  // medium
        new Popcorn(75),  // large
        new CandyBar(18), // bite-size
        new CandyBar(60), // regular
        new SodaPop(350), // small
        new SodaPop(500), // medium
        new SodaPop(700)  // lethal
    };
    
    public static void main(String[] args)
    {
        final int n = args.length;
        int i = 0;
        double sum = 0;
        while (i < n)
        {
            final int qty = Integer.parseInt(args[i++]);
            final int itemNo = Integer.parseInt(args[i++]);
            final Orderable item = menu[itemNo];
            final double pricePerItem = item.price();
            final double price = pricePerItem * qty;
            System.out.print(qty + " x ");
            System.out.print(item);
            System.out.println(" = $" + price);
            sum += price;
        }
        System.out.println("Total: $" + sum);            
    }
}
Requirements
  1. Define an interface called Orderable that consists of two method declarations:
    1. A method called description, defined in terms of zero parameters, must return a String.
    2. A method called price, defined in terms of zero parameters, must return a double.
  2. Define an abstract class called MenuItem that implements the Orderable interface.
    1. The class should have exactly one constructor method that is defined in terms of two parameters. The first parameter should be of type double. (Its value represents the weight of an item measured in grams.) The second parameter should be of type double. (Its value represents the item's price per gram measured in cents.)
    2. The class should implement the price method. The value returned should be the item's weight in grams multiplied by its price in cents per gram divided by 100.
    3. The class should not implement the description method. (It may explicitly declare that the method is abstract.)
    4. The class should override the standard toString method; the String value returned should be the weight of the item in grams followed by a space, followed by a lowercase g, followed by a space, followed by the value return by the description method.
    5. The class should have private final instance variables that hold the values of the object's weight in grams and its price per gram in cents.
  3. Define an abstract class called TaxedMenuItem that extends MenuItem.
    1. The class should have exactly one constructor method that is defined in terms of three parameters. The first parameter should be of type double. (Its value represents the weight of an item measured in grams.) The second parameter should be of type double. (Its value represents the item's price per gram measured in cents.) The third parameter should be of type double. (Its value should be greater than 1 and represents the item's tax rate.)
    2. The constructor method should call the constructor method of the MenuItem class.
    3. The class should override the price method. The value returned should be the value returned by the MenuItem class's price method multiplied by the object's tax rate.
    4. The class should have a private final instance variable that holds the value of the object's tax rate.
  4. Define classes Hotdog and Popcorn that extend MenuItem and CandyBar and SodaPop that extended TaxedMenuItem. The cents/gram price ratios (and tax rates if applicable) for the items represented by these classes are 1.3, 5.0, 2.2 (1.15), and 0.25 (1.2) respectively. (Corrected on Thursday 3/8. Good catch, Jerry!)
Example

Arguments:    2 0 1 1 3 2 2 3 1 4 2 5 1 6 1 7 1 8 1 9

Given the command-line arguments shown above, your program should output:

2 x 50.0 g Hotdog = $1.3
1 x 75.0 g Hotdog = $0.975
3 x 25.0 g Popcorn = $3.75
2 x 40.0 g Popcorn = $4.0
1 x 75.0 g Popcorn = $3.75
2 x 18.0 g Candy Bar = $0.9107999999999999
1 x 60.0 g Candy Bar = $1.518
1 x 350.0 g Soda Pop = $1.05
1 x 500.0 g Soda Pop = $1.5
1 x 700.0 g Soda Pop = $2.1
Total: $20.853800000000003