Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2017 QUESTION 1 CARTOON PACKING

Tuesday, March 12, 2019

ISC COMPUTER SCIENCE PRACTICAL 2017 QUESTION 1 CARTOON PACKING

Question:

A company manufactures packing cartons in four sizes, i.e. cartons to accommodate 6 boxes, 12 boxes, 24 boxes and 48 boxes. Design a program to accept the number of boxes to be packed (N) by the user (maximum up to 1000 boxes) and display the break-up of the cartons used in descending order of capacity (i.e. preference should be given to the highest capacity available, and if boxes left are less than 6, an extra carton of capacity 6 should be used.)
Test your program with the sample data and some random data:
Example 1
INPUT : N = 726
OUTPUT :
48 x 15 = 720
6 x 1 = 6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16
Example 2
INPUT : N = 140
OUTPUT :
48 X 2 = 96
24 x 1 = 24
12 x 1 = 12
6 x 1 = 6
Remaining boxes 2 x 1 = 2
Total number of boxes = 140
Total number of cartons = 6
Example 3
INPUT : N = 4296
OUTPUT : INVALID LENGTH





import java.util.*;
public class ISC2017Q1 {
       public static void main(String[] args) {
             int N;
             Scanner in = new Scanner(System.in);
             System.out.println("Enter the number of boxes:");
             N = in.nextInt();
             while( N>1000) {
                    System.out.println("Invalid length, try again:");
                    N = in.nextInt();
             }
             Cartoons obj = new Cartoons(N);
             obj.CartoonsCalculation();
             System.out.println("OUTPUT:");
             obj.Display();
             obj.TotalNoOfCartoons();
       }
       }
       class Cartoons{
            
             Cartoons(int N){
                    number_of_boxes = N;
                    total_cartoons = 0;
             }
            
             public void CartoonsCalculation() {
                    int box_counter=number_of_boxes;
                    for(int i=0;i<4;i++) {
                         
                          if(box_counter % capacity_of_cartoons[i]==0) {
                                 total_no_of_cartoons[i] = (box_counter/capacity_of_cartoons[i]);
                                 box_counter = box_counter % capacity_of_cartoons[i];
                                 break;
                          }
                          else {
                                 total_no_of_cartoons[i] = (box_counter/capacity_of_cartoons[i]);
                                 box_counter = box_counter % capacity_of_cartoons[i];
                          }
                    }
                    remaining_boxes = box_counter;
             }
            
             public void Display() {
                    for(int i=0;i<4;i++)
                          System.out.println("                 "+capacity_of_cartoons[i]+"X"+total_no_of_cartoons[i]);
                    if(remaining_boxes !=0) {
                          System.out.println("Remaining Boxes:"+remaining_boxes+"X"+1);
                          total_no_of_cartoons[3] +=1;  // one cartoon of capacity 6 to be used for remaining boxes
                    }
                    else
                          System.out.println("Remaining Boxes:"+remaining_boxes);
                    System.out.println("Total number of Boxes:"+number_of_boxes);
             }
            
             public void TotalNoOfCartoons() {
                    for(int j=0;j<4;j++) {
                          total_cartoons +=total_no_of_cartoons[j];
                    }
                    System.out.println("Total number of Cartoons:"+total_cartoons);
             }
            
             private int[] capacity_of_cartoons = {48,24,12,6};
             private int number_of_boxes;
             private int[] total_no_of_cartoons = {0,0,0,0};
             private int remaining_boxes;
             private int total_cartoons;
       }     

No comments:

Post a Comment