PA2: Brendon's Bakery Warehouse (Methods)

Due Dates and Submission Details

See submission details below for all due dates and submission details. Note there are multiple submission parts.

Honor Code

This assignment should be completed individually to maximize learning. It is important that you adhere to the Course Policies, particularly the section on Programming Assignments. Also relevant is the JMU Honor Code.


  • Organize input/output code into multiple methods.
  • Write proper documentation comments for methods.
  • Correctly use the modulus operator and order of operations.
  • Format numerical data to fixed precision for display.
  • Use constants for readability and maintainability.
  • Use the command line to test program correctness.


Your program has been a tremendous success, and the bakery is selling more goods than ever before! To keep up with demand, the owner has opened up a warehouse to handle all orders with quantities 100 or over and needs you to implement more features for the program to be used at the warehouse:

  • Your program must read in and calculate the total for three products.

  • Products are counted in terms of dozens, with a leftover amount.

  • A standard discount of 20% is given to each dozen products, but not to additional products.

  • Because discounts are for dozens, customer's have requested an average product cost.

  • All decimal values must be formatted with two digits of precision.

Getting Started

Since the program is starting to get long, you will need to improve your PA1 submission by organizing it into multiple well-documented methods. Make a copy of your PA1 source file and name it Don't forget to rename the class to Bakery2 for it to compile, and while you're at it update the @version tag. Then add the following methods to your program (above main). An important part of this assignment will be to write method javadoc comments with @param and @return tags where applicable.

The constants for the tax rate and discount rate will be shared across the whole class. To declare these where the whole class can make use of them, copy the following lines directly beneath the class header (i.e., the first element inside the class):

public static final double TAX_RATE = 5.3;

public static final int DISCOUNT = 20;

The public keyword means the variable is visible outside this class. The static keyword means the variable will be shared by all instances of the class. The final keyword means that the variable is only assigned once and is a constant. (We'll learn more about these concepts later in the semester.)


Each of the methods below should be declared above main and implemented exactly as described. Only one Scanner should be declared and used in the entire class. It should be declared in main and then passed as an argument to each method as appropriate.

    • public static double inputNumber(Scanner in, String prompt)

      This method will prompt the user using the prompt passed to it, read in the user's response as a double from the Scanner in, consume the rest of the line (i.e., the \n character), and then return the response as a double.

      Example: inputNumber(in, "Enter Customer's Product 1 Price: ") where the user enters 2 would return 2.0

    • public static String inputLine(Scanner in, String prompt)

      This method will prompt the user using the prompt passed to it, read in the user's response as a String from the Scanner in and then return the response as a String.

      Example: inputLine(in, "Enter Customer's Personal Name: ") where the user enters Ali would return "Ali"

    • public static void printHeading(String personalName, String familyName)

This method will must print a blank line, the logo for the bakery (2 lines), the first and last name of the customer on one line with a single space in between, and the date this assignment is due, followed by another blank line. This should look similar to the heading in PA1, but have the due date of this assignment.

  • public static double printProduct(String code, int quantity, double price)

    This method will print the individual product information for one line in the table and calculate the average price for each product after the discount is applied, so you will call it three times. The columns will each be separated with a single tab character and the price will be preceded by a $. Each column should be calculated and formatted as follows:

      • Product

    The product is the code entered by the user and should be printed out as a right justified String with a minimum of 7 spaces.

      • Dozen

    This is the number of DISCOUNT_NUM products in the quantity entered by the user for this product. It should be printed as a right justified integer with a minimum of 5 spaces.

      • Extra

    This is the remainder of the number of this product leftover after dividing the quantity entered by the user by the DISCOUNT_NUM. It should be also be printed as a right justified integer with a minimum of 5 spaces.

      • Price/1

    The price per one product is the average price of the product after the discount has been applied to the quantity that is an even multiple of DISCOUNT_NUM. (Think mod) It should be formatted as an unjustified floating point number with with only two decimal places and an unspecified number of total spaces.


    Example: printProduct("cake22", 100, 1.00) would printout cake22 8 4 $0.81 spaced as described above and would return the value 0.808. See the expected output examples shown below for precise details.

You will still have a main method as before, but it will primarily call the other methods to do most (but not all) of the work. In particular, main should declare a single Scanner that will be passed to each method. As before, many variables (but not all) will be declared in main. Make sure you do not duplicate any effort between your main method and the supporting methods.

As in the previous assignment, there is no need to handle invalid user input (e.g., the user typing in a string when you're expecting a double).

Examples and Testing

The files below contain example input and corresponding expected output of a finished program. You must use these files to perform diff/meld testing similar to the Command Line Testing Lab lab. It is highly unlikely that you will be able to get the format exactly right without using this testing technique. This is the purpose of the detailed description and formatting requirements - to get you to use command-line testing.


This assignment has two parts that should be completed in order. By the first deadline you must complete a readiness quiz in Canvas, by the second deadline you must submit your completed code through AutoLab.

Part A - Friday September 14, 11:00PM

Read this entire document and complete the quiz in Canvas. The grading for this quiz is all-or-nothing. You must answer all questions correctly by the deadline to get any credit. You will have as many attempts as you need, but if you do not answer all questions correctly by the deadline or you will receive no credit for this part.

Part B - Friday September 21st, 11:00PM

Upload your completed file through AutoLab.

  • -15% before Saturday, 922 11:00 pm
  • -30% before Sunday, 923 11:00 pm
  • Not accepted after Sunday, 11:00 pm

You must implement the above in java, creating a file called Before uploading your file, be sure to complete the following steps carefully:

    • Verify that your code is working. You should test it with several of your own tests, but included above are three input files with the corresponding output file to give you verified command-line tests. Note if your output is not exactly like the output in the `pa2testx.exp` when run on ``, then your code is not correct.
    • Make sure all your variables are declared at the beginning of the `main()` method, before any other statements.
    • Check that you have inserted the proper javadoc `@author` and `@version` comments in the appropriate header.
    • Run Checkstyle and eliminate ALL warnings about your code.


Your submission will be graded using the following criteria:

Requirement Points
PART A: Readiness Quiz 10
PART B: Checkstyle 10
PART B: Javadoc and Comments 10
PART B: Accurate algebraic calculations 20
PART B: Accurate modulus calculations 10
PART B: I/O and Formatting 10
PART B: Proper method calls 10
PART B: Passes Diff Testing 10
PART B: Instructor grading based on style and code quality 10


Don't put off submission until the last possible minute! Any submission system may become bogged down when it receives a large number of submissions. You may have some unanticipated difficulties uploading your code. It is your responsibility to take these possibilities into account and submit early enough to ensure that the submission process is completed before the deadline.


Format the floating point values in the final report of totals as right justified numbers in an unspecified number of spaces with 2 places after the decimal point. Also, compute the discount by subtracting the discounted price from the full price. Compute the discounted price by multiplying each average price returned by printProduct by the quantity entered by the user.

For a nice summary of printf format specifiers, look here.

Going Further

Why did we use the colors above (input/output)?


This assignment was originally developed by Nathan Sprague and repurposed by Dee Weikle.

Back to Top