2

The program I am working on is a simple shipping program. What I am having difficulty with is populating a multidimensional array factoring in certain variables.

Example

320 items need to be shipped out to 1 receiver using different box sizes.

  • XL can hold 50 items
  • LG can hold 20 items
  • MD can hold 5 items
  • SM can hold 1 items

Use the least number of boxes so far.

Code

This is my code so far.

import java.util.Scanner;

public class Shipping {
    public static void main(String [] args) {
        Scanner kbd = new Scanner(System.in);

        final int EXTRA_LARGE = 50;
        final int LARGE = 20;
        final int MEDIUM = 5;
        final int SMALL = 1;

        String sBusinessName = "";
        int iNumberOfGPS = 0;

        int iShipmentCount = 0;

        displayHeading(kbd);
        iShipmentCount = enterShipments(kbd);
        int[][] ai_NumberOfShipments = new int [iShipmentCount][4];
        String[] as_BusinessNames = new String [iShipmentCount];

        for (int iStepper = 0; iStepper < iShipmentCount; iStepper++) {
            sBusinessName = varifyBusinessName(kbd);
            as_BusinessNames[iStepper] = sBusinessName;
            iNumberOfGPS = varifyGPS(kbd);
            calculateBoxes(ai_NumberOfShipments[iStepper],iNumberOfGPS, EXTRA_LARGE, LARGE, MEDIUM, SMALL);
        }
        //showArray(as_BusinessNames);
    }

    public static void displayHeading(Scanner kbd) {
        System.out.println("Red River Electronics");
        System.out.println("Shipping System");
        System.out.println("---------------");

        return;
    }

    public static int enterShipments(Scanner kbd) {
        int iShipmentCount = 0;
        boolean bError = false;
        do {
            bError = false;
            System.out.print("How many shipments to enter? ");
            iShipmentCount = Integer.parseInt(kbd.nextLine());

            if (iShipmentCount < 1) {
                System.out.println("\n**Error** - Invalid number of shipments\n");
                bError = true;
            }
        } while (bError == true);

        return iShipmentCount;
    }

    public static String varifyBusinessName(Scanner kbd) {
        String sBusinessName = "", sValidName = "";
        do {
            System.out.print("Business Name: ");
            sBusinessName = kbd.nextLine();

            if (sBusinessName.length() == 0) {
                System.out.println("");
                System.out.println("**Error** - Name is required\n");
            } else if (sBusinessName.length() >= 1) {
                sValidName = sBusinessName;
            }
        } while (sValidName == "");

        return sValidName;
    }

    public static int varifyGPS(Scanner kbd) {
        int iCheckGPS = 0;
        int iValidGPS = 0;
        do {
            System.out.print("Enter the number of GPS receivers to ship: ");
            iCheckGPS = Integer.parseInt(kbd.nextLine());

            if (iCheckGPS < 1) {
                System.out.println("\n**Error** - Invalid number of shipments\n");
            } else if (iCheckGPS >= 1) {
                iValidGPS = iCheckGPS;
            }
        } while(iCheckGPS < 1);

        return iValidGPS;   
    }
    
    public static void calculateBoxes(int[] ai_ToFill, int iNumberOfGPS) {
        for (int iStepper = 0; iStepper < ai_ToFill.length; iStepper++)
    }

    //public static void showArray( String[] ai_ToShow) {
    //    for (int iStepper = 0; iStepper < ai_ToShow.length; iStepper++) {
    //        System.out.println("Integer at position " + iStepper + " is " + ai_ToShow[iStepper]);
    //    }
    //}
}
3
  • 1
    What is the problem exactly? It looks like you haven't figured out how to write calculateBoxes, but you seem to have started OK. However, I can't tell what this method needs to accomplish. It might help to have an array of four constants, instead of four separate constants EXTRA_LARGE, LARGE, etc. Commented Nov 11, 2014 at 23:53
  • Looks like this is a 0-1 Knapsack problem. Equation: 320 = 50a+20b+5c+1d Is there a box limit; weight? Commented Nov 11, 2014 at 23:54
  • The code is not yet complete as I am not sure how to write it out. What I want to do is take 320 items and place them into 6 XL boxes(320 - 300 = 20) and 1 LG box(20 - 20 = 0). I need to add this information in an array. What I was hoping to do was use the method calculateBoxes to do that. I am just not sure on how to structure the logic for it Commented Nov 12, 2014 at 0:33

1 Answer 1

1

Change your definition of calculateBoxes() to also take an array that represents the volume of each of the boxes (in your case this will be {50, 20, 5, 1}:

public static void calculateBoxes(int[] ai_ToFill, int[] boxVolumes, int iNumberOfGPS) {
    // for each box size
    for (int iStepper = 0; iStepper < ai_ToFill.length; iStepper++) {
        // while the remaining items to pack is greater than the current box size
        while(iNumberOfGPS >= boxVolumes[iStepper]) {
            // increment the current box type
            ai_ToFill[iStepper]++;
            // subtract the items that just got packed
            iNumberOfGPS -= boxVolumes[iStepper];
        }
    }
}

Another way of calculating this (using / and % instead of a while loop) would be:

public static void calculateBoxes(int[] ai_ToFill, int[] boxVolumes, int iNumberOfGPS) {
    // for each box size
    for (int iStepper = 0; iStepper < ai_ToFill.length; iStepper++) {
        if(iNumberOfGPS >= boxVolumes[iStepper]) {
            // calculate the number of boxes that could be filled by the items
            ai_ToFill[iStepper] = iNumberOfGPS/boxVolumes[iStepper];
            // reset the count of items to the remainder
            iNumberOfGPS = iNumberOfGPS%boxVolumes[iStepper];
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for this! I think the problem was that I was planning on importing each Box Size integer. Honestly, I was going to and use several IF, ELSE IF statements. I knew there had to be a better way.
@Colin If you want an even better way, use the / and % operators. You shouldn't have to write a loop that keeps subtracting 50 from a number, when you can use integer division to figure it out.
@ajb correct, however this might be simpler to understand in the first instance. I'll add that as an alternative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.