Let's assume that your classes are working, and you have created an "A" object, and added 4 boxes. (5 x 5, 4 x 10, 2 x 4, and 6 x 12). Now choose the method for displaying all the box sizes. The following text might be displayed:

box: 5 x 5, cost $2.5
box: 4 x 10, cost $4.0
box: 2 x 4, cost $.8
box: 6 x 12, cost $7.2

Now choose the "A" method which sells a box. The method needs two pieces of information: height and width. If the height is 3 and the width is 6, then the following text should be displayed:

box: 4 x 10, cost $4.0

I need help with the second part, i can't seem to be able to come up with a loop that would work for that. A and B refers to two different classes, the postoffice class and a boxes class.

so far i just tried to getting the height

public int findBox(int height)
    {

        int index=-1;
        int counter = 0;
        for(Boxes box : inventory)
        {
if (box.getHeight() == height)
    {

        index = counter;


    }

    counter ++;

}

   return index;
}
}

but this only tells me the index of a box if its the exact number in the array list for height

You haven't given enough info to answer that. It depends upon how you have stored those objects and their data. The part about "choosing" the method is odd as well.

edit: Okay, you added another post while I was typing. Why not take both height and width and compare both values in your loop, then return the Box object that matches?

heres the "A" class

import java.util.ArrayList;

public class PostOffice
{

    public PostOffice()
    {
        // initialise instance variables
        inventory = new ArrayList <Boxes> ();
        inventory.add ( new Boxes (5,5));
        inventory.add ( new Boxes (4,10));
        inventory.add ( new Boxes (2,4));
        inventory.add ( new Boxes (6,12));

    }

    public void printBoxes ()
    {
        System.out.println("Boxes");
        for(Boxes boxes : inventory)
        {
            System.out.println(boxes);

        }
    }

    public int findBox(int height)
    {

        int index=-1;
        int counter = 0;
        for(Boxes box : inventory)
        {
    if (box.getHeight() == height)
    {

        index = counter;


    }

    counter ++;

}

   return index;
}
}

heres the B class

public class Boxes
{
    // instance variables - replace the example below with your own
 private  int width;
 private  int height;
   private double cost;



    public Boxes( int newHeight, int newWidth)
    {
      height = newHeight;
      width = newWidth;
      cost = .10*height*width;
    }

    public int getHeight()
    {
        return height;
    }
    public int getWidth()
    {
        return width;
    }


    public String toString ()
    {
        return "Box: " + height + " x " + width + "\t Cost: $: " + cost;
    }

In the future, try to put all of the relevant info in a single post rather than composing it across many. It's rather difficult to answer a question that is actually only an incomplete portion of a question.

Overview

A post office supplies boxes of many different sizes. The post office can add new box sizes at any time. Customers can ask to see the different box sizes, and can buy a box to ship their package in. The post office will sell that customer the cheapest box which fits the package.

For the purposes of this assignment, you may assume that a box has two dimensions - height and width. Feel free to extend the assignment to three dimensions AFTER you get it working for two dimensions. Furthermore, the cost of the box is 10 cents times (the height of the box) times (the width of the box). The height of a box is always less than or equal to the width of the box. Finally, the post office has an infinite number of all box sizes, so they never run out.

You can interact with your project using the object bench and calling methods on objects directly. If you want to get more elaborate, feel free to use the InputReader class from the previous homeworks, and create your own UI class.
Getting Started

There is a famous saying - "Hours of coding will save you minutes of planning." Right now, before you start anything else, think about what classes you are going to need. You must have at least two. What are they? What attributes does each one have? What methods does each one have? Write down on a piece of paper your answers to these questions. (If you are stuck at this point, go back and look at project 1. If that does not help, then ask Dr. Allen. If your design starts looking really complicated, then ask Dr. Allen.)

Now, think about your test cases for each class. Consider positive, negative, and boundary test cases. Write those down too.
Start Coding

For this project, you will start a project from scratch. Open BlueJ, and select Project, then New Project. Make sure the New Project window is open to the DeskTop, and type YourNameP2 at the end of the path labelled File Name.

At this point, you probably have in mind two classes "A" and "B" (not their real names). "A" has an attribute which is an ArrayList of "B". Create the "B" class first, by clicking on the New Class button, and filling in its real name. Then open the editor, and fill in its attributes, methods, and comments. Compile, test, and assure yourself that the code works. (JUnit testing is your friend here.

Now create your other class. Fill in its attributes, methods, and comments. Build the JUnit tests for this class. Fixtures will make this very simple. I suggest a fixture which adds at least 3 different "B"s to the ArrayList.
Sample Interaction

Let's assume that your classes are working, and you have created an "A" object, and added 4 boxes. (5 x 5, 4 x 10, 2 x 4, and 6 x 12). Now choose the method for displaying all the box sizes. The following text might be displayed:

box: 5 x 5, cost $2.5
box: 4 x 10, cost $4.0
box: 2 x 4, cost $.8
box: 6 x 12, cost $7.2

Now choose the "A" method which sells a box. The method needs two pieces of information: height and width. If the height is 3 and the width is 6, then the following text should be displayed:

box: 4 x 10, cost $4.0

Ok, so in regards to my (edited) post up above, why not alter what you have written to accept both parameters, height and width, and return the Box that is found (or Boxes in your case, but I would recommend renaming that to just Box because that more accurately reflects what it is - you don't have a single instance of a "Boxes"). The code that is searching for a particular box doesn't care what its index is in some arbitrary inventory list is - it needs a Box, so return that. Here is your method changed to return the Box(es) object. I'll leave it to you to add the parameter to also pass in a width and check that in your if() statement.

public Boxes findBox(int height)
{
  Boxes foundBox = null;
  for(Boxes box : inventory)
  {
    if (box.getHeight() == height)
    {
      foundBox = box;
      break; // you found it, no point in looking any further
    }
  }
  return foundBox;
}

Keep in mind, this method will return null if it doesn't find a matching box, so the code that calls this method needs to check that the return is not null before it attempts to do anything with that Box.

so how would the user inputs dimensions of height and width, the method be able to pick the next highest number for a box in the array list?

Ah, sorry, I only glanced over the assignment and missed that requirement. You could change your if() criteria to check that the box is big enough to hold the item. If it's big enough, check against the one you have already found to see if the current box is smaller (or if foundBox is null). If it is, set 'foundBox' to the current box. When you're done, 'foundBox' should contain the smallest box that will accommodate the dimensions you specified.

im not sure how that if statement would look like, i have been on this same problem for too long! can you please help?

Ah, sorry, I only glanced over the assignment and missed that requirement. You could change your if() criteria to check that the box is big enough to hold the item. If it's big enough, check against the one you have already found to see if the current box is smaller (or if foundBox is null). If it is, set 'foundBox' to the current box. When you're done, 'foundBox' should contain the smallest box that will accommodate the dimensions you specified.

@ OP: If your post prior to this one is referring to what Ezzaral said, the if statement pseudocode would probably look like this...

if(Box Height >= Item Height && Box Width >= Item Width){
//Do what ezzaral suggested in here.
}

Of course, you will have to replace the statements I made with equivalent code statements. And out of curiosity, why are you using a 2D box instead of a 3D box?

how would you make it able to return that box. because with the example from before it doesnt even return anything.

with the if statement by bestjew it picks the 6 X 12 box instead of the 4 X 10, if i enter 3 X 6 for dimensions.

And are you comparing that box against the smallest foundBox so far? You still need to compare the Box against the current "foundBox" to keep the smaller of the two. so you also need

// pseudocode: inside the if() statement mentioned above
if (foundBox==null || thisBox is smaller than foundBox){
  foundBox = thisBox
}

is that the only thing under the if statement, because 3 X 6 is now working. but if i put a number like 1 X1 it gives me 5 X 5 instead of 2 X 4.

Well, it's hard to say because you haven't posted any updated code in awhile. You could get more specific answers about why your method is behaving in a certain manner if you would post it. We can't see over your shoulder from where we are sitting :P

As you loop through the boxes, the logic of your if() checks should be:
1) Is this box big enough for each parameter?
2) If so, is it smaller than the last box I found that was big enough or is this the first box I've found that's large enough (this is the null case)?
3) If so, then save the reference to this box.

So after this has checked all the boxes, you should be left with the smallest box that fits the height and width parameters or a null variable if none of them are big enough.

is this right? I'm still having the same problem.

   public Boxes findBox(int height, int width)
    {

         Boxes foundBox = null;
        for(Boxes box : inventory)
        {
    if (box.getHeight() >= height && box.getWidth() >= width)
    {


      if( foundBox==null)
      foundBox = box;


    }
}

return foundBox;    

}

You still need to add the criteria to check if box is smaller than foundBox.

so would i be adding this in the second if statement in the parenthesis?

Yes. You want to see if either dimension is smaller than the box you already found.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.