Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Carton Packing (OOP)

Updated 21 Oct 2026

A company packs articles in cartons of 48, 24, 12 and 6 articles. This program finds the number of cartons of each size, the total number of cartons and the articles left over. In this OOP version the object is created with the number of articles (a parameterised constructor), and one-job methods check the number, use the cartons of one size, pack, count the cartons and give the remaining articles.

Definition

Greedy packing means taking the largest carton first: as many cartons of 48 as possible, then 24, then 12, then 6 from what is left. The articles still left after the cartons of 6 (fewer than 6) are the remaining articles and are not packed.

For $x$ articles and carton size $s$, the cartons used are x / s and the articles left are x % s (integer division).

For example, for 143 articles: $143 = 2 \times 48 + 47$, $47 = 1 \times 24 + 23$, $23 = 1 \times 12 + 11$, $11 = 1 \times 6 + 5$. That is 2, 1, 1 and 1 cartons (5 in all) with 5 articles remaining.

Algorithm

  1. main reads the number of articles x and creates ob = new Carton(x). The constructor stores x in n and in left, puts 48, 24, 12, 6 in the array size and creates the array count.
  2. main calls ob.isValid(), which returns true when n is from 1 to 1000000. If it is false, main prints INVALID INPUT.
  3. Otherwise main calls ob.pack(): for i from 0 to 3 it sets count[i] = useCartons(size[i]).
  4. useCartons(s) finds c = left / s, sets left = left % s and returns c.
  5. main prints the cartons of each size, then ob.totalCartons() (the sum of the four counts) and ob.remaining() (the value of left).

Program

Each method does one job: isValid(), useCartons(), totalCartons() and remaining() return values, and pack() calls useCartons() for each size.

import java.util.Scanner;

/**
 * Class Carton (OOP version)
 * A company packs articles in cartons of 48, 24, 12 and 6 articles.
 * The object holds the articles to be packed; methods pack them greedily
 * (largest carton first) and return the totals.
 */
public class Carton
{
    int n;                  // number of articles given
    int left;               // articles not yet packed (fewer than 6 at the end)
    int size[];             // carton sizes, largest first: 48, 24, 12, 6
    int count[];            // count[i] = number of cartons of size[i] used

    /**
     * Parameterised constructor: stores the articles and sets the carton sizes.
     * @param articles the number of articles to be packed
     */
    Carton(int articles)
    {
        n = articles;
        left = articles;
        size = new int[] {48, 24, 12, 6};
        count = new int[4];
    }

    /**
     * Checks the number of articles.
     * @return true if n is from 1 to 1000000
     */
    boolean isValid()
    {
        return n >= 1 && n <= 1000000;
    }

    /**
     * Uses as many cartons of one size as possible from the articles left.
     * @param s the carton size
     * @return the number of cartons of size s used; left is reduced to the rest
     */
    int useCartons(int s)
    {
        int c = left / s;                     // whole cartons of this size
        left = left % s;                      // articles still to be packed
        return c;
    }

    /**
     * Packs the articles greedily, from the largest carton to the smallest.
     */
    void pack()
    {
        for (int i = 0; i < 4; i++)
        {
            count[i] = useCartons(size[i]);
        }
    }

    /**
     * Finds the total number of cartons used.
     * @return the sum of the four counts
     */
    int totalCartons()
    {
        int total = 0;
        for (int i = 0; i < 4; i++)
        {
            total = total + count[i];
        }
        return total;
    }

    /**
     * Gives the articles that could not be packed.
     * @return the remaining articles (after packing)
     */
    int remaining()
    {
        return left;
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of articles: ");
        int x = sc.nextInt();
        Carton ob = new Carton(x);
        if (ob.isValid())
        {
            ob.pack();
            for (int i = 0; i < 4; i++)
            {
                System.out.println("Cartons of " + ob.size[i] + ": " + ob.count[i]);
            }
            System.out.println("Total cartons: " + ob.totalCartons());
            System.out.println("REMAINING articles: " + ob.remaining());
        }
        else
        {
            System.out.println("INVALID INPUT");
        }
    }
}

Variable description table

VariableData TypePurpose
nintData member: number of articles given
leftintData member: articles not yet packed; at the end, the remaining articles
sizeint[]Data member: carton sizes 48, 24, 12, 6
countint[]Data member: cartons used of each size, count[i] for size[i]
articlesintParameter of the constructor: number of articles
sintParameter of useCartons(): the carton size
cintCartons of size s used, returned by useCartons()
iintLoop counter over the four carton sizes
totalintRunning total of cartons in totalCartons()
scScannerReads the number of articles typed by the user
xintNumber of articles read in main
obCartonObject of the class created in main
argsString[]Command-line arguments of main (not used)

Sample input and output

Sample 1

Enter the number of articles: 143
Cartons of 48: 2
Cartons of 24: 1
Cartons of 12: 1
Cartons of 6: 1
Total cartons: 5
REMAINING articles: 5

Sample 2

Enter the number of articles: 48
Cartons of 48: 1
Cartons of 24: 0
Cartons of 12: 0
Cartons of 6: 0
Total cartons: 1
REMAINING articles: 0

Sample 3

Enter the number of articles: 59
Cartons of 48: 1
Cartons of 24: 0
Cartons of 12: 0
Cartons of 6: 1
Total cartons: 2
REMAINING articles: 5

Sample 4

Enter the number of articles: 5
Cartons of 48: 0
Cartons of 24: 0
Cartons of 12: 0
Cartons of 6: 0
Total cartons: 0
REMAINING articles: 5

Sample 5

Enter the number of articles: 30
Cartons of 48: 0
Cartons of 24: 1
Cartons of 12: 0
Cartons of 6: 1
Total cartons: 2
REMAINING articles: 0

Sample 6

Enter the number of articles: 1000000
Cartons of 48: 20833
Cartons of 24: 0
Cartons of 12: 1
Cartons of 6: 0
Total cartons: 20834
REMAINING articles: 4

Sample 7

Enter the number of articles: 0
INVALID INPUT

Sample 8

Enter the number of articles: 1000001
INVALID INPUT

One value per line

Dry run

Take the input $x = 143$. The constructor sets n = 143 and left = 143; isValid() is true. pack() calls useCartons(size[i]) for each i:

is = size[i]left beforec = left / sleft = left % scount[i]
0481432472
124471231
212231111
3611151

totalCartons() returns $2 + 1 + 1 + 1 = 5$ and remaining() returns 5. main prints 2, 1, 1 and 1 cartons, Total cartons: 5 and REMAINING articles: 5, as in the first sample run.

Variations

  1. Currency notes "Read an amount and find the smallest number of notes of each denomination needed to make it."
  2. Sizes read from the user "Read the carton sizes in descending order and pack the articles in the same way."