Author Topic: Fahrenheit <=> Celsius Converter  (Read 10846 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Fahrenheit <=> Celsius Converter
« on: April 09, 2006, 01:56:18 pm »
http://www.javaop.com/uploads/guest/FCConverter.tar.gz

It's more of an example of how to do this than anything. Plus it shows how to use the Java tool line to create javadocs (and cut them down to only what you need), run the java disassembler (and stick it in a file), and of course, compile. Shell script included to do that stuff.

EDIT -
Replaced assembler with disassembler (hehe, oops).
« Last Edit: April 09, 2006, 02:02:27 pm by Joe »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: F<=>C Converter
« Reply #1 on: April 09, 2006, 01:59:13 pm »
F?

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: F<=>C Converter
« Reply #2 on: April 09, 2006, 02:01:13 pm »
Fahrenheit
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Fahrenheit <=> Celsius Converter
« Reply #3 on: April 09, 2006, 02:26:37 pm »
No Kelvins? :(

Offline Ergot

  • 吴立峰 ^_^ !
  • x86
  • Hero Member
  • *****
  • Posts: 3724
  • I steal bandwidth. p_o
    • View Profile
Re: Fahrenheit <=> Celsius Converter
« Reply #4 on: April 09, 2006, 02:43:36 pm »
No Kelvins? :(
Just add 273 to Celcius :\
Who gives a damn? I fuck sheep all the time.
And yes, male both ends.  There are a couple lesbians that need a two-ended dildo...My router just refuses to wear a strap-on.
(05:55:03) JoE ThE oDD: omfg good job i got a boner thinkin bout them chinese bitches
(17:54:15) Sidoh: I love cosmetology

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Fahrenheit <=> Celsius Converter
« Reply #5 on: April 09, 2006, 03:10:54 pm »
Just add 273 to Celcius :\

I know. : - P

I want his program to do it for me.

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Fahrenheit <=> Celsius Converter
« Reply #6 on: April 09, 2006, 03:20:38 pm »
It's open source for a reason! <3.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: Fahrenheit <=> Celsius Converter
« Reply #7 on: April 09, 2006, 04:06:12 pm »
No Kelvins? :(
Just add 273 to Celcius :\
273.15

It's open source for a reason! <3.
And that reason is?
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Fahrenheit <=> Celsius Converter
« Reply #8 on: April 09, 2006, 04:21:23 pm »
Quote
And that reason is?
You're kidding, right? To show others how I did it, and for Sigh-dough to add his nice modification. ^_^
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Fahrenheit <=> Celsius Converter
« Reply #9 on: April 09, 2006, 04:21:39 pm »
273.15

Depends on significant digits and desired accuracy. ;)

Offline deadly7

  • 42
  • Moderator
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
Re: Fahrenheit <=> Celsius Converter
« Reply #10 on: April 09, 2006, 06:24:18 pm »
Is it me or does this seem like a really basic thing to make?
[17:42:21.609] <Ergot> Kutsuju you're girlfrieds pussy must be a 403 error for you
 [17:42:25.585] <Ergot> FORBIDDEN

on IRC playing T&T++
<iago> He is unarmed
<Hitmen> he has no arms?!

on AIM with a drunk mythix:
(00:50:05) Mythix: Deadly
(00:50:11) Mythix: I'm going to fuck that red dot out of your head.
(00:50:15) Mythix: with my nine

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: Fahrenheit <=> Celsius Converter
« Reply #11 on: April 09, 2006, 06:26:26 pm »
Is it me or does this seem like a really basic thing to make?
z0mG N00es!  J00 gotta get teh 1nput, m4ke 1t a num|3er, times and add teh numb3rz, n put 1t on t3h scr33nz.  c0mp|_icated!!!!!!1!1!11!1!one11!
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Quik

  • Webmaster Guy
  • x86
  • Hero Member
  • *****
  • Posts: 3262
  • \x51 \x75 \x69 \x6B \x5B \x78 \x38 \x36 \x5D
    • View Profile
Re: Fahrenheit <=> Celsius Converter
« Reply #12 on: April 09, 2006, 06:36:20 pm »
Is it me or does this seem like a really basic thing to make?

In most "learn to program" books, this is the 2nd or 3rd program it has you make. Complexity is not the point here, not even usefulness (though I can see a situation where someone might find it handy). The point is that he's practicing programming and (supposedly) learning new things.

From your comment, I take it you can make this same program in a quick 5 minutes? Thought so.
Quote
[20:21:13] xar: i was just thinking about the time iago came over here and we made this huge bomb and light up the sky for 6 min
[20:21:15] xar: that was funny

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: Fahrenheit <=> Celsius Converter
« Reply #13 on: April 09, 2006, 06:55:10 pm »
The point is that he's practicing programming and (supposedly) learning new things.

I might say that he's not.  He's still doing typical Joe things.  For instance:
Code: [Select]
if ((args.length == 0) || (args.length == 1))
How about:
Code: [Select]
if (args.length < 2)
so that we only need one runtime check if args.length == 1?

Then he's got this:
Code: [Select]
System.out.println(args[1] + " F = " + fToC(Double.parseDouble(args[1])) + " C.");
...which is not the most readable thing ever.  Not exactly the most beneficial for him learning or someone else learning.

Then he has inconsistent accessibility which is shown in his JavaDoc:
Code: [Select]
public static double fToC(double temp)
private static double cToF(double temp)
An external user trying to convert Celsius to Fahrenheit will get a compile-time error because cToF is private.

Finally, his algorithms are just wrong:
Code: [Select]
/**
Converts a temperature from fahrenheit to celsius
@param temp Temperature to convert (fahrenheit)
@return Converted temperature (celsius)
*/
public static double fToC(double temp)
{
return 100.0 / 212 * (temp - 32.0);
}

/**
Converts a temperature from celsius to fahrenheit
@param temp Temperature to convert (celsius)
@return Converted temperature (fahrenheit)
*/
private static double cToF(double temp)
{
return 212.0 / 100 * temp + 32.0;
}
It's well-known that the appropriate formulae for converting between Celsius and Fahrenheit are the following:
C to F: degreesC * 9 / 5 + 32
F to C: (degreesF - 32) * 5 / 9

For example, when converting 10 C to Fahrenheit, Joe's program would return 53.2 F, where the correct value is actually 50 F. 

Also, it is commonly known that the Fahrenheit and Celsius scales intersect at -40 degrees.  Joe's algorithms, however, return the following:
-40 C to F: -52.8
-40 F to C: -33.96

Ultimately this is a completely and utterly worthless "tutorial," with the exception that it shows that you should use .equalsIgnoreCase when you're parsing command-line arguments.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline deadly7

  • 42
  • Moderator
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
Re: Fahrenheit <=> Celsius Converter
« Reply #14 on: April 09, 2006, 07:00:28 pm »
Is it me or does this seem like a really basic thing to make?

In most "learn to program" books, this is the 2nd or 3rd program it has you make. Complexity is not the point here, not even usefulness (though I can see a situation where someone might find it handy). The point is that he's practicing programming and (supposedly) learning new things.

From your comment, I take it you can make this same program in a quick 5 minutes? Thought so.
Uh.  Joe boasts about his programming knowledge, and unless he picked up a new language I doubt this is learning something new for him.

I don't program.  Programming bores me.
[17:42:21.609] <Ergot> Kutsuju you're girlfrieds pussy must be a 403 error for you
 [17:42:25.585] <Ergot> FORBIDDEN

on IRC playing T&T++
<iago> He is unarmed
<Hitmen> he has no arms?!

on AIM with a drunk mythix:
(00:50:05) Mythix: Deadly
(00:50:11) Mythix: I'm going to fuck that red dot out of your head.
(00:50:15) Mythix: with my nine

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Fahrenheit <=> Celsius Converter
« Reply #15 on: April 09, 2006, 07:05:54 pm »
I got annoyed at Blaze for correcting me, saying Winnipeg was 9 degrees, when I said it was 48 (same thing). So, I wrote this~
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline deadly7

  • 42
  • Moderator
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
Re: Fahrenheit <=> Celsius Converter
« Reply #16 on: April 09, 2006, 07:18:41 pm »
(9^2)/5 = 16.2
16.2+32 = 48.2
You both lose.
[17:42:21.609] <Ergot> Kutsuju you're girlfrieds pussy must be a 403 error for you
 [17:42:25.585] <Ergot> FORBIDDEN

on IRC playing T&T++
<iago> He is unarmed
<Hitmen> he has no arms?!

on AIM with a drunk mythix:
(00:50:05) Mythix: Deadly
(00:50:11) Mythix: I'm going to fuck that red dot out of your head.
(00:50:15) Mythix: with my nine

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: Fahrenheit <=> Celsius Converter
« Reply #17 on: April 09, 2006, 09:30:46 pm »

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Fahrenheit <=> Celsius Converter
« Reply #18 on: April 10, 2006, 12:07:12 pm »
The point is that he's practicing programming and (supposedly) learning new things.

I might say that he's not.  He's still doing typical Joe things.  For instance:
He's still learning.  He'll especially learn from criticisms that you provide, but it helps if you don't phrase it in such a negative way.  Is it really necessary to have a "you're an idiot" tone when a "this would be better" tone would suffice?

Code: [Select]
if ((args.length == 0) || (args.length == 1))
How about:
Code: [Select]
if (args.length < 2)
so that we only need one runtime check if args.length == 1?
I like Joes way better.  The code will end up the same after being compiled, but Joe's way is more clear to people reading it. 

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: Fahrenheit <=> Celsius Converter
« Reply #19 on: April 10, 2006, 01:23:20 pm »
Code: [Select]
if ((args.length == 0) || (args.length == 1))
How about:
Code: [Select]
if (args.length < 2)
so that we only need one runtime check if args.length == 1?
I like Joes way better.  The code will end up the same after being compiled, but Joe's way is more clear to people reading it. 
Uhh, wha-?

That might be the case in Java, but *only* for the length property.  The thing about some languages such as Java, C, C#, etc. is that by calling methods you could inadvertantly change the class.

For example, if (instead of saying .length), you had to use a function like .getLength(), Java would have no way of knowing that the array object didn't change state and that it could optimize it out.  C fortunately offers that option to intelligent programmers:
Code: [Select]
class CArray {
  public:
    int getLength() const;
}

with the const modifier on class methods.

The problem is that what the assembler will produce is something like this:
Code: [Select]
mov    eax, [edx+08h]   ; current
cmp    eax, 0
jz       conditional_okay
cmp    eax, 1
jnz      conditional_false
conditional_okay:
  ; do stuff inside the conditional scope

conditional_false:
  ; ...
Whereas with my way, there's only one conditional evaluation:
Code: [Select]
mov    eax, [edx+08h]
cmp    eax, 2
jge     conditional_false
 ; do stuff inside the conditional scope
conditional_false:
 ; else --
Now, let's say that we're calling a method (non-virtual) from a class that's not marked const:
Code: [Select]
mov    edx, [ebx+08h]
call     edx
cmp    eax, 0
jz       conditional_okay
mov    edx, [ebx+08h] ; depending on whether edx was overwritten, this instruction may be unnecessary.  The linker knows.
call     edx
cmp    eax, 1
jnz      conditional_false
conditional_okay:
  ; do stuff inside the conditional scope

conditional_false:
  ; ...

Hopefully you see the problem.

Plus, what if Joe made a "debuffer" in Java?  Then, using this line of coding would be something like:
Code: [Select]
if (pck.getInt32() == 0 || pck.getInt32() == 1)
which would DEFINITELY lead to runtime errors as he read too quickly through a packet where this value did not initially equal zero.

The other nice thing about my method, besides what I've already given you, is that it guards you from some exceptional cases.  For example, the .indexOf function returns -1 when it cannot find an instance of the specified string within another string.  This is an exceptional case -- it shouldn't require an Exception object, but it should be able to be handled.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Fahrenheit <=> Celsius Converter
« Reply #20 on: April 10, 2006, 05:05:08 pm »
Uhh, what?  Aren't we talking about commandline parameters?

He wants to know if there are 0 or 1 commandline parameters.  Anybody who views his source will instantly see that he is looking for 0 or 1 commandline paremeters.  If he uses <2, then I stop and think, "Less than 2, that's 1 and 0 and negatives.  But negatives can't be here, so just 1 and 0."  Whereas if he uses 0||1, I think "Ah, he needs 0 or 1 parameters".  That's it! 

In other cases, it really depends on the situation.  But in this case, I prefer to spell out the exact number of parameters I want, not use a < or > comparison. 

The main point is, you're complaining about his coding style there.  He does it one way, you do it another.  That doesn't make you right and him wrong, or vice versa. 


Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: Fahrenheit <=> Celsius Converter
« Reply #21 on: April 17, 2006, 12:38:24 am »
I knew that his formula wasn't the one I used, but was too lazy to see if it worked out.  :)
And like a fool I believed myself, and thought I was somebody else...