Results 1 to 7 of 7
- 09-03-2012, 08:04 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
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
Additional Notes
- 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:
Java 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; } }Last edited by niloufar; 09-03-2012 at 08:13 AM.
- 09-03-2012, 03:39 PM #2
Java Rookie
- Join Date
- Aug 2012
- Posts
- 20
- Rep Power
- 0
Re: A method that performs the Fahrenheit-to-Celsius conversion
So... What is your question?
- 09-03-2012, 04:34 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Re: A method that performs the Fahrenheit-to-Celsius conversion
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 #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 09-03-2012, 05:14 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
- 09-03-2012, 05:25 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 09-03-2012, 06:31 PM #7
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
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!
Java 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; } }
Similar Threads
-
Fahrenheit to Celsius help please
By Moka in forum New To JavaReplies: 23Last Post: 03-20-2011, 01:20 AM -
Celsius to Fahrenheit
By Danieldcc in forum New To JavaReplies: 2Last Post: 09-30-2010, 04:50 AM -
Help with fahrenheit to celsius conversion
By java_is_killingme in forum New To JavaReplies: 4Last Post: 03-07-2009, 11:11 AM -
Fahrenheit to celsius
By lalithalydia in forum New To JavaReplies: 3Last Post: 02-05-2008, 07:50 AM -
Converts from Fahrenheit to Celsius
By trill in forum New To JavaReplies: 1Last Post: 08-06-2007, 05:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks