HW1: JMU Pizzeria v1¶
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).
Detailed Design Specifications¶
In addition to the specifications contained in the UML class diagram, this class must conform to the following specifications.
- All of the "constants" must be declared to be
final
. - All of the methods that are passed a number of slices and return a number (whether an
int
or adouble
) 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.
- Note: There is a method that is passed a number of slices and returns a
numberOfFullBoxes()
andnumberOfBoxes()
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.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).needAnExtraBox()
must returntrue
if there are extra slice(s) (i.e., slices that won't be in a full box) in the order and must returnfalse
otherwise. This method must not use anif
statement, loop, or ternary operator.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).
canApplyCoupon()
must returntrue
if the discount code is valid and order total is at least $25. The method must returnfalse
if either of the condition is invalid.finalPrice()
must return the price of an order after the discount if applicable.- 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:
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.
Recommended Process¶
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¶
- Read and understand the entire assignment.
- Create a folder for this assignment named
hw1
(undersrc/hws
). - Download
PizzaPricerTest.java
to thesrc/hws/hw1
folder.
Understand the Test Cases¶
- Read and understand the test cases in
PizzaPricerTest.java
. - 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
¶
- Create a version of the
PizzaPricer
class that contains all of the methods (with appropriate signatures), each of which should return0
,0.0
, orfalse
as appropriate. - Add the Javadoc comments to the
PizzaPricer
class and the methods in it. Help on javadoc comments is available on the Department's Wiki. - Make sure there are no compiler errors in
PizzaPricerTest.java
. If there are, you probably need to fix the stubbed-out version ofPizzaPricer.java
(since there should be no syntax errors inPizzaPricerTest.java
). - Check the style of the
PizzaPricer
class and make any necessary corrections.
Implement and Test the PizzaPricer
Class¶
- Add the "constants".
- Add the
numberOfFullBoxes()
method. - Run the
PizzaPricerTest
class and make sure that all of the answers returned by thenumberOfFullBoxes()
method are correct. - Debug
numberOfFullBoxes()
if necessary. - Add the
numberOfExtras()
method. - Run the
PizzaPricerTest
class and make sure that all of the answers returned by thenumberOfExtras()
method are correct. - Debug
numberOfExtras()
if necessary. - Add the
needAnExtraBox()
method. - Run the
PizzaPricerTest
class and make sure that all of the answers returned by theneedAnExtraBox()
method are correct. - Debug
needAnExtraBox()
if necessary. - Add the
numberOfBoxes()
method. - Run the
PizzaPricerTest
class and make sure that all of the answers returned by thenumberOfBoxes()
method are correct. - Debug
numberOfBoxes()
if necessary. - Add the
priceFor()
method. - Run the
PizzaPricerTest
class and make sure that all of the answers returned by thepriceFor()
method are correct. - Debug
priceFor()
if necessary. - Add the
canApplyCoupon()
method. - Run the
PizzaPricerTest
class and make sure that all of the answers returned by thecanApplyCoupon()
method are correct. - Debug
canApplyCoupon()
if necessary. - Add the
finalPrice()
method. - Run the
PizzaPricerTest
class and make sure that all of the answers returned by thefinalPrice()
method are correct. - 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.
- What compile-time errors (if any) do you get in
PizzaPricer.java
class if you make the attributes non-static? Why? - What compile-time errors (if any) do you get in
PizzaPricer.java
if you make all of the methods non-static? Why? - What compile-time errors (if any) do you get in
PizzaPricerTest.java
if you make the methods inPizzaPricer.java
non-static? Why? - Why does the recommended process have you test and debug each method immediately after you implement it?
- 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 implementnumberOfExtraBoxes()
andneedAnExtraBox()
?