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
- Define an interface called
Orderablethat consists of two method declarations:- A method called
description, defined in terms of zero parameters, must return aString. - A method called
price, defined in terms of zero parameters, must return adouble.
- A method called
- Define an abstract class called
MenuItemthat implements theOrderableinterface.- 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 typedouble. (Its value represents the item's price per gram measured in cents.) - The class should implement the
pricemethod. The value returned should be the item's weight in grams multiplied by its price in cents per gram divided by 100. - The class should not implement the
descriptionmethod. (It may explicitly declare that the method is abstract.) - The class should override the standard
toStringmethod; theStringvalue returned should be the weight of the item in grams followed by a space, followed by a lowercaseg, followed by a space, followed by the value return by thedescriptionmethod. - The class should have
private finalinstance variables that hold the values of the object's weight in grams and its price per gram in cents.
- The class should have exactly one constructor method that is defined in terms of two parameters.
The first parameter should be of type
- Define an abstract class called
TaxedMenuItemthat extendsMenuItem.- 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 typedouble. (Its value represents the item's price per gram measured in cents.) The third parameter should be of typedouble. (Its value should be greater than 1 and represents the item's tax rate.) - The constructor method should call the constructor method of the
MenuItemclass. - The class should override the
pricemethod. The value returned should be the value returned by theMenuItemclass'spricemethod multiplied by the object's tax rate. - The class should have a
private finalinstance variable that holds the value of the object's tax rate.
- The class should have exactly one constructor method that is defined in terms of three parameters.
The first parameter should be of type
- Define classes
HotdogandPopcornthat extendMenuItemandCandyBarandSodaPopthat extendedTaxedMenuItem. 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!)
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