0

I want the program to check how many large boxes are needed, then use the remainder to determine how many medium boxes are needed and do the same for small boxes. But when i run this

in the first step to determine the large boxes

variable pensnotinbox comes out as 0 even though i know there should be a remainder

   pen.setName("Awesome Pen");
     pen.setPrice(5.0);
     pen.setUPC(102);
     pen.setMediumCapacity(50);
     pen.setLargeCapacity(100);

     //printing information about the product to the user, and asking how many of this item they want to purchase
     System.out.print("Product 2 \n"   + "Name:" + pen.getName() + "    Price: " + pen.getPrice() + "    UPC: " + pen.getUPC() + "\n");
     System.out.print("How Many Of These Would You Like To Purchase?\n" );
     //using the scanner to get the integer/quantity they want
     penquant = scan.nextInt();
      //storing the total price in a variable 

        int penlargeboxes;
        double penboxes;
        int penmediumboxes;
        double penremainder;
        double pensnotinbox;
         int pensmallboxes;

      if (pen.getLargeCapacity() <= penquant) {
          penlargeboxes = penquant/pen.getLargeCapacity(); 
          penremainder = penquant % pen.getLargeCapacity(); 


          System.out.print(penlargeboxes + "\n");
          System.out.print(penremainder + "\n");


           if (penremainder > 0 ) {

              penboxes = penremainder/pen.getMediumCapacity() ;
              penmediumboxes = ((int)penboxes);
              penremainder =  penquant % pen.getLargeCapacity(); 

              pensnotinbox = penremainder;

              System.out.print(penmediumboxes + "\n");
              System.out.print(pensnotinbox + "\n");

            }else {
                if (penremainder > .99 ) {

               penboxes = penremainder/1 ;
               pensmallboxes = ((int)penboxes);

                  System.out.print(pensmallboxes + "\n");


            }

            }

     } else {
          System.err.println("OOPS!");
     } 


     pentotal= (pen.totalPurchase(penquant));
     //printing their total cost for this item
     System.out.print("The total cost for this item will be " + pentotal + "\n" + "\n");
6
  • What is the value of penquant and pen.getLargeCapacity? Commented Mar 23, 2011 at 15:15
  • penremainder = (penboxes-(int)penboxes);//will yield zero, what data type is penremainder? Commented Mar 23, 2011 at 15:15
  • When you step through the code in a debugger what are the values of the product which sets this variable e.g. what is pen.getLargeCapacity() ? Commented Mar 23, 2011 at 15:15
  • Why are you using double when counting discrete units? Commented Mar 23, 2011 at 15:16
  • @james_bond, penboxes is a double however penquant/pen.getLargeCapacity() may use integer division meaning there is no fractional part. ;) Commented Mar 23, 2011 at 15:17

6 Answers 6

3

What are the types of penquant and pen.getLargeCapacity()? If they are both integers, you are performing integer division which has no fractional component (the remainder is discarded). The integer result would then be promoted to a double.

So if that's the case you can instead try

penboxes = ((double)penquant)/pen.getLargeCapacity();
Sign up to request clarification or add additional context in comments.

3 Comments

+1 This most likely the problem. An alternative fix to the one you propose is to let java do the integer division and calculate the remainder using the % operator, which should eliminate the need for the penboxes variable altogether.
@gcooney: consider posting that as an answer, I was too lazy with this question to care for figuring out the "proper" fix since that would require me to be more than just a compiler ;-).
done....I'm sometimes unsure where to draw the line between adding a new answer and just placing a comment :)
2

I guess, in your code, both penquant and getLargeCapacity() are integer. so the result of division is also integer. try like,

penboxes = penquant/((double)pen.getLargeCapacity());

or

penboxes = ((double)penquant)/pen.getLargeCapacity();

Comments

2

As @Mark Peters noted, the issue is most likely that penquant and pen.getLargeCapacity() are both integers meaning java is doing integer division and then casting the integer result to a double. An alternative fix to the one he posted is to change the lines:

penboxes = penquant/pen.getLargeCapacity(); 
penlargeboxes = ((int)penboxes);
penremainder = (penboxes-(int)penboxes );

pensnotinbox=penremainder*pen.getLargeCapacity();

to take advantage of built in integer division and the modulo operator and eliminate a step altogether. The modified code would look like this:

penlargeboxes = penquant/pen.getLargeCapacity(); 
pensnotinbox = penquant % pen.getLargeCapacity(); 

5 Comments

This isn't exactly the same thing yet. For example, if penquant was 5 and pen.getLargeCapacity() was 2, in the OP's original code he would expect penremainder to be 0.5, but your code would give 1. You would need to do (penquant % pen.getLargeCapacity) / ((double)pen.getLargeCapacity()).
Your penremainder would however get him the value he expects for pensnotinbox which is what he uses penremainder to compute anyways, so maybe you could update your answer with that.
hmm when i tryed the modified code for some reason it didnt come out right
I liked your second method i am going to implement it
Good point. I've modified the answer to calculate pensnotinbox directly.
0

Look at this part:

penremainder = (penboxes-(int)penboxes );

pensnotinbox = (penremainder * pen.getLargeCapacity());

After the first line, penremainer has to be 0. Then you're multiplying 0 with pen.getLargeCapactiy(). The result has to be 0 too.

3 Comments

No, not if penboxes has a fractional component. -1.
@Mark Peters do you have any other explanaiton?
And depending on penquant, penboxes can't maybe have a fractional compontent.
0

Your first condition states:

 if (pen.getLargeCapacity() <= penquant) ...

But what

 if (pen.getLargeCapacity() > penquant) ...

???

1 Comment

I think that is a miniscule point right now, we don'e even have all the supporting functions.
-2
penremainder = (penboxes-(int)penboxes );

This line will always equal zero, you are subtracting penboxes from itself.

1 Comment

It will only be zero if the value of penboxes is an integer.

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.