473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

convert Fahrenheit to Celsius: Get correct Output

i want to get correct output:

Expand|Select|Wrap|Line Numbers
  1. public class Temperature
  2. {
  3.    private String myScale; //valid values are "F" or "C"
  4.    private double myDegrees;
  5.    double celsius;
  6.  
  7.    //default constructor
  8.    public Temperature()
  9.    { 
  10.       myScale = "";
  11.       myDegrees = 0.0;
  12.    }
  13.  
  14.    //constructor with specified degrees and scale
  15.    public Temperature(double degrees, String scale)
  16.    {
  17.       myDegrees = degrees;
  18.       myScale = scale;
  19.    }
  20.  
  21.    //a method to convert Fahrenheit to Celsius
  22.  
  23.    public Temperature toCelsius()
  24.    {
  25.  
  26.       Temperature tempCelsius;
  27.       celsius = ((myDegrees - 32) * 5) / 9; 
  28.       tempCelsius = new Temperature(celsius, "C");
  29.       return tempCelsius;
  30.  
  31.    }
  32.    // a method to display the temperature
  33.    public void displayTemperature()
  34.    {
  35.    System.out.println("The temperature in C is " + celsius);
  36.    }
  37. }
  38.  
  39. import java.util.Scanner;
  40.  
  41. public class TemperatureDemo
  42. {
  43.    public static void main (String [] args)
  44.    {
  45.       Temperature tempCelsius;
  46.       Scanner input = new Scanner(System.in);
  47.       String scale = ""; double degree;
  48.       System.out.print("Enter temperature scale: ");
  49.       scale = input.next();
  50.       System.out.print("Enter number of degrees: ");
  51.       degree = input.nextDouble();
  52.       //Create a Temperature object based on user input
  53.       Temperature Answer = new Temperature();
  54.       //call toCelsius() method
  55.       Answer.toCelsius();
  56.  
  57.       //call displayTemperature() method
  58.       Answer.displayTemperature();
  59.  
  60.    }
  61. }
  62.  
Output:
Enter temperature scale: F
Enter number of degrees: 212
The temperature in C is -17.77777777777778

(My output of temperature in C should be 100)

Any help and response will be appreciate.

Regards!
Attached Files
File Type: docx exercise.docx (16.3 KB, 379 views)
Nov 8 '13 #1

✓ answered by Nepomuk

You're not passing the values you read to the Temparature object; replace what is displayed as line 53 here with
Expand|Select|Wrap|Line Numbers
  1. Temperature Answer = new Temperature(degree, scale);
and it should work.

4 1663
Nepomuk
3,112 Expert 2GB
OK, we know what you want to get. What are you getting? What's working and what isn't?

Also, please use [code]...[/code] tags when posting code. It makes it much easier to read.
Nov 8 '13 #2
Sorry for the inconvenience. i edit it alreaddy.....My output for the temperature in C is -17.77777777777778 (wrong).... i want to get The temperature in C is 100
Nov 8 '13 #3
Nepomuk
3,112 Expert 2GB
You're not passing the values you read to the Temparature object; replace what is displayed as line 53 here with
Expand|Select|Wrap|Line Numbers
  1. Temperature Answer = new Temperature(degree, scale);
and it should work.
Nov 8 '13 #4
It work!

Thanks you very much, Nepomuk
Nov 8 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: hnzhang | last post by:
int main() { char *test = "hello"; std::cout << *test << std::endl; return 0; } the result of out put is h; only the first character. But what I want
3
by: pv1 | last post by:
Hello, I am somewhat new to Access and vba, but am trying to complete a request that has been made of me. Access version 2002 WindowsXP. At this point these text boxes are bound to the table...
13
by: Carramba | last post by:
Hi! I have written some peace of code, but I wonder if it's legal for ansi-c I have no problem to compiling it, but since I'm inexperience and the output is not correct I have doubts. Thank you...
2
by: pks83 | last post by:
In MSSQL I am using datetime as the column type and trying to execute the below mentioned query select time from tickets where ticket_id = '1'; when i execute the query on sql manager window it...
0
by: dfluker | last post by:
The Java code for this temperature conversion from Fahrenheit to Celsius is given below. the code starts by asking the user to enter a temperature in Fahrenheit scale and then displays the equivalent...
0
by: Masterstill | last post by:
I've faced some problems while designing a page. the below codes look perfect on 800 * 600 browser but higher resolution is not supported. I've tried using % instead of px. but still i could not...
1
by: certteam | last post by:
hai everybody first of all thank u for allowing me to join this forum. i m a newbie to perl script but had done some projects with windows batch.. my new project is to create a output of a pc...
3
by: rwgwiccan | last post by:
Have a program that is to take a persons name, hours worked, rate of pay, fed and state taxes, and combine them to get a net pay. However, most of the calculations will be done in a separate class...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.