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

# Thread: Not understanding this java output...

1. ## 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. ## 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.

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

What happens if you want to drink 18 cups?

5. ## Re: Not understanding this java output...

Originally Posted by kowaii
...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