The Artima Developer Community
Sponsored Link

Java Answers Forum
Temperature Converter

1 reply on 1 page. Most recent reply: Aug 10, 2005 10:42 PM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Chris

Posts: 5
Nickname: jarvio678
Registered: Aug, 2005

Temperature Converter Posted: Aug 10, 2005 9:33 AM
Reply to this message Reply
Advertisement
Hi again.

I've worked on doing an application to convert celsius to fahrenheit and visa-versa but it's not compiling and i can't seem to solve the errors. If anyone has a chance to take a look here's the code:
It comes in 2 parts.. 1 class with the equation and 1 class to control the user input:
1st Class:

import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.util.*;
 
public class Equation {
 
public static float convertF(float temp)
 
{ fahrenheit = (centigrade*1.8f) + 32; }
 
public static float convertC(float temp)
 
{ centigrade = (fahrenheit/32)/1.8f; }
		}

2nd Class:

import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.util.*;
 
public class Temperature {
 
String input = JOptionPane.showInputDialog(null, "Enter which temperature you wish to convert using C or F:");
	String temp = String.parseString(input);
{
 
if(temp.equals("C"));
 
String input1 = JOptionPane.showInputDialog(null, "Enter Temperature in Centigrade to convert:");
float centigrade = Equation.convertF(input1);
 
{
 
if(temp.equals("F"));
 
String input2 = JOptionPane.showInputDialog(null, "Enter Temperature in Fahrenheit to convert:");
float fahrenheit = Equation.convertC(input2);
	  }
		 }
}


Thanks if anyone can spot my errors.

Chris.


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Temperature Converter Posted: Aug 10, 2005 10:42 PM
Reply to this message Reply
> Hi again.
>
> I've worked on doing an application to convert celsius to
> fahrenheit and visa-versa but it's not compiling and i
> can't seem to solve the errors. If anyone has a chance to
> take a look here's the code:
> It comes in 2 parts.. 1 class with the equation and 1
> class to control the user input:
> 1st Class:
>
>
> import javax.swing.*;
> import java.lang.*;
> import java.io.*;
> import java.util.*;
> 
> public class Equation {
> 
> public static float convertF(float temp)
> 
> { fahrenheit = (centigrade*1.8f) + 32; }
> 
> public static float convertC(float temp)
> 
> { centigrade = (fahrenheit/32)/1.8f; }
> 		}
> 

> 2nd Class:
>
>
> import javax.swing.*;
> import java.lang.*;
> import java.io.*;
> import java.util.*;
> 
> public class Temperature {
> 
> String input = JOptionPane.showInputDialog(null, "Enter
> which temperature you wish to convert using C or F:");
> 	String temp = String.parseString(input);
> {
> 
> if(temp.equals("C"));
> 
> String input1 = JOptionPane.showInputDialog(null, "Enter
> Temperature in Centigrade to convert:");
> float centigrade = Equation.convertF(input1);
> 
> {
> 
> if(temp.equals("F"));
> 
> String input2 = JOptionPane.showInputDialog(null, "Enter
> Temperature in Fahrenheit to convert:");
> float fahrenheit = Equation.convertC(input2);
> 	  }
> 		 }
> }
> 

>
> Thanks if anyone can spot my errors.
>
> Chris.

Where do I begin?? The whole program is flawed.

Starting with your method:


public static float convertF(float temp)

The signature is public (meaning it is accessible
from any other class), static means the method can
only be called as follows:

ClassName.convertF(some_value);

float means it *MUST* return *SOMETHING* your method
returns nothing.

Here is an example:
public static float convertF(float value_I_want_to_convert){
   float faranheit = 0.0;
   faranheit = (do_your_calculation_here);
   //  return the value you have converted
   return farenheit;
}

When you want to retrieve this value you make the
following call if the name of the Class containing
the method is "MyClass":

MyClass.convertF(value_to_convert);

There is no such a String method:

String temp = String.parseString(input);

I assume what you wanted was:

// note the use of Upper and Lower cases
float temp = Float.parseFloat(input);

// Primitive float temp = Object Float.parse blah blah

Your friendly Java reference.

http://java.sun.com/j2se/1.5.0/docs/api/

Go through the classes in the above API.

Go through Float. Note that the signature for the
method parseFloat is static which is why we call
it by: "Float.parseFloat(String)"

As opposed to:

// INCORRECT
Float fizzo - new Float();
fizzo.parseFloat();

You have a lot of work ahead. Go through a Java
Tutorial with regards to Classes and Methods
and refer to the Java API for all classes that
come as part of the standard Java Library.

There are a whole bunch of other errors such as

faranheigh *MUST* be declared in some capacity
(global or local) before its used:

// this is effortless
float faranheit = 0.0;

Flat View: This topic has 1 reply on 1 page
Topic: add one Treemap to another, etc... Previous Topic   Next Topic Topic: visibility issue

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use