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: Not understanding this java output...

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Not understanding this java output...

    We are not allowed to run the code, just doing it by hand. The question is:

    Coffee pot holds 12 cups, if x in an integer represent the amount of cups you need to drink, find the number of pots you need to fill the x cups needed(pot may have left overs after pouring the x cups, but need to be able to fill all the x cups) and the answer is (x/12) + 1; now by hand I understand the x/12 of course but why add one? does it matter that x is in int? what happens if x is a float or double?
    Thank so much for any help


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Not understanding this java output...

    What do you think the answers are? Why?

    Even if you can't write this specific code, you should write a simple test program that tests your assumptions in a more general way.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not understanding this java output...

    Hi, I thought the answer would be simply x/12 but the teacher said it wold be (x/12) + 1, my only guess is that because we have integer division, we will lose remainder so adding one would ensure we always get more than we need (since it is okay to have left overs)?! is that correct?Thank you

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Not understanding this java output...

    What happens if you want to drink 18 cups?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Not understanding this java output...

    Quote Originally Posted by kowaii View Post
    ...teacher said it wold be (x/12) + 1, my only guess is that because we have integer division, we will lose remainder so adding one would ensure we always get more than we need (since it is okay to have left overs)?! is that correct?...
    Well, it's "sort of correct" if there is such a thing.

    Why don't you do a few manual ('mental") calculations and write down the results.

    Forget Java for a minute. Forget about any programming considerations or any stuff about "integer arithmetic." Just think about coffee and coffee pots and cups of coffee.

    Interestingly, there was a similar problem in a fifth grade arithmetic book that I saw. It was not about programming. It was about fifth grade arithmetic, and asked questions for a few specific cases. (It was about pizza slices, not cups of coffee, but the principle was the same.)


    So...


    How many pots will you have to brew if you need zero cups of coffee?

    How many pots if you need 5 cups?

    11 cups?
    12 cups?
    13 cups?
    .
    .
    18 cups?
    .
    .
    23 cups?
    24 cups?
    25 cups?
    .
    .
    .

    Now,, spotting a single elegant mathematical expression that gives correct answers for all possible cups requirements might take a little head-scratching. In fact, a single elegant mathematical expression might not be the thing to look for. Just figure out how to get the right answer first. Then try to express it in terms of mathematical operations that can be put into a program.

    Anyhow...

    First of all, why not write a program that shows results of whatever formula you want to test? Make some kind of loop that lets it calculate the required pot-fillings for various numbers of cups.

    I did my own calculations and here's a list that I concocted for zero through 100 cups.

    x is the number of cups required from a 12-cup brewing machine.

                            My
      x  x/12  x/12+1   Calculation
    --------------------------------
      0    0     1           0 <---> What's the correct answer here?
      1    0     1           1
      2    0     1           1
      3    0     1           1
      4    0     1           1
      5    0     1           1
      6    0     1           1
      7    0     1           1
      8    0     1           1
      9    0     1           1
     10    0     1           1
     11    0     1           1
     12    1     2           1 <---> What's the correct answer here?
     13    1     2           2
     14    1     2           2
     15    1     2           2
     16    1     2           2
     17    1     2           2
     18    1     2           2
     19    1     2           2
     20    1     2           2
     21    1     2           2
     22    1     2           2
     23    1     2           2
     24    2     3           2 <---> What's the correct answer here?
     25    2     3           3
     26    2     3           3
     27    2     3           3
     28    2     3           3
     29    2     3           3
     30    2     3           3
     31    2     3           3
     32    2     3           3
     33    2     3           3
     34    2     3           3
     35    2     3           3
     36    3     4           3 <---> What's the correct answer here?
     37    3     4           4
     38    3     4           4
     39    3     4           4
     40    3     4           4
     41    3     4           4
     42    3     4           4
     43    3     4           4
     44    3     4           4
     45    3     4           4
     46    3     4           4
     47    3     4           4
     48    4     5           4 <---> What's the correct answer here?
     49    4     5           5
     50    4     5           5
     51    4     5           5
     52    4     5           5
     53    4     5           5
     54    4     5           5
     55    4     5           5
     56    4     5           5
     57    4     5           5
     58    4     5           5
     59    4     5           5
     60    5     6           5 <---> What's the correct answer here?
     61    5     6           6
     62    5     6           6
     63    5     6           6
     64    5     6           6
     65    5     6           6
     66    5     6           6
     67    5     6           6
     68    5     6           6
     69    5     6           6
     70    5     6           6
     71    5     6           6
     72    6     7           6 <---> What's the correct answer here?
     73    6     7           7
     74    6     7           7
     75    6     7           7
     76    6     7           7
     77    6     7           7
     78    6     7           7
     79    6     7           7
     80    6     7           7
     81    6     7           7
     82    6     7           7
     83    6     7           7
     84    7     8           7 <---> What's the correct answer here?
     85    7     8           8
     86    7     8           8
     87    7     8           8
     88    7     8           8
     89    7     8           8
     90    7     8           8
     91    7     8           8
     92    7     8           8
     93    7     8           8
     94    7     8           8
     95    7     8           8
     96    8     9           8 <---> What's the correct answer here?
     97    8     9           9
     98    8     9           9
     99    8     9           9
    100    8     9           9


    Now, you see that, according to my calculations, x/12 hardly ever gives the correct answer. Also, according to my calculations, x/12+1 is correct more often than not.

    However...

    For a few values, there is a discrepancy between the teacher's formula and my calculations.

    So now some questions arise:

    1. Do any of the values that I showed disagree with your pencil and paper values? Look at my values and look at your values. Which are correct? If you decide that mine are correct, revisit your calculations and see what could account for the differences. If you decide that yours are correct, show me the steps that allowed you to reach your number.

    2. Look at the values where my answers disagreed with the teacher's formula. Are my values correct? Are the teacher's values correct?

    3. Etc.



    Cheers!

    Z

Similar Threads

  1. Replies: 5
    Last Post: February 6th, 2013, 12:32 PM
  2. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  3. problem in understanding output.
    By vinaysa86 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 5th, 2012, 01:22 AM
  4. Replies: 4
    Last Post: April 8th, 2012, 02:03 PM
  5. Problem understanding Java code modification
    By eagle09 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 26th, 2011, 04:13 PM

Tags for this Thread