Solved

Temperature Converter Program

Posted on 2007-11-30
6
1,773 Views
Last Modified: 2013-11-23
Hi,
I'm just looking for some help in getting this code to compile. Any suggestions would be much appreciated.
Regards,
Niall
class TemperatureConverter 

{

	public static void main(String[] args) 

	{

		System.out.print("Enter temperature type. Fahrenheit [F/f], Celsius [C/c] :   ");

		String temp_unit = TextIO.getlnString();

		System.out.print("Enter the temperature: ");

		Double temp_value = TextIO.getlnDouble();

		

		if(		temp_unit.equals("f")	||	temp_unit.equals("F")	)

			System.out.println("Fahrenheit mapped to Celsius = " + covertCtoF( temp_value ) );

		else if(		temp_unit.equals("c")	||	temp_unit.equals("C")	)

			System.out.println("Fahrenheit mapped to Celsius = " + covertFtoC( temp_value ) );

		else	

			System.out.println("Enter only C/c or F/f, please try again.");

			break;

		

	}

	

	static double covertCtoF(Double temp_C)

	{

		return ( ( temp_C / 5 ) * 9 ) + 32;

	}
 

	static double covertFtoC(Double temp_F)

	{

		return ( ( temp_F - 32 )  / 9 ) * 5;

	}

}

Open in new window

0
Comment
Question by:niallmunroe
6 Comments
 
LVL 3

Accepted Solution

by:
CPlusJavaCSharp earned 150 total points
Comment Utility
What is TextIO- looks like a user defined class for input- should you import it...
What about Scanner class for input..

You also repeat Fahrenheit mapped to Cel.. one should be Celsuis mapped to Fahern...?

0
 

Author Comment

by:niallmunroe
Comment Utility
Many thanks for your reply. Done some more work & it compiles and runs ok now.
TextIO is user defined but compiled in the directory where stored so no need to import.
Well spotted F mapped to C twice.

Just one more small issue-How do I format the output to two decimal places?

if(temp_unit.equals("f")	||	temp_unit.equals("F")	)

System.out.printf("The temperature "+temp_value+" degrees Celsius is "+covertCtoF(temp_value)+" degrees Fahrenheit");

Open in new window

0
 
LVL 11

Assisted Solution

by:spoxox
spoxox earned 150 total points
Comment Utility
Info on formatting the output can be found here:

http://java.sun.com/docs/books/tutorial/java/data/numberformat.html

0
 
LVL 1

Expert Comment

by:kunalthakur
Comment Utility
here is an example....
System.out.format("%.2f%n", tempvalue);
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now