Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jun 9th, 2005, 8:54 AM   #1
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Temperature Conversion Program - Help!

I am soooooo lost!

Here's the assignment: "Write a program that allows the user to convert either from degrees Celsius to Fahrenheit or from degrees Fahrenheit to Celsius. In a loop, prompt the user to enter a temperature (as an integer). In a nested loop, prompt the user to enter a letter for Celsius or Fahrenheit. Use a switch statement to analyze the user input and to request to reenter the letter (but not the temperature) in case of invalid input. Print the result of conversion as a double and prompt the user to choose to continue or to quit."

More details from the book assignment: "Write a program that allows the user to convert either from degrees Celsius to Fahrenheit or from degrees Fahrenheit to Celsius. Use the following formulas:

degreesC = 5(degreesF = 32)/9
degreesF = (9(degreesC)/5) + 32

Prompt the user to enter a temperature and either a 'C' (or 'c') for Celsius or an 'F' (or 'f') for Fahrenheit; allow either uppercase or lowercase, but if anything other than 'C', 'c', 'F', or 'f' is entered, print an error message and ask the user to reeneter a valid selection (uppercase or lowercase 'C' or 'F'). Convert the temperature to Fahrenheit if Celsius is entered, or to Celsius if Fahrenheit is entered, and then ask the user to press 'Q' or 'q' to quit or to press any other key to repeat the loop and perform another conversion."

import java.util.*;

public class TempConversion
{
    public static void main(String[] args)
    {
    	int degreesC, degreesF;
    	
    	System.out.println("=====TEMPERATURE CONVERSION=====");
    	System.out.println();
    	System.out.println("Please enter a whole number for");
    	System.out.println("conversion:");
    	
    	Scanner keyboard = new Scanner(System.in);
    	
    	
    	degreesC = keyboard.nextInt();
    	degreesF = keyboard.nextInt();
    }
}

This is how far I am. Not far at all, in other words. I desperately need help on this one

Thanks
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 9:09 AM   #2
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
I don't understand why it says "in a loop, prompt the user to enter a temperature (as an integer)"... isn't what I've come up with above perfectly sufficient for this first user-input?
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 9:15 AM   #3
Knight
Newbie
 
Join Date: Jun 2005
Posts: 28
Rep Power: 0 Knight is on a distinguished road
You have no loops in your code. That is your first problem. There are different types of loops, i.e. for while do-while etc... Try those and see what you come up with.
Knight is offline   Reply With Quote
Old Jun 9th, 2005, 9:18 AM   #4
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Working on it. I just don't understand why I'm supposed to use a loop just so the user can enter an integer..
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 9:19 AM   #5
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 5 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
also ask the user which conversion they require?
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Jun 9th, 2005, 9:21 AM   #6
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
are you allowed to use JOptionPane ? if yes then:

import java.util.*;

public class TempConversion
{
    public static void main(String[] args)
    {
	double result;
	String degree;
	int input;
    	
	degree = JOptionPane.showInputDialog("Enter: \n C - Fahrenheit to Celsius\n F - Celsius to Fahrenheit");


	input = new Integer(JOptionPane.showInputDialog("Enter the degree:")).intValue();

	if(degree == "C" || degree == "c")
	{
			result = 5.0/9.0 * (input - 32);
			System.out.println(input + " Fahrenheit is " + result + " Celsius");
	}
	else if(degree == "F" || degree == "f")
	{
		result = 9.0/5.0 * input + 32;
		System.out.println(input + " Celsius is " + result + " Fahrenheit");
	}
	
    }
}
__________________
countdown++;
HeX is offline   Reply With Quote
Old Jun 9th, 2005, 9:24 AM   #7
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
No.. we are not doing Swing at all. But thanks.. that helped somewhat
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 9:32 AM   #8
Knight
Newbie
 
Join Date: Jun 2005
Posts: 28
Rep Power: 0 Knight is on a distinguished road
Quote:
Originally Posted by shangnyun
Working on it. I just don't understand why I'm supposed to use a loop just so the user can enter an integer..
I think they're trying to teach you how to handle invalid input. You don't want your *strong deep voice* huge immensely powerful super program *end strong deep voice* that you worked on for months and cost the company thousands or millions of dollars (heh, millions..right..) to crash because of a simple invalid input (scenario for when you are working for a major corporation or something). Its just a simple check to make sure the input is valid so your program can function correctly later on.
Knight is offline   Reply With Quote
Old Jun 9th, 2005, 9:51 AM   #9
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Quote:
Originally Posted by HeX
are you allowed to use JOptionPane ? if yes then:

import java.util.*;

public class TempConversion
{
    public static void main(String[] args)
    {
	double result;
	String degree;
	int input;
    	
	degree = JOptionPane.showInputDialog("Enter: \n C - Fahrenheit to Celsius\n F - Celsius to Fahrenheit");


	input = new Integer(JOptionPane.showInputDialog("Enter the degree:")).intValue();

	if(degree == "C" || degree == "c")
	{
			result = 5.0/9.0 * (input - 32);
			System.out.println(input + " Fahrenheit is " + result + " Celsius");
	}
	else if(degree == "F" || degree == "f")
	{
		result = 9.0/5.0 * input + 32;
		System.out.println(input + " Celsius is " + result + " Fahrenheit");
	}
	
    }
}
OMG... you helped me but also confused the shit out of me. I totally understand your logic in using only one variable for the degree integer, namely "input", but I'm confused why my book is using TWO variables, namely degreesC and degreesF.
And this is without Swing, so no JOptionPane
shangnyun is offline   Reply With Quote
Old Jun 9th, 2005, 10:05 AM   #10
Knight
Newbie
 
Join Date: Jun 2005
Posts: 28
Rep Power: 0 Knight is on a distinguished road
The only reason I could think of why they have (2) variables is that, if you want to use the degreeF later on and you initially converted to degreeC, you just have to use the degreeF variable (which means you don't have to reconvert it again). So, I would stick this in the beginning:
// if they input it in farenheit first

degreesC = 5.0/9.0 * (input - 32);
degreesF = input;

//if they input it in celsius first
degreesF = 9.0/5.0 * input + 32;
degreesC = input;

That way, if you need the temperature in Farenheit, you could just use the variable degreesF and vice versa
Knight 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 5:57 PM.

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