Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Write a Java class to represent a temperature

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I'm looking for some insight on the following question (blue text)...

Write a Java class to represent a temperature. The class has a single data field:
• temperatureC
The class has a single constructor that accepts an initial temperature provided as a double argument. If this
argument is < -273.15, set it to -273.15.
The class has the following methods:
• setC – this method takes a single double argument and changes the value of the data field accordingly.
Like the constructor his method should prevent the temperature from being less than -273.15.
• getC – this method returns the current value of the data field.
• getF – this method computes and returns the value of temperatureC in Fahrenheit.
o C ×1.8 + 32 = F
• getK – this method computes and returns the value of temperatureC in Kelvin.
o C + 273.15 = K
• ***Write JavaDoc comments for this class.***
Write a tester class that performs the following.
• Prompt the user and collect a double value. Use this value to create a Temperature object.
• Show the initial temperature in C, F and K.
• Prompt the user for another value and use this value to change the temperature of your existing object.
Again, show the new values of C, F and K.



The work I have done so far is as follows, I'm not sure what I have done wrong, but it won't run, plus in the code (quote below)

Temperature temp = new Temperature(setC);
System.out.print("The current temperature in Celsius is: " + temp.getC());
System.out.print("The current temperature in Fahrenheit is: " + temp.getF());
System.out.print("The current temperature in Kelvin is: " + temp.getK());


has errors in it, and I can't seem to figure it out.

The code that I have written so far:



Any advice would be appreciated.
 
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What errors?
 
Marshal
Posts: 76479
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you got the right formula for °K?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could be wrong, but it looks like the first problem is you are creating an instance of the "Temperature" class instead of the "temperatureC" class.

From your code:



So the Class temperatureC isn't actually used.
 
Bear Bibeault
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please follow naming conventions: class names should start with an uppercase letter.
 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Have you got the right formula for °K?

Campbell Ritchie


Thank you, I corrected that.

I could be wrong, but it looks like the first problem is you are creating an instance of the "Temperature" class instead of the "temperatureC" class.
From your code:
Temperature temp = new Temperature(setC);
So the Class temperatureC isn't actually used.

Braden Olson


That seems to be the problem, because it appears to work fine now, thank you...pretty impressive for a "greenhorn"....
 
Braden Olson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karen Barlow wrote:
That seems to be the problem, because it appears to work fine now, thank you...pretty impressive for a "greenhorn"....



*High Five*
 
Campbell Ritchie
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK.
I'm a bit of a stickler for following the requirements.
So:
"
Write a Java class to represent a temperature. The class has a single data field:
• temperatureC
"

You've called your class temperatureC...and your attribute setC.
setC is the method, not the attribute. And you are, in fact, missing that method entirely.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Begin with:By the way, the class you are currently using that is named Temperature should probably be named TemperatureTester ot Tester (if you like shorter names).
 
Campbell Ritchie
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and I know you won't need this until later, but what are you doing when somebody enters a temperature lower than absolute zero?
By the way: make absolute zero a public constant field rather than using the value -273.15 repeatedly.
 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
+1! -273.15 is a magic number.
 
and POOF! You're gone! But look, this tiny ad is still here:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic