Skip to content

HW1: JMU Pizzeria v1

JMU Pizzeria Image source: vecteezy

Learning Objectives

  • Choose the appropriate data types, variables, literals, and arithmetic operators for specific use cases.
  • Implement utility classes, demonstrating their organization and functionality.
  • Identify how to declare and use static attributes and methods effectively.
  • Demonstrate proficiency in using tools and resources throughout the semester.
  • Apply course policies related to homework assignments.

Overview

JMU Pizzeria needs your help creating an ordering system. They have asked us to create a system that they can use to calculate the price of a particular order. All types of pizzas are priced based on the number of slices in an order. While pizzas are always delivered in boxes, customers can also order individual slices. However, the price for individual slices is different from the price of a full boxed pizza. The number of pizzas that fit in a full box is given by BOX_SIZE. The price of a full box of pizzas is given by PRICE_PER_BOX and the price of an individual pizza is given by PRICE_PER_SLICE.

At the pizzeria a full box holds 8 slices and costs $28.00, and each slice by itself costs $4.00. JMU students can sign up to receive a coupon code to reduce the cost. This term’s code is "WELCOME-BACK". If the discount code is correct and the total is over $25, an automatic discount of 10% is included.

Example:

Assuming a student orders 11 slices and has the correct coupon code. Total cost before the discount applied would be $40, given 1 full box and 3 extra slices that didn’t fit the box. Hence, with a 10% off with correct discount code would be $36.

Your task is to write a static utility method that, given the total number of slices ordered and the discount code (optional), calculates the total cost.

The Class to be Written

You must write the `PizzaPricer class.

UML Class Diagram

The following UML class diagram provides an overview of the attributes and methods in this class (which must be in the hws.hw1 package).

Class Diagram

Detailed Design Specifications

In addition to the specifications contained in the UML class diagram, this class must conform to the following specifications.

  1. All of the "constants" must be declared to be final.
  2. All of the methods that are passed a number of slices and return a number (whether an int or a double) must return 0 when the number of pizza is less than or equal to 0.
    • Note: There is a method that is passed a number of slices and returns a boolean. The specifications for that method are given below.
  3. numberOfFullBoxes() and numberOfBoxes() must return the number of full boxes and the total number of boxes required to hold the order. So, for the above example, an order of 11 slices requires 1 full box and 2 boxes in total with 3 slices in the second box.
  4. numberOfExtras() must return the number of slices in the order that will be in a box that isn't full. So, for example, an order of 11 slices will have 3 extras (since 8 of the slices will be in the full box).
  5. needAnExtraBox() must return true if there are extra slice(s) (i.e., slices that won't be in a full box) in the order and must return false otherwise. This method must not use an if statement, loop, or ternary operator.
  6. priceFor() must return the price of an order of the given size.
    • When a customer orders less than a full box, they pay the individual price for each slice. So, for example, an order of 4 slices will have a price of $16.0.
    • When a customer orders a full box, they pay the price of a full box. For example, an order of 8 slices will have a price of $28.0.
    • Finally, when a customer orders more than a full box, they pay the price of however many full boxes there are plus the cost of the extras. For example, an order of 11 slices will have a price of $40.0 (1 box at $28 plus 3 extras at $4.0 each).
  7. canApplyCoupon() must return true if the discount code is valid and order total is at least $25. The method must return false if either of the condition is invalid.
  8. finalPrice() must return the price of an order after the discount if applicable.
  9. Methods must not duplicate the code in other methods unless it is absolutely necessary (e.g., for parameter validation). Instead, methods must invoke each other as needed.

An Existing Class

A main class (i.e., a class with a main() method) that you can use to test the PizzaPricer class has already been written. It is named PizzaPricerTest and the source code (i.e., the .java file) is available here:

PizzaPricerTest.java

Submission

Submit your implementation of the PizzaPricer class to Gradescope. (Do not submit the PizzaPricerTest class.)

There is no limit on the number of submissions and no penalty for excessive submissions. Note that your submission will not be graded if it does not comply with the specifications. So, if you do not complete the PizzaPricer class your submission should include a stubbed-out version of all of the methods. This will allow you to potentially get credit for the methods that you do implement.

Grading

The grade you receive from Gradescope is the maximum grade that you can receive on the assignment. After the due date, the Professor may manually review your code. At this time, points may be deducted for inelegant code, inappropriate variable names, bad comments, etc.

Gradescope Grading

Your code must compile (in Gradescope, this will be indicated in the section on "Does your code compile?") and all class names and method signatures must comply with the specifications (in Gradescope, this will be indicated in the section on "Do your class names, method signatures, etc. comply with the specifications?") for you to receive any points on this assignment. Gradescope will then grade your submission as follows:

Criterion Points Details
Conformance to the Style Guide 20 points (Partial Credit Possible)
Correctness 80 points (Partial Credit Possible)

Gradescope will provide you with hints, but may not completely identify the defects in your submission.

Since nobody will be looking over your shoulder, you can use any process that you would like to use. However, it is strongly recommended that you use the process described here.

Get Started

  1. Read and understand the entire assignment.
  2. Create a folder for this assignment named hw1 (under src/hws).
  3. Download PizzaPricerTest.java to the src/hws/hw1 folder.

Understand the Test Cases

  1. Read and understand the test cases in PizzaPricerTest.java.
  2. By hand (i.e., using pencil and paper), calculate the expected answer for each of the test cases in PizzaPricerTest.java.

Stub-Out PizzaPricer.java

  1. Create a version of the PizzaPricer class that contains all of the methods (with appropriate signatures), each of which should return 0, 0.0, or false as appropriate.
  2. Add the Javadoc comments to the PizzaPricer class and the methods in it. Help on javadoc comments is available on the Department's Wiki.
  3. Make sure there are no compiler errors in PizzaPricerTest.java. If there are, you probably need to fix the stubbed-out version of PizzaPricer.java (since there should be no syntax errors in PizzaPricerTest.java).
  4. Check the style of the PizzaPricer class and make any necessary corrections.

Implement and Test the PizzaPricer Class

  1. Add the "constants".
  2. Add the numberOfFullBoxes() method.
  3. Run the PizzaPricerTest class and make sure that all of the answers returned by the numberOfFullBoxes() method are correct.
  4. Debug numberOfFullBoxes() if necessary.
  5. Add the numberOfExtras() method.
  6. Run the PizzaPricerTest class and make sure that all of the answers returned by the numberOfExtras() method are correct.
  7. Debug numberOfExtras() if necessary.
  8. Add the needAnExtraBox() method.
  9. Run the PizzaPricerTest class and make sure that all of the answers returned by the needAnExtraBox() method are correct.
  10. Debug needAnExtraBox() if necessary.
  11. Add the numberOfBoxes() method.
  12. Run the PizzaPricerTest class and make sure that all of the answers returned by the numberOfBoxes() method are correct.
  13. Debug numberOfBoxes() if necessary.
  14. Add the priceFor() method.
  15. Run the PizzaPricerTest class and make sure that all of the answers returned by the priceFor() method are correct.
  16. Debug priceFor() if necessary.
  17. Add the canApplyCoupon() method.
  18. Run the PizzaPricerTest class and make sure that all of the answers returned by the canApplyCoupon() method are correct.
  19. Debug canApplyCoupon() if necessary.
  20. Add the finalPrice() method.
  21. Run the PizzaPricerTest class and make sure that all of the answers returned by the finalPrice() method are correct.
  22. Debug finalPrice() if necessary.

Questions to Think About

You don't have to submit your answers to these questions, but you should try to answer them because they will help you determine whether or not you understand some of the important concepts covered in this assignment.

  1. What compile-time errors (if any) do you get in PizzaPricer.java class if you make the attributes non-static? Why?
  2. What compile-time errors (if any) do you get in PizzaPricer.java if you make all of the methods non-static? Why?
  3. What compile-time errors (if any) do you get in PizzaPricerTest.java if you make the methods in PizzaPricer.java non-static? Why?
  4. Why does the recommended process have you test and debug each method immediately after you implement it?
  5. Why does the recommended process have you implement the methods in the order it does? For example, why does it have you implement numberOfBoxes() after it has you implement numberOfExtraBoxes() and needAnExtraBox()?