# A method that performs the Fahrenheit-to-Celsius conversion

• 09-03-2012, 08:04 AM
niloufar
A method that performs the Fahrenheit-to-Celsius conversion
Hi i have wrote java for my homework, but wanna make sure everything is exactly as my professor requested!
here is his requirements:

- Define a constant for the decimal value used in the meters conversion equation (2 points)
- Define a constant for the decimal value used in the kilograms conversion equation (2 points)
- Define a method that performs the Fahrenheit-to-Celsius conversion (6 points)
o The method should receive the Fahrenheit value as an input
o The method should return the converted Celsius value
o Update your program code to call the method

- Use only one variable for each value (there are 6 variables in this project)
- Assume the user enters valid data (don't worry about verifying the input values for errors)
- Do not use methods (all statements must be located in the main() method)
- Ensure your source code conforms to the commenting standards for the class

AND here is my Codes:

Code:

```import java.util.Scanner; public class UnitConversionTool {                 /**         * Main method         * @param args         */ public static void main (String[] args ) {                                 /**                 * Create a Scanner                 */                 Scanner input = new Scanner(System.in);                                                         System.out.println("\nAuthor: ");                 System.out.println("Project 1 – Unit Conversion Tool");                                 /**                 * Coverts from fahrenheit                 */                 System.out.print("\nEnter a Fahrenheit temperature: ");                 int Fahrenheit = input.nextInt();                 final double Celsius = FahrenheitToCelsius(Fahrenheit);                                 /**                 * Converts from Feet to meters                 */                 System.out.print("\nEnter a distance in feet: ");                 int Distance = input.nextInt();                 final double Meters = (Distance * 0.3048 );                                 /**                 * Converts from Pound to Kilogram                 */                 System.out.print("\nEnter a weight in pounds: ");                 int Weight = input.nextInt();                 final double Kilograms = (Weight * 0.4536);                                        /**                 * Print the result                 */                 System.out.println(Fahrenheit +" Fahrenheit is  " +Celsius + " Celsius");                 System.out.println("\n"+Distance +" Distance is "+ Meters+ " Meters");                 System.out.println(Weight + " Weight is "+ Kilograms+ " Kilograms");                                                 }                         /**         * Converts from Fahrenheit to Celsius         * @param temp the temperature in Fahrenheit         * @return the temperature in Celsius         */                 public static double FahrenheitToCelsius(double temp)                 {                         return (temp - 32 )*5/9;                                         }         }```
• 09-03-2012, 03:39 PM
bob 6
Re: A method that performs the Fahrenheit-to-Celsius conversion
• 09-03-2012, 04:34 PM
niloufar
Re: A method that performs the Fahrenheit-to-Celsius conversion
Quote:

Originally Posted by bob 6

As I said, i'm pretty new to JAVA and need to know if I did all my professor requirements right? His requirements is foreign language to me and not sure if i did what he wanted!!!

I just needed somebody to check my codes with his requirement to make sure i did everything he needed?!!! """ of course to receive full grade :D ""
• 09-03-2012, 05:10 PM
Tolls
Re: A method that performs the Fahrenheit-to-Celsius conversion
You have no constants.

Other than that, does it work?
That's my usual criteria for successful coding.
• 09-03-2012, 05:14 PM
niloufar
Re: A method that performs the Fahrenheit-to-Celsius conversion
Quote:

Originally Posted by Tolls
You have no constants.

Other than that, does it work?
That's my usual criteria for successful coding.

Yes thanx , it's working....
this doesn't consider as constant? : final double Celsius = FahrenheitToCelsius(Fahrenheit);
or
final double Meters = (Distance * 0.3048 ); ?
• 09-03-2012, 05:25 PM
Tolls
Re: A method that performs the Fahrenheit-to-Celsius conversion
That 0.3048 is what is known in the industry as a Magic Number.
A defined constant (as intended by your task) would be a named attribute that represents that number, which you would then use in place of the number.
• 09-03-2012, 06:31 PM
niloufar
Re: A method that performs the Fahrenheit-to-Celsius conversion
Thank you for all your help; please check my codes and let me know what you think and if the constants are defined correctly!

Thank you agian!

Code:

```import java.util.Scanner; public class UnitConversionTool {                 /**         * Constant Declaration         */         public static final double Meter_To_Feet = 0.3048; // a constant for the decimal value used in the meters         public static final double Kilogram_TO_Pound = 0.4536; // a constant for the decimal value used in the kilograms                         /**         * Main method         * @param args         */ public static void main (String[] args ) {         Scanner input = new Scanner(System.in);  // Create a Scanner                                                         System.out.println("\nAuthor: ");                 System.out.println("Project 1 – Unit Conversion Tool");                                 /**                 * Converts from fahrenheit                 */                 System.out.print("\nEnter a Fahrenheit temperature: ");                 int Fahrenheit = input.nextInt();                 double Celsius = FahrenheitToCelsius(Fahrenheit);  //invoke FahrenheitToCelsius method                                 /**                 * Converts from Feet to meters                 */                 System.out.print("\nEnter a distance in feet: ");                 int Distance = input.nextInt();                 double Meters = (Distance * Meter_To_Feet );                                 /**                 * Converts from Pound to Kilogram                 */                 System.out.print("\nEnter a weight in pounds: ");                 int Weight = input.nextInt();                 double Kilograms = (Weight * Kilogram_TO_Pound);                                        /**                 * Print the result                 */                 System.out.println("\n"+Fahrenheit +" Fahrenheit is  " +Celsius + " Celsius");                 System.out.println(Distance +" Distance is "+ Meters+ " Meters");                 System.out.println(Weight + " Weight is "+ Kilograms+ " Kilograms");                                         input.close(); // Close Scanner                                }                         /**         * Converts from Fahrenheit to Celsius         * @param temp the temperature in Fahrenheit         * @return the temperature in Celsius         */                 public static int FahrenheitToCelsius(int temp)                 {                         return (temp - 32 )*5/9;                                         }         }```