A small company wants you to write a program to find the number of boxes needed to ship book orders without wasting space. There are four types of boxes: extra-large, large, medium and small, which holds 25, 15, 5 and 1 book(s), respectively.

Write a Java program that accepts the number of books to be shipped, and displays the number of individual boxes along with their type, as well as the total number of boxes needed. For example if the company wants to ship 81 books, your output should be 3 extra-large boxes, 1 medium box and 1 small box; a total of 5 boxes.

Use the following class diagram as the basis for your solution.

Hint: Define an algorithm for the method determineBoxes before writing the code. This is the only method necessary for determining the number of boxes needed.

Use the following test class that creates an object of 127 books.

The test class
1. class TestPackingBooks 2. {
3. public static void main(String [] arg) 4. {
5. PackingBooks pack = new PackingBooks (127);
6. pack.determineBoxes(); 7.
8. System.out.println(pack.getXLargeBox() + " extra-large boxes\n"
9. + pack.getLargeBox() + " large boxes\n"
10. + pack.getMediumBox() + " medium boxes\n"
11. + pack.getSmallBox() + " small boxes\n"
12. + pack.getTotalBoxes() + " total boxes\n") ;
13. }
14. }

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public class PackingBooks {

    private static final int XTRA_LARGE_BOX = 25;
    private static final int LARGE_BOX = 15;
    private static final int MEDIUM_BOX = 5;
    private int books;
    private int xlarge;
    private int large;
    private int medium;
    private int small;
    private int total;

    * constructor
    * @param books
    public PackingBooks(int books) {
       this.books = books;

    * determining the number of boxes needed
    public void determineBoxes() {
       this.xlarge = 0;
       this.large = 0;
       this.medium = 0;
       this.small = 0; = 0;
       // reseve value of books
       int bks = books;
       // try the big box first
       if (bks > 0) {
            // get nubmer of box
            xlarge = bks / XTRA_LARGE_BOX;
            // get remainder
            bks = bks % XTRA_LARGE_BOX;
       // take the same action
       if (bks > 0) {
            large = bks / LARGE_BOX;
            bks = bks % LARGE_BOX;

By purchasing this solution you'll be able to access the following files: and

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats