programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# While loops

Shal Lango
Ranch Hand
Posts: 31
Newton’s Law of Cooling states that when a hot liquid is placed in a cool
room, each minute the decrease in the temperature is approximately proportional
to the difference between the liquid’s temperature and the room’s temperature.
That is, there is a constant k such that each minute the temperature loss is
k # (liquids temperature - rooms temperature). Suppose a cup of 212°F coffee is
placed in a 70°F room and that k = .079. Determine the number of minutes required
for the coffee to cool to below 150°F.

I'm not completely sure about my math here. I haven't fully coded yet, I just want to get the math right first.

Shal Lango
Ranch Hand
Posts: 31
Shal Lango wrote:Newton’s Law of Cooling states that when a hot liquid is placed in a cool
room, each minute the decrease in the temperature is approximately proportional
to the difference between the liquid’s temperature and the room’s temperature.
That is, there is a constant k such that each minute the temperature loss is
k # (liquids temperature - rooms temperature). Suppose a cup of 212°F coffee is
placed in a 70°F room and that k = .079. Determine the number of minutes required
for the coffee to cool to below 150°F.

I'm not completely sure about my math here. I haven't fully coded yet, I just want to get the math right first.

I meant minutes += 1 on line 9

Liutauras Vilda
Sheriff
Posts: 4923
334
• 1
Your code won't achieve what the formula says.

One of the reasons are, that you never update liquid's (which is in a cup I pressume) temperature.

Shal Lango
Ranch Hand
Posts: 31
Liutauras Vilda wrote:Your code won't achieve what the formula says.

One of the reasons are, that you never update liquid's (which is in a cup I pressume) temperature.

Like this?
k = 0.79;
room = 70;
cup = 212;
tempLoss = 0;
minutes = 0;

while (cup <= 212 & cup >= 150) {
tempLoss = k*(cup - room);
tempLoss = cup
minutes += 0;
}

Liutauras Vilda
Sheriff
Posts: 4923
334
Not really.

Read the formula you wrote once again.

An example. I have 100 \$ in my account. For the internet I pay 20\$ a month. How many months I can be calmed?

What you saying now is:

While the answer should be 5, in your case would be 1 using that loop.

Liutauras Vilda
Sheriff
Posts: 4923
334
So, maybe subtraction does the job?

If I have 100\$ in my account, and every month pay 20\$. That means after or before month begins I pay 100 - 20 = 80. Then after the month 80 - 20...

Shal Lango
Ranch Hand
Posts: 31
Liutauras Vilda wrote:So, maybe subtraction does the job?

If I have 100\$ in my account, and every month pay 20\$. That means after or before month begins I pay 100 - 20 = 80. Then after the month 80 - 20...

cup -= tempLoss
?

Shal Lango
Ranch Hand
Posts: 31
Liutauras Vilda wrote:So, maybe subtraction does the job?

If I have 100\$ in my account, and every month pay 20\$. That means after or before month begins I pay 100 - 20 = 80. Then after the month 80 - 20...

cup -= tempLoss
?

Liutauras Vilda
Sheriff
Posts: 4923
334
• 1
That is a basic thing. I have explained I think try do the math on a paper and see if it makes sense.

Shal Lango
Ranch Hand
Posts: 31
Liutauras Vilda wrote:That is a basic thing. I have explained I think try do the math on a paper and see if it makes sense.

Thank you!

Campbell Ritchie
Marshal
Posts: 56576
172
What types are the three numbers? You can get incorrect results if you use the wrong types.
You have written 0.79 when the question says 0.079.
Why have you written the bit about <= 212? Does that do anything useful?
There is probably a method using logarithms which is simpler but you are probably being taught how to write a loop.

 Don't get me started about those stupid light bulbs.