School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,158 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,071 people online right now. Registration is fast and FREE... Join Now!



Temperature converter

Temperature converter converts fahrenheit to celsius and celsius to fahrenheit Rate Topic: -----

#1 gmillerlight  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 26
  • Joined: 30-April 09


Dream Kudos: 0

Post icon  Posted 07 June 2009 - 06:53 PM

not asking user for temp to convert just printing print line statements.
package celsiusfahrenheitconverter;
import java.util.Scanner;

/** This java code reads
 *  Fahrenheit temperature the
 * user enters and displays back
 * temperature in Celsius. This also
 * Reads the Celsius temperature
 * entered and displays back
 * Fahrenheit temperature.
 *
 * @author 
 */
public class CelsiusFahrenheitConverter
{

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args)
	{
		  // declare variables
		String from;
		String to;
		String temp;
		int choice;
		int fahrenheit;
		int celsius;

		Scanner input = new Scanner(System.in);
	   
		do
		{
			choice = -1;
			System.out.println("Convert tmperature:");
			System.out.println(" Convert from Fahrenheit to Celsius:");
			System.out.println(" Convert from Celsius to Fahrenheit:");
			System.out.println("Enter 3 - Exit:");
			choice = input.nextInt();

		switch (choice)
		{
			// case 1 asks user for a Fahrenheit temp to convert to Celsius
			// case 2 asks user for a Celsius temp to convert to Fahrenheit
		   case 1:
				 
				System.out.println("Enter the temperature in Fahrenheit:");
				fahrenheit = input.nextInt();
				celsius = convertToCelsius(fahrenheit);
				from = "Fahrenheit";
				to = "Celsius";
				break;

		   case 2:
				 
				System.out.println("Enter the temperature in Celsius:");
				celsius = input.nextInt();
				fahrenheit = convertToFahrenheit(celsius);
				from = "Celsius";
				to = "Fahrenheit";
				break;
			  default:
					continue; // no input or output
		}

				 System.out.print("\n");
				 System.out.print(fahrenheit);
			  
				 System.out.print(from);
				 System.out.print("=");
				 System.out.print(celsius);
			   
				 System.out.print(to);
				 System.out.print('\n');
		}
			while (choice != 3);
	}

			/**
	 * Converts form Celsius to
	 * Fahrenheit
	 * @param temp The temperature
	 * in Celsius
	 * @return The temperature in
	 * Fahrenheit
	 */

	public static int convertToFahrenheit(int temp)
	{
		return (9 * temp)/ 5 + 32;
	}

	/**
	 * Converts from Fahrenheit
	 * to Celsius
	 * @param temp the temperature
	 * in Fahrenheit
	 * @return the temperature in Celsius
	 */

	 public static int convertToCelsius(int temp)
	 {
		 return (temp - 32) / 9 * 5;
	 }

}

/*run:
Convert tmperature:
 Convert from Fahrenheit to Celsius:
 Convert from Celsius to Fahrenheit:
Enter 3 - Exit:*/

This post has been edited by gmillerlight: 07 June 2009 - 06:54 PM

Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 7,676
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Posted 07 June 2009 - 08:25 PM

I am not sure what you are asking but I believe both of your equations for conversion are wrong. You also have to take into account that 5/9 won't return .55555 etc, it will return 0 because you are doing integer division. Make sure you convert them to doubles and use doubles throughout your equation. If you want it to return int, cast it to an int at the end.

return (int)(((9.0 / 5.0) * (double)temp) + 32.0);

and 

return (int)((5.0 / 9.0) * ((double)temp - 32.0));



Lastly you will need to take a look at the printing at the end and watch which labels you are printing because they are backwards. Don't switch the "to" and "from" variable labels because you are then not switching what variables you print.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#3 gmillerlight  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 26
  • Joined: 30-April 09


Dream Kudos: 0

Posted 08 June 2009 - 04:39 AM

Quote

I have made the changes that were suggested, but it is still not asking me for a temperature to convert. However the enter 3 to exit works just fine. I included the Run output so you can see what I mean. I did enter 59 but it didn't do anything.


package celsiusfahrenheitconverter;
import java.util.Scanner;

/** This java code reads
 *  Fahrenheit temperature the
 * user enters and displays back
 * temperature in Celsius. This also
 * Reads the Celsius temperature
 * entered and displays back
 * Fahrenheit temperature.
 *
 * @author 
 */
public class CelsiusFahrenheitConverter
{

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args)
	{
		  // declare variables
		String from;
		String to;
		String temp;
		int choice;
		int fahrenheit;
		int celsius;

		Scanner input = new Scanner(System.in);
	   
		do
		{
			choice = -1;
			System.out.println("Convert temperature:");
			System.out.println(" Convert from Fahrenheit to Celsius:");
			System.out.println(" Convert from Celsius to Fahrenheit:");
			System.out.println("Enter 3 - Exit:");
			choice = input.nextInt();

		switch (choice)
		{
			// case 1 asks user for a Fahrenheit temp to convert to Celsius
			// case 2 asks user for a Celsius temp to convert to Fahrenheit
		   case 1:
				 
				System.out.println("Enter the temperature in Fahrenheit:");
				fahrenheit = input.nextInt();
				celsius = convertToCelsius(fahrenheit);
				from = "Celsius";
				to = "Fahrenheit";
				break;

		   case 2:
				 
				System.out.println("Enter the temperature in Celsius:");
				celsius = input.nextInt();
				fahrenheit = convertToFahrenheit(celsius);
				from = "Fahrenheit";
				to = "Celsius";
				break;
			  default:
					continue; // no input or output
		}

				 System.out.print("\n");
				 System.out.print(fahrenheit);
			  
				 System.out.print(from);
				 System.out.print("=");
				 System.out.print(celsius);
			   
				 System.out.print(to);
				 System.out.print('\n');
		}
			while (choice != 3);
	}

			/**
	 * Converts form Celsius to
	 * Fahrenheit
	 * @param temp The temperature
	 * in Celsius
	 * @return The temperature in
	 * Fahrenheit
	 */

	public static int convertToFahrenheit(int temp)
	{
		return (int)(((9.0 / 5.0) * (double)temp) + 32.0);


	}

	/**
	 * Converts from Fahrenheit
	 * to Celsius
	 * @param temp the temperature
	 * in Fahrenheit
	 * @return the temperature in Celsius
	 */

	 public static int convertToCelsius(int temp)
	 {
		 return (int)((5.0 / 9.0) * ((double)temp - 32.0));

	 }

}
/*run:
Convert tmperature:
 Convert from Fahrenheit to Celsius:
 Convert from Celsius to Fahrenheit:
Enter 3 - Exit:
59
Convert tmperature:
 Convert from Fahrenheit to Celsius:
 Convert from Celsius to Fahrenheit:
Enter 3 - Exit:
59
Convert tmperature:
 Convert from Fahrenheit to Celsius:
 Convert from Celsius to Fahrenheit:
Enter 3 - Exit:
3
BUILD SUCCESSFUL (total time: 1 minute 12 seconds)
*/


	

This post has been edited by gmillerlight: 08 June 2009 - 04:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 computerfox  Icon User is offline

  • straight vegetarian kid
  • PipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 3,772
  • Joined: 29-January 09


Dream Kudos: 1700

Posted 08 June 2009 - 05:29 AM

i'm guessing it's okay now :unsure:
Was This Post Helpful? 0
  • +
  • -

#5 gmillerlight  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 26
  • Joined: 30-April 09


Dream Kudos: 0

Posted 08 June 2009 - 05:45 AM

Quote

No this is not working correctly yet. The exit is working but it is not doing anything the temperature to convert to either celsius or fahrenheit. Please help I'm a beginner and really not sure what I did wrong.

Was This Post Helpful? 0
  • +
  • -

#6 computerfox  Icon User is offline

  • straight vegetarian kid
  • PipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 3,772
  • Joined: 29-January 09


Dream Kudos: 1700

Posted 08 June 2009 - 05:51 AM

okay i was gonna do this earlier, but i wasn't sure of something. click here now it's in C++, but it's set up relatively easy to convert. just make a new variable and then return the new variable

for example if you change the cout<< to return then it would work. hope that helps :)


PLEASE READ THE INSTRUCTION IN THIS POST CAREFULLY

This post has been edited by computerfox: 08 June 2009 - 07:55 AM

Was This Post Helpful? 1
  • +
  • -

#7 computerfox  Icon User is offline

  • straight vegetarian kid
  • PipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 3,772
  • Joined: 29-January 09


Dream Kudos: 1700

Posted 08 June 2009 - 08:11 AM

*facepalm* i'll help you out dude...


public double convertTemp(double temp,char from,char to)
{
		  from=from.toUpperCase();
		  to=to.toUpperCase();
		  int answer1=from.compareTo("C");
		  int answer2=to.compareTo("F");

	  //from c to f
		  if(answer1==0&&answer2==0)
	  {
		  return (((temp/5)*9)+32);
	  }
	  
	  //from f to c
		  answer1=from.compareTo("F");
		  answer2=to.compareTo("C");
	  if(answer1==0&&answer2==0)
	  {

		   return (((temp-32)*5)/9);
	  }
	  
	
}



hope that helps :)
Was This Post Helpful? 1
  • +
  • -



Fast Reply

  

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month