Centigrade to Fahrenheit table help

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 6475 Views - Last Post: 30 June 2009 - 10:31 AM Rate Topic: -----

#1 Nigo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-June 09

Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:09 AM

public class Centigrade_Fahrenheit
{
	public static void main(String[] args)
	 {
		int start = 0;
		final int end = 20;

		System.out.println("Centigrade \t Fahrenheit");
		System.out.println("------------------------------");
		while(start <= end)
		 {
			double fahrenheit = (9/5) * start + 32;
			System.out.println(start + "\t\t" + fahrenheit);
		 	start++;
		 }
	   }
}



very quick code to display 0 to 20 centigrade degree and show its equivalents in fahrenheit. It work.. my program farenheit is like 9 or 8 degree off though. I notice in my program the farenheit show up as (33.00) instead of (33.8). what do i need to do to make the caculation more precise?

Is This A Good Question/Topic? 0
  • +

Replies To: Centigrade to Fahrenheit table help

#2 Tanira   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 102
  • Joined: 30-May 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:15 AM

View PostNigo, on 30 Jun, 2009 - 08:09 AM, said:

public class Centigrade_Fahrenheit
{
	public static void main(String[] args)
	 {
		int start = 0;
		final int end = 20;

		System.out.println("Centigrade \t Fahrenheit");
		System.out.println("------------------------------");
		while(start <= end)
		 {
			double fahrenheit = (9/5) * start + 32;
			System.out.println(start + "\t\t" + fahrenheit);
		 	start++;
		 }
	   }
}



very quick code to display 0 to 20 centigrade degree and show its equivalents in fahrenheit. It work.. my program farenheit is like 9 or 8 degree off though. I notice in my program the farenheit show up as (33.00) instead of (33.8). what do i need to do to make the caculation more precise?


Change 9/5 to 1.8

The reason is java takes 9/5 and rounds it. 9.0/5 or 9/5.0 would also fix it.

This post has been edited by Tanira: 30 June 2009 - 09:17 AM

Was This Post Helpful? 1
  • +
  • -

#3 kmangold   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 167
  • Joined: 24-June 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:15 AM

double fahrenheit = (9 * start) / 5 + 32;

Was This Post Helpful? 0
  • +
  • -

#4 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:17 AM

You are performing integer mathematics. You can either case start as a double, like:
  double fahrenheit = (9/5) * (double) start + 32



Or just make a double to start with. You'll want to make it 9.0/5 also, or you'll only get an integer for that.

View Postkmangold, on 30 Jun, 2009 - 12:15 PM, said:

double fahrenheit = (9 * start) / 5 + 32;


This solution will not get you the correct answer: 9 * start / 5 will perform integer, not real, division.
Was This Post Helpful? 1
  • +
  • -

#5 kmangold   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 167
  • Joined: 24-June 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:21 AM

Very true. It's still a lot closer than what the original poster had in the beginning. This is what they get for half effort in finding the answer on their own, a half a decent response.
Was This Post Helpful? 0
  • +
  • -

#6 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:24 AM

View Postkmangold, on 30 Jun, 2009 - 12:21 PM, said:

Very true. It's still a lot closer than what the original poster had in the beginning. This is what they get for half effort in finding the answer on their own, a half a decent response.


I wouldn't exactly call it a half effort, it simply seems that the OP was not aware of how java does integer division. It's an honest mistake that a lot of people make. The logic in the program is sound.
Was This Post Helpful? 0
  • +
  • -

#7 kmangold   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 167
  • Joined: 24-June 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:26 AM

There are so many examples already online that does celcius to fahrenheit conversion...
Was This Post Helpful? 0
  • +
  • -

#8 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:28 AM

View Postkmangold, on 30 Jun, 2009 - 12:26 PM, said:

There are so many examples already online that does celcius to fahrenheit conversion...


There are, but I would rather have somebody explain to me what I was doing wrong than have to look at an example that just works and hope I can find what I did differently. If there are examples of things online, why does anybody go to school, anyway?
Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:40 AM

If there are soo many examples.. Why did you get it wrong answer anyways? Just because something is"closer" then what they had, doesn't mean you should post it.

Quote

It's still a lot closer than what the original poster had in the beginning.


Most people post on here ask for help because they don't understand something, last thing they need is to be misleaded because you felt like posting the "closest" thing.


As for Nigo-

Like everyone said what you were getting was Integer division.
10/3 = 3 <-- the / sign will tell you how many times goes into it. Will NOT give you a remainder. if it doesnt go into it at all, will be 0. Dividing 2 integers will give you integer Division if there is supposed to be a decimal

2/3 <-- that is 0, 3 cannot go into it.

10 % 3 = 1 <--- the % sign will give you the remainder.

Also, Integer division occurs because there is supposed to be a remained, and you not getting it because dividing by an int. So, you can do 1 of 2 things.

1. Cast as a double (double) 9/5

2. Make one of the numbers a double by adding a .0 9.0/5 or 9/5.0


Hope this helps!

This post has been edited by Fuzzyness: 30 June 2009 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#10 kmangold   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 167
  • Joined: 24-June 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:43 AM

OMG! I gave credit to xclite! I admitted I was wrong! Let it go! I didn't post it because I thought it was closer, I overlooked the fact that it was integer math!
Was This Post Helpful? 0
  • +
  • -

#11 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:45 AM

View Postkmangold, on 30 Jun, 2009 - 12:43 PM, said:

OMG! I gave credit to xclite! I admitted I was wrong! Let it go! I didn't post it because I thought it was closer, I overlooked the fact that it was integer math!


Except that in every instance where you "gave somebody credit" for having a more proper solution, you have to throw in an insult or a snide comment so that you don't take some perceived pride hit. See the other thread about integers from 0 to 9 for another example of that. You could have just said "Oh, I forgot about integer math," instead you accused the OP of being lazy and said there were solutions on the internet.
Was This Post Helpful? 0
  • +
  • -

#12 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4933
  • View blog
  • Posts: 20,259
  • Joined: 24-September 08

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:50 AM

Okaaayyyyyyy..... Everyone is so testy today.....


It looks like your variables are all ints. No matter how you divide them they will always come out whole numbers.
Was This Post Helpful? 0
  • +
  • -

#13 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:54 AM

View PostNeoTifa, on 30 Jun, 2009 - 08:50 AM, said:

Okaaayyyyyyy..... Everyone is so testy today.....


It looks like your variables are all ints. No matter how you divide them they will always come out whole numbers.


More like grouchy :) Just woke up lol

I have actually never used floats or longs before in Java. Is there a similiar problem with those as there is with mathmatical operations or is it pretty much just integers?
Was This Post Helpful? 0
  • +
  • -

#14 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1528
  • View blog
  • Posts: 4,448
  • Joined: 12-May 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:55 AM

View PostFuzzyness, on 30 Jun, 2009 - 12:54 PM, said:

More like grouchy :) Just woke up lol

I have actually never used floats or longs before in Java. Is there a similiar problem with those as there is with mathmatical operations or is it pretty much just integers?


A long will act like an int in division, a float like a double, if I understand your question correctly. Is that what you were asking?
Was This Post Helpful? 0
  • +
  • -

#15 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Centigrade to Fahrenheit table help

Posted 30 June 2009 - 09:56 AM

Yeah it was. Never used those types before, haven't seen a problem where I would need to. Thanks for the answer :)

Yay for hi-jacking a forum! :o
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2