473,605 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert Fahrenheit to Celsius: Get correct Output

7 New Member
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.777777777777 78

(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
4 1671
Nepomuk
3,112 Recognized Expert Specialist
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
raitoHiong
7 New Member
Sorry for the inconvenience. i edit it alreaddy.....My output for the temperature in C is -17.777777777777 78 (wrong).... i want to get The temperature in C is 100
Nov 8 '13 #3
Nepomuk
3,112 Recognized Expert Specialist
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
raitoHiong
7 New Member
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
4309
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
2093
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 memo field. I have located Stephen Lebans code to limit enter of text field based on characters entered and code to limit text based upon width of a text box (this seems to be most applicable for single line text box). I am have trouble putting...
13
2194
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 for help! #include <stdio.h> #include <stdlib.h> #define __buff(s, c) s_c
2
2370
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 shows it correctly with seconds and milliseconds in the output like this 2008-01-30 13:58:04.000 But when i run the sql through PHP 5.2.5 it replaces the seconds and milliseconds with AM/PM sql1="select time from tickets where ticket_id = '1'...
0
6710
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 Celsius Temperature. ///////////////////////////////// package developer;
0
1223
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 solve it. I am a newbie any hint/help will be highly appreciated.I have used CoffeeCup Visual Site Designer. code is as follows <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head>...
1
2277
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 system audit. i had got csv files from windows wmic script.. which is uploadeded below (one of patches and other is disk info).both are in csv format but changed extension to .text for uploading here. my task to combine these two csv files into...
3
1621
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 file. main file looks like Scanner scanner = new Scanner(System.in); boolean end = false; // is the input name stop? while (end == false) // as long as end is false, proceed { ...
0
8004
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7934
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8418
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.