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
mainreads the number of articlesxand createsob = new Carton(x). The constructor storesxinnand inleft, puts 48, 24, 12, 6 in the arraysizeand creates the arraycount.maincallsob.isValid(), which returns true whennis from 1 to 1000000. If it is false,mainprintsINVALID INPUT.- Otherwise
maincallsob.pack(): forifrom 0 to 3 it setscount[i] = useCartons(size[i]). useCartons(s)findsc = left / s, setsleft = left % sand returnsc.mainprints the cartons of each size, thenob.totalCartons()(the sum of the four counts) andob.remaining()(the value ofleft).
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
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member: number of articles given |
left | int | Data member: articles not yet packed; at the end, the remaining articles |
size | int[] | Data member: carton sizes 48, 24, 12, 6 |
count | int[] | Data member: cartons used of each size, count[i] for size[i] |
articles | int | Parameter of the constructor: number of articles |
s | int | Parameter of useCartons(): the carton size |
c | int | Cartons of size s used, returned by useCartons() |
i | int | Loop counter over the four carton sizes |
total | int | Running total of cartons in totalCartons() |
sc | Scanner | Reads the number of articles typed by the user |
x | int | Number of articles read in main |
ob | Carton | Object of the class created in main |
args | String[] | 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: 5Sample 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: 0Sample 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: 5Sample 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: 5Sample 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: 0Sample 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: 4Sample 7
Enter the number of articles: 0
INVALID INPUTSample 8
Enter the number of articles: 1000001
INVALID INPUTDry 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:
| i | s = size[i] | left before | c = left / s | left = left % s | count[i] |
|---|---|---|---|---|---|
| 0 | 48 | 143 | 2 | 47 | 2 |
| 1 | 24 | 47 | 1 | 23 | 1 |
| 2 | 12 | 23 | 1 | 11 | 1 |
| 3 | 6 | 11 | 1 | 5 | 1 |
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
- Currency notes "Read an amount and find the smallest number of notes of each denomination needed to make it."
- Sizes read from the user "Read the carton sizes in descending order and pack the articles in the same way."