Logic issue(I believe) stumped
I am trying to write a problem that calculates the number of boxes and containers based on the total number of cookies input.
Even trying to step through each line I cannot figure it out. Here's my code:
Code :
import java.util.*;
public class Ch4_PrEx7
{
static Scanner console = new Scanner(System.in);
static final int COOKIES_PER_BOX = 24;
static final int BOXES_PER_CONTAINER = 75;
public static void main(String[] args)
{
int totalCookies = 0, numOfBoxes = 0, remainderCookies = 0, remainderBoxes = 0, numOfContainers = 0;
System.out.println("Please enter the total number of cookies: ");
System.out.println();
totalCookies = console.nextInt();
System.out.println("The total number of boxes needed is: " + String.format("%d%n", Boxes(totalCookies, numOfBoxes)));
System.out.println("The total number of containers needed is: " + String.format("%d%n", Containers(numOfBoxes, numOfContainers)));
System.out.println("The number of left over boxes is: " + String.format("%d%n", LeftOverBoxes(numOfBoxes, remainderBoxes)));
System.out.println("The number of left over cookies is: " + String.format("%d%n", LeftOverCookies(totalCookies, remainderCookies)));
}
public static int Boxes(int totalCookies, int numOfBoxes)
{
numOfBoxes = totalCookies / COOKIES_PER_BOX;
return numOfBoxes;
}
public static int Containers(int numOfBoxes, int numOfContainers)
{
numOfContainers = numOfBoxes / BOXES_PER_CONTAINER;
return numOfContainers;
}
public static int LeftOverCookies(int totalCookies, int remainderCookies)
{
remainderCookies = totalCookies % COOKIES_PER_BOX;
return remainderCookies;
}
public static int LeftOverBoxes(int numOfBoxes, int remainderBoxes)
{
remainderBoxes = numOfBoxes % BOXES_PER_CONTAINER;
return remainderBoxes;
}
}
Thanks in advance!
Re: Logic issue(I believe) stumped
Your problem is you do not assign the values returned from the function into the variables you declared.
Java passes primitives such as integers by value so you should not expect numOfBoxes,numOfContainers etc to have
their new value. Try to print what the values of those parameters are at the end of your main. I bet they are all zero.
Other than that the logic seem correct to me.
Re: Logic issue(I believe) stumped
I did what you said. The numOfBoxes was correct and remainderCookies was correct. NumOfContainers and remainderBoxes are zero. So I am concluding that
they are not getting these values after the previous method valuations. Can you return a value to another method.I think that is what I ultimately have to do. I need to get the numOfBoxes value after Boxes() to Containers() and LeftOverBoxes(). How can I do this?
Re: Logic issue(I believe) stumped
No the values of the variables should all be zero. You are directly printing out what is returned from the functions
without assigning it to your variables. So what you probably saw is those returned values. To understand this issue do this
Code java:
String message = String.format("%d %d %d %d \n",
numOfBoxes,
numOfContainers,
remainderCookies,
remainderBoxes
);
System.out.println(message);
That will tell you they are all zero. To get the desired behavior you have to assign them outside the function
numOfBoxes = Boxes(..)
numOfContainers = Containers(..)
etc..
Re: Logic issue(I believe) stumped
Thanks a lot. It was actually not that hard to fix after your last post.What I did was: numOfBoxes = Boxes(numOfBoxes, totalCookies); I did this after the
method Boxes() returned numOfBoxes, That way when the numOfBoxes with the new value was passed as an argument to the Container() and
the LeftOverBoxes() method, they returned the correct values.