Results 1 to 7 of 7
  1. #1
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Question 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.

  2. #2
    bob 6 is offline Java Rookie
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Default Re: A method that performs the Fahrenheit-to-Celsius conversion

    So... What is your question?

  3. #3
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Red face Re: A method that performs the Fahrenheit-to-Celsius conversion

    Quote Originally Posted by bob 6 View Post
    So... What is your question?
    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 ""

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default 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.

    ** This space for rent **

  5. #5
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: A method that performs the Fahrenheit-to-Celsius conversion

    Quote Originally Posted by Tolls View Post
    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 ); ?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default 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.

    ** This space for rent **

  7. #7
    niloufar is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Talking 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

  1. Fahrenheit to Celsius help please
    By Moka in forum New To Java
    Replies: 23
    Last Post: 03-20-2011, 01:20 AM
  2. Celsius to Fahrenheit
    By Danieldcc in forum New To Java
    Replies: 2
    Last Post: 09-30-2010, 04:50 AM
  3. Help with fahrenheit to celsius conversion
    By java_is_killingme in forum New To Java
    Replies: 4
    Last Post: 03-07-2009, 11:11 AM
  4. Fahrenheit to celsius
    By lalithalydia in forum New To Java
    Replies: 3
    Last Post: 02-05-2008, 07:50 AM
  5. Converts from Fahrenheit to Celsius
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •