HW1: Duke-n-Donuts v1¶
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¶
A group of former JMU students have decided to open a bakery called
Duke-n-Donuts. They have asked you to create a utility class named
DonutPricer
that they can use to calculate the price of a particular
order. All types of donuts are priced the same way and all donuts are
"delivered" in a box (that may be full or partially full). The number
of donuts that fit in a full box is given by BOX_SIZE
. The price of
a full box of donuts is given by PRICE_PER_BOX
and the price of an
individual donut is given by PRICE_PER_INDIVIDUAL
.
The Class to be Written¶
You must write the DonutPricer
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 donuts and return a number (whether an
int
or adouble
) must return 0 when the number of donuts is less than or equal to 0.- Note: There is a method that is passed a number of donuts and returns a
boolean
. The specifications for that method are given below.
- Note: There is a method that is passed a number of donuts 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 example, an order of 98 donuts will require 8 full boxes and 9 boxes in total.numberOfExtras()
must return the number of donuts in the order that will be in a box that isn't full. So, for example, an order of 98 donuts will have 2 extras (since 96 of the donuts will be in full boxes).needAnExtraBox()
must returntrue
if there are extra donuts (i.e., donuts 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 donut. So, for example, an order of 6 donuts will have a price of $5.94.
- When a customer orders a full box, they pay the price of a full box. For example, an order of 12 donuts will have a price of $9.99.
- 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 98 donuts will have a price of $81.90 (8 boxes at $9.99 plus 2 extras at $0.99 each).
- 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 DonutPricer
class has already been written. It is named
DonutPricerTest
and the source code (i.e., the .java
file) is available here:
Submission¶
Submit your implementation of the DonutPricer
class to Gradescope.
(Do not submit the DonutPricerTest
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 DonutPricer
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¶
Your code will first be graded by Gradescope and then by the Professor. The grade you receive from Gradescope is the maximum grade that you can receive on the assignment
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.
Manual Grading¶
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.
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
DonutPricerTest.java
to thesrc/hws/hw1
folder.
Understand the Test Cases¶
- Read and understand the test cases in
DonutPricerTest.java
. - By hand (i.e., using pencil and paper), calculate the expected answer for each of the test cases in
DonutPricerTest.java
.
Stub-Out DonutPricer.java
¶
- Create a version of the
DonutPricer
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
DonutPricer
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
DonutPricerTest.java
. If there are, you probably need to fix the stubbed-out version ofDonutPricer.java
(since there should be no syntax errors inDonutPricerTest.java
). - Check the style of the
DonutPricer
class and make any necessary corrections.
Implement and Test the DonutPricer
Class¶
- Add the "constants".
- Add the
numberOfFullBoxes()
method. - Run the
DonutPricerTest
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
DonutPricerTest
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
DonutPricerTest
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
DonutPricerTest
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
DonutPricerTest
class and make sure that all of the answers returned by thepriceFor()
method are correct. - Debug
priceFor()
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
DonutPricer.java
class if you make the attributes non-static? Why? - What compile-time errors (if any) do you get in
DonutPricer.java
if you make all of the methods non-static? Why? - What compile-time errors (if any) do you get in
DonutPricerTest.java
if you make the methods inDonutPricer.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()
?