Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2006, 8:20 PM   #31
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
[php]
import java.util.Scanner;

public class Temperature
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);

double Celsius=0,Fahrenheit=0,tempValue=0;
int choice=0;

System.out.print("+===========================+\n");
System.out.print("| [1] Convert to Fahrenheit |\n");
System.out.print("| [2] Convert to Celsius |\n");
System.out.print("+===========================+\n");
System.out.print("+->");

choice=input.nextInt();

switch(choice)
{
case 1:

System.out.print("Enter Celsius: ");
Celsius=input.nextDouble();

tempValue=(Celsius+32)*(0.55555555555555555555555555555556);

System.out.printf("Fahrenheit = %.2f",tempValue);
break;

case 2:

System.out.print("Enter Fahrenheit: ");
Fahrenheit=input.nextDouble();

tempValue=(Fahrenheit-32)*(1.8);

System.out.printf("Celsius = %.2f",tempValue);
break;

default:

System.out.println("Invalid Input... Try Again.");
break;
}
}
}
[/php]

Maybe you can explain to me why i convert from Celsius to Fahrenheit.. get 2 numbers try to reverse and get different numbers... i messed up
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Feb 15th, 2006, 8:30 PM   #32
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
dammn it, that pisses me off! I didnt think of the scheisse Switch statement to do it! I would have been A LOT easier than what I did! OMG!

[PHP]

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Celsius extends JApplet implements ActionListener
{
double celsius;
double fahrenheit;
JButton convertbutton;
JLabel labelC;
JLabel labelF;
JTextField celField;
JTextField fahrField;
public void init()
{
super.setSize(400, 150);
Container window = getContentPane();
window.setLayout(new FlowLayout());
labelC = new JLabel("Celsius");
labelF = new JLabel("Fahrenheit");
convertbutton = new JButton("Convert");
convertbutton.addActionListener(this);
celField = new JTextField(10);
fahrField = new JTextField(10);
window.add(labelF);
window.add(fahrField);
window.add(labelC);
window.add(celField);
window.add(convertbutton);

}
public void actionPerformed(ActionEvent actionEvent)
{
String input = fahrField.getText();
fahrenheit = Double.parseDouble(input);
setConversion(fahrenheit);
celsius = getConversion();
celsius = Math.round(celsius);
celField.setText(Double.toString(celsius));
}
public void setConversion(double d)
{
celsius = ((5.0 / 9.0) * (d - 32.0));
}
public double getConversion()
{
return celsius;
}
}
[/PHP]
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Feb 15th, 2006, 8:43 PM   #33
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 572
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Here is a suggestion... instead of putting a lot of spaces use escape keys.

System.out.println("Hello\t" + "World");
the output would be something like

Hello World

Oh yeah... I think the "Keyboard Class" will replace the Keyboard Listener even in an applet. You can get the "Keyboard Class" from my signature.
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Feb 15th, 2006, 8:45 PM   #34
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
converting is easy.. but can you convert back? mine isn't working!!!

i set celsius to 50 then i convert
i get 147.60 fahrenheit.

then when i set fahrenheit to 147.60 and convert
i get 64.22 celsius? WTF???


EDIT: My New Code
[php]
import java.util.Scanner;

public class Temperature
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);

double Celsius=0,Fahrenheit=0,tempValue=0;
int choice=0;

System.out.print("+===========================+\n");
System.out.print("| [1] Convert to Fahrenheit |\n");
System.out.print("| [2] Convert to Celsius |\n");
System.out.print("+===========================+\n");
System.out.print("+->");

choice=input.nextInt();

switch(choice)
{
case 1:

System.out.print("Enter Celsius: ");
Celsius=input.nextDouble();

tempValue=(Celsius+32)*(9.0/5.0);

System.out.printf("Fahrenheit = %.2f",tempValue);
break;

case 2:

System.out.print("Enter Fahrenheit: ");
Fahrenheit=input.nextDouble();

tempValue=(Fahrenheit-32)*(5.0/9.0);

System.out.printf("Celsius = %.2f",tempValue);
break;

default:

System.out.println("Invalid Input... Try Again.");
break;
}
}
}
[/php]

EDIT2: Here is a tip for you reggaeton_king to shorten your code
[php]

celsius = setConversion(fahrenheit);

public double setConversion(double d)
{
return ((5.0 / 9.0) * (d - 32.0));
}
[/php]
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Feb 15th, 2006, 8:45 PM   #35
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Why even that?
just do this
[PHP]
System.out.println("Hello\tWorld");
[/PHP]
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Feb 15th, 2006, 8:48 PM   #36
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 572
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Oops sorry I forgot about that. Yeah that works too.
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Feb 15th, 2006, 8:49 PM   #37
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Quote:
Originally Posted by Kilo
converting is easy.. but can you convert back? mine isn't working!!!

i set celsius to 50 then i convert
i get 147.60 fahrenheit.

then when i set fahrenheit to 147.60 and convert
i get 64.22 celsius? WTF???
your formula is wrong

do this
[PHP]
//from celsius to fahrenheit
tempValue = (9 / 5) * Celsius + 32;

//from fahreheit to celsius
tempValue = ( 5 / 9 ) * (Fahrenheit - 32);
[/PHP]

tell us the results!

EDIT: Now you did it! lol you did it while I was posting this! lol
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Feb 15th, 2006, 8:50 PM   #38
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
i edited my previous post, please observe.


i edited my code to match your post.. and it still doesn't work

[php]
switch(choice)
{
case 1:

System.out.print("Enter Celsius: ");
Celsius=input.nextDouble();

tempValue=(9.0/5.0) * (Celsius+32);

System.out.printf("Fahrenheit = %.2f",tempValue);
break;

case 2:

System.out.print("Enter Fahrenheit: ");
Fahrenheit=input.nextDouble();

tempValue=(5.0/9.0) * (Fahrenheit-32);

System.out.printf("Celsius = %.2f",tempValue);
break;

default:

System.out.println("Invalid Input... Try Again.");
break;
}
[/php]
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Feb 15th, 2006, 8:51 PM   #39
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
lol me too now!
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Feb 15th, 2006, 8:54 PM   #40
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
did you catch that tip for you to shorten code?
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC