Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: help w/ conversion of degrees(CtoF, FtoC)

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default help w/ conversion of degrees(CtoF, FtoC)

    here is what we're supposed to do
    This assignment is a Celsius to Fahrenheit and Fahrenheit to Celsius temperature conversion program.

    Write a class that contains the following two methods ( class will have a total of 3 methods ):
    public static double celtofahr ( double celsius)
    and public static double fahrtocel ( double fahrenheit )

    main should prompt the user for a celsius, call / use the celtofahr method to do the conversion, next display the result to the user. Next, prompt the user for a Fahrenheit, call / use the fahrtocel method to do the conversion, next display the result to the user.

    Note : the formula for conversion is
    fahrenheit = ( ( 9.0 / 5.0 ) * celsius ) + 32

    WHERE DO I PUT THE STRING TO PROMPT THE USER TO ENTER EACH DEGREE?

    here is my code

    public class Assign8_Roberts{
     
    	private double fahrenheit;
    	private double celsius;
    	private String displayString;
     
    	public Assign8_Roberts (double Fahrenheit, double Celsius)
    	{
    		this.fahrenheit = Fahrenheit;
    		this.celsius = Celsius;
    	}
     
    	public double GetFahrenheitTemp()
    	{
    		return this.fahrenheit;
    	}
     
    	public double FahrtoCel(double fahrenheit)
    	{
    		double result = (fahrenheit - 32)/1.8;
    		return result;
    	}
     
    	public double GetCelsiusTemp()
    	{
    		return this.celsius;
    	}
     
    	public void FahrenheitTemp(double fahrenheitVal)
    	{
    		this.fahrenheit = fahrenheitVal;
    	}
     
    	public double CeltoFahr(double celsius)
    	{
    		double result = (1.8 * celsius) + 32;
    		return result;
    	}
     
    	public void CelsiusTemp(double celsiusVal)
    	{
    	   this.celsius  = celsiusVal;
    	}
     
    }


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: help w/ conversion of degrees(CtoF, FtoC)

    you need a main class to execute and get user input

    add the following under your Assign8_Roberts constructor
    void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a value: ");

    float f = scan.nextFloat();

    //use f as input to your methods
    }

    Hope it helps

  3. The Following User Says Thank You to retsameht For This Useful Post:

    javapenguin (November 7th, 2010)

  4. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: help w/ conversion of degrees(CtoF, FtoC)

    WHERE DO I PUT THE STRING TO PROMPT THE USER TO ENTER EACH DEGREE?
    Don't shout.

    The assignment tells you.
    main should prompt the user for a celsius, ... Next, prompt the user for a Fahrenheit
    db

  5. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    javapenguin (November 7th, 2010)

  6. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: help w/ conversion of degrees(CtoF, FtoC)


  7. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help w/ conversion of degrees(CtoF, FtoC)

    Quote Originally Posted by Darryl.Burke View Post
    PLEASE STOP THIS. SOMETIMES PEOPLE DO THIS JUST BECAUSE THEY HAVE DEADLINES AND DAYS GO BY WITH NO RESPONSE. I'VE BEEN THERE BEFORE.

  8. #6
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ conversion of degrees(CtoF, FtoC)

    here's my updated program..when i run it, it runs previous assignment that i've done instead of this one so something isnt right

    import java.util.Scanner ; 
     
    public class Assign8_Roberts{
     
     
    private double fahrenheit;
    private double celsius;
    private String displayString;
     
    public Assign8_Roberts (double Fahrenheit, double Celsius)
    {
    this.fahrenheit = Fahrenheit;
    this.celsius = Celsius;
    }
     
    public double GetFahrenheitTemp()
    {
    return this.fahrenheit;
    }
     
    public double FahrtoCel(double fahrenheit)
    {
    double result = (fahrenheit - 32)/1.8;
    return result;
    }
     
    public double GetCelsiusTemp()
    {
    return this.celsius;
    }
     
    public void CelsiusTemp(double celsiusVal)
    {
    this.celsius = celsiusVal;
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a value: ");
     
    float c = scan.nextFloat();
     
    //use c as input to your methods
    }
    }
    public void FahrenheitTemp(double fahrenheitVal)
    {
    this.fahrenheit = fahrenheitVal;
     
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a value: ");
     
    float f = scan.nextFloat();
     
    //use f as input to your methods
    }
     
    }
     
    public double CeltoFahr(double celsius)
    {
    double result = (1.8 * celsius) + 32;
    return result;
    }
     
     
     
    }
    Last edited by robertsbd; November 7th, 2010 at 11:18 PM.

  9. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help w/ conversion of degrees(CtoF, FtoC)

    Quote Originally Posted by robertsbd View Post
    To DB(aka java forum police):does it really matter if i posted it on another board..isnt that what these are for??
    Yes it does matter, especially when doing so violates the policy's of the very forum's those posts reside. The policies of certain forums were created for a reason.

    While 'cross posting' is not against the policy of these forums, we recommend posting links to the other forums where the question has been asked so responders, who do this without pay and in their spare time, do not spend their time answering a question which has already been answered elsewhere.

    Quote Originally Posted by robertsbd View Post
    here's my updated program..when i run it, it runs previous assignment that i've done instead of this one so something isnt right
    Please describe what you mean by 'it runs the previous assignment...instead of this'. What input did you provide, and what output did you receive? This information is important information to help you
    Last edited by copeg; November 7th, 2010 at 10:28 PM.

  10. #8
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ conversion of degrees(CtoF, FtoC)

    ok sorry about my previous rant..i apologize
    but when i run the program, the output is the same as the output of another assignment on my workspace

  11. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help w/ conversion of degrees(CtoF, FtoC)

    Are you using Eclipse? Sounds more like an IDE issue that happens when you have more than one main function. In eclipse (if you are using it) go to to Run->Run Configuration and select the appropriate main to run.

  12. #10
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ conversion of degrees(CtoF, FtoC)

    yeah im using eclipse, ill give it a try when i get home..i just upgraded from vista to 7 so idk if that has anything to do w/ it

  13. #11
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ conversion of degrees(CtoF, FtoC)

    ok i've changed my program around and while im getting the correct results, im not sure if i have it designed the way the assignment says

    public class Assign8_Roberts{
     
      public static void main(String[] args)
      {
     
        double fahrenheit;
        double celsius;
     
        Scanner input = new Scanner(System.in);
     
        System.out.println("Enter the Degrees in Fahrenheit");
        fahrenheit = input.nextDouble();
        celsius = (5.0/9.0)*(fahrenheit - 32);
        System.out.println("The number of degrees of Fahrenheit: " + fahrenheit);
        System.out.println("Converted to Celsius is: " + celsius);//end convert to celsius
     
        System.out.println("Enter the Degrees in Celsius");
        celsius = input.nextDouble();
        fahrenheit = (9.0/5.0)* celsius + 32;
        System.out.println("The number of degrees of Celsius: " + celsius);
        System.out.println("Converted to Fahrenheit is: " + fahrenheit);
      }
     
    }

  14. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: help w/ conversion of degrees(CtoF, FtoC)

    Quote Originally Posted by robertsbd View Post
    ok i've changed my program around and while im getting the correct results, im not sure if i have it designed the way the assignment says
    The assignment mentions you need 3 methods, one of which is main. So I'd suggest pulling out those C->F and F->C calculations into their own methods (named as per the assignment guidelines).

  15. #13
    Member
    Join Date
    Oct 2010
    Posts
    39
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help w/ conversion of degrees(CtoF, FtoC)

    i think im more in the ballpark in what the assignment says..my errors are coming in the output

    import java.util.Scanner;
     
    public class Assign8_Roberts{
     
    //Converts between CandF
    private double celsius;
    private double fahrenheit; 
     
    //converts from CtoF
    public void CeltoFahr(double temp){
    fahrenheit = (9.0/5.0*temp) + 32;
    celsius = temp;
    }
     
    //converts from FtoC
    public void FahrtoCel(double temp){
    celsius = (5.0/9.0) * (temp-32);
    fahrenheit = temp;
    }
     
    //displays temp in C
    public void outputCelsius(){
    System.out.println("The temperature is " + celsius + "C");
    }
     
    //displays temp in F
    public void outputFahrenheit(){
    System.out.println("The temperature is " + fahrenheit + "F");
    }
     
    //output
    public static void main(String[] args) {
    Scanner temp = new Scanner(system.in);
    temp t = new temp();
    t.setCelsius(100);
    t.outputFahrenheit();
    t.outputCelsius();
    }
     
    }
    Last edited by robertsbd; November 8th, 2010 at 01:54 PM.

Similar Threads

  1. Base Conversion
    By Pingu in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 22nd, 2011, 03:15 PM
  2. JAVA to C++ conversion
    By hackerboy101 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 28th, 2010, 08:31 PM
  3. Explicit Conversion?
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 11th, 2009, 11:40 PM
  4. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 04:29 AM
  5. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM