Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

# Conversions

15 replies to this topic

### #1 ussr1943

ussr1943

• Members
• 490 posts
• OFFLINE
•
• Gender:Male
• Location:USA
• Local time:04:42 PM

Posted 29 August 2006 - 06:09 PM

hi again all, i have been very busy theese last two weeks and have been unable to work on my computer and so im down on java again.
i figured to get back into the grove and learn somemore i should review first. so im creating a table of conversions from farenheit to celcius.(yes a table,yes i know thats not great )

anyways i am not sure but when i try to run the program it shows the farenheit increasing, but the celcius always stays -0.0

here is the code

```//converts Farenheit  temp to celcius
class TempTable {
public static void main(String args[]) {
double f, c;
int counter;

counter = 0;
for(f = 1; f <= 25; f++) {
c =((  5 / 9) * (f - 32)); /* im pretty sure it has sum thing to do with this conversion which is written
5/9(f-32)*/

System.out.println(f+"Degrees Farenheit is equivilent to: "
+c+" Degrees Celcius.");

counter++;
if(counter==5) {
System.out.println();
counter=0;
}
}
}
}```

if anyone could give me a hint i would be grateful, and possibly an idea how to make the code a lil better and more cleaner
thanks.
"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"

### #2 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:02:42 PM

Posted 29 August 2006 - 06:40 PM

A couple of things...

First of all, your sentinel in your counter should never be declared outside of the for statement:
for(f = 1; f <= 25; f++) {

It is also not necessary to call it as a double, and just takes up memory. Granted, this is such a small program that memory is not really a problem, but why use a double when an int is adequate.
Second, there is an issue called scope, which is the context of a variable. When you declare it like you did, the variable f is able to be used anywhere within the program. That is bad way to do it. What happens if you forget that you used it as a sentinel, and try to reuse it somewhere else? Again, in code this small, it is not an issue, but what happens when you get to 500 lines of code? What happens if you try to use f later? The safe way to do a for loop is like this:
```for (int f=0; f>=25; f++{
\\todo code
}```
Once the loop has been executed, f goes away and can not cause you problems anywhere else.

The other problem is your division. When doing division on a computer, it works a bit differently. Division returns an int, which is a whole number. (I think I mentioned looking into how division works in another one of your threads). What I would suggest is to put these lines into your code and see what happens (ignore the loop for now)
```System.out.println(5/9);
System.out.println(9/9);
System.out.println(10/9);
System.out.println(12/9);
System.out.println(13/9);
System.out.println(18/9);```

Look at the results and see if you can figure out what is happening. Also, look up what MOD means. Once you understand how division works, then you will understand why I was able to remove the counter in the first program that you posted.

### #3 ussr1943

ussr1943
• Topic Starter

• Members
• 490 posts
• OFFLINE
•
• Gender:Male
• Location:USA
• Local time:04:42 PM

Posted 29 August 2006 - 07:16 PM

thanks for all your help man, very interesting. i am currently revising the program and will be looking up many items, i will post what i can figure out when i get it. thanks again.
"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"

### #4 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:02:42 PM

Posted 29 August 2006 - 07:22 PM

Did you figure out what 5/9 is now? If you found that out, then you will understand why you got the result you fif.

### #5 ussr1943

ussr1943
• Topic Starter

• Members
• 490 posts
• OFFLINE
•
• Gender:Male
• Location:USA
• Local time:04:42 PM

Posted 29 August 2006 - 08:02 PM

alrighty when u said MOD me imediate thought was devision/mod hmm maybe modulus operator? (%)
it made sense to me, but to help demonstrate this i created a quikie used off your example
```class HelpDevision {
public static void main(String args[]) {

System.out.println(5/9);
System.out.println(9/9);//will yield 1
System.out.println(10/9+"with a remainder of "+ 10 % 9);//uses remainder
System.out.println(12/9);//without modulus operator yields 1 should be 1 with a remainder of 3
System.out.println(13/9+"with a remaider of "+13 % 9);
System.out.println(18/9);//yield 2

}
}```

so it makes sence when i tell it
`c =  5 / 9 (f - 32)`
its really doing 5/9 (with out remainder=0) times (f-32) and anything times 0 = 0

well that was pretty neat to think about. so i got that part of the problem partly solved, but now it presents a new one, how do i make it so i can actually do that. well, well, well i thought. why be complicated with the 5/9 when i can just put .555(which is the same, stupid me )
and i declared bout f and c within the scope, thats a great point to bring up about scope, and i know i couldda made f an int before, but i figured it is just a small program why bother. anyways i fixed everything, looked up some stuff and im happy to report that it works perfect (checked against a calculator)
here is the finished code.

```//converts Farenheit  temp to celcius
class TempTable {
public static void main(String args[]) {

for(int f=0; f<=25; f++) {

double c = (.555 * f - 32);
System.out.println(f+"Degrees Farenheit is equivilent to: "
+c+" Degrees Celcius.");

}
}
}```

it works great, thanks again(i certainly like to bug you )
if you have any more suggestions i certainly would like to hear.
"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"

### #6 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:02:42 PM

Posted 29 August 2006 - 08:14 PM

Using .555 is one way to do it, but you lose a bit of precision. For mucking around, that is ok, but for scientific purposes, you want to be as precise as possible. I wonder what would happen if you did 5.0/9.0? What did you get as a result this time?

### #7 ussr1943

ussr1943
• Topic Starter

• Members
• 490 posts
• OFFLINE
•
• Gender:Male
• Location:USA
• Local time:04:42 PM

Posted 29 August 2006 - 09:47 PM

dang, nice one, its the same thing more precise though, but why does it work when u put 5.0/9.0? is it becuase variable C is of type double? and when i was putting in the value as 5/9 it switched to type int?
"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"

### #8 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:02:42 PM

Posted 30 August 2006 - 06:38 AM

Yes. Integer division works differently than when using a double. Tou could have also cast them to doubles by doing (double)5/(double)9, but that takes more typing. However, there are times when you have two ints to divide, and you must cast one or both to a double. One example would be if you needed to determine grade point average.

EDIT: See if you can figure out how to use modulus division to put your space every five lines again.

### #9 ussr1943

ussr1943
• Topic Starter

• Members
• 490 posts
• OFFLINE
•
• Gender:Male
• Location:USA
• Local time:04:42 PM

Posted 30 August 2006 - 04:48 PM

EDIT: See if you can figure out how to use modulus division to put your space every five lines again.

hmm im a little stumped on this part i dont know even what to do but im guessing the statement is goning to be
if() {
System.out.println();
}

im not sure what to put in the if statement, if the remainder of c is == to sumthing? or maybe if its not i have no clue. maybe a hint?

Edited by ussr1943, 30 August 2006 - 04:48 PM.

"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"

### #10 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:02:42 PM

Posted 30 August 2006 - 04:52 PM

Ok. So let's think just a bit. You know that every 5th line, you want a space. Using Mod Division, how would you know when you had hit the fifth line? Maybe in your code, you could add a line like this:
System.out.println("When f=" + f + ", f/5 = " + f/5);

Tell me what happens when f is a multiple of 5.

### #11 ussr1943

ussr1943
• Topic Starter

• Members
• 490 posts
• OFFLINE
•
• Gender:Male
• Location:USA
• Local time:04:42 PM

Posted 30 August 2006 - 04:56 PM

when its 5 = 1
10 = 2
15 = 3
20 = 4
25 = 5

maybe if (f%5==1,2,3,4,5)? maybe sum sort of increse by one or sumthing.

Edited by ussr1943, 30 August 2006 - 04:58 PM.

"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"

### #12 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:02:42 PM

Posted 30 August 2006 - 05:03 PM

Whoops, my bad. Try this instead:
System.out.println("When f=" + f + ", f%5 = " + f%5);

Sorry about that... the sun got in my eye.

### #13 ussr1943

ussr1943
• Topic Starter

• Members
• 490 posts
• OFFLINE
•
• Gender:Male
• Location:USA
• Local time:04:42 PM

Posted 30 August 2006 - 05:10 PM

```//converts Farenheit  temp to celcius
class TempTable {
public static void main(String args[]) {

for(int f=0; f<=25; f++) {

double c = (5.0/9.0 * f - 32);//can replace with .555555555
System.out.println(f+ " Degrees Farhenheit is equivalent to "
+c+" Degrees Celcius.");

if(f%5==0) {
System.out.println();
}
}
}
}```

it works, every 5th line the remainder is 0 so i put if f%5==0 then blanks line, wow. thats pretty neat stuff.
thanks again
hail groovicus indeed
"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"

### #14 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:02:42 PM

Posted 30 August 2006 - 05:12 PM

This is just basic programming stuff, but I can show you another little shortcut. When you do an if statement, if it only has one instruction after, you can ignore the brackets. I am about 99% sure that is true with any language. So your line would then be:
[b]if(f%5==0) System.out.println("");

However, in long code, it is a good idea to use the brackets as a means of maintaining separation.

### #15 ussr1943

ussr1943
• Topic Starter

• Members
• 490 posts
• OFFLINE
•
• Gender:Male
• Location:USA
• Local time:04:42 PM

Posted 30 August 2006 - 05:15 PM

if i wanted to add more things during this block i would definately have to use brackets? but withonly one thing no brackets are needed?
"Ideas are far more powerful than guns."
"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards -- and even then I have my doubts." --Eugene H. Spafford
"One man's terrorist is another's freedom fighter"

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users