Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Logic issue(I believe) stumped

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default 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:

    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!


  2. #2
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default 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.

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default 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?

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default 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
    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..

  5. The Following User Says Thank You to dabdi For This Useful Post:

    mwr76 (September 23rd, 2011)

  6. #5
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default 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.

Similar Threads

  1. Array question. Stumped!
    By tjanuranus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 02:35 PM
  2. Replies: 2
    Last Post: January 7th, 2011, 09:10 PM
  3. help im stumped :(
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 9th, 2010, 12:51 PM
  4. Help so stumped....
    By Macgrubber in forum Loops & Control Statements
    Replies: 8
    Last Post: October 28th, 2010, 03:53 PM
  5. Stumped?
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2009, 01:02 AM