• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

confused

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused on an assignment i am working on. I got the first half of the assignment done, the next part is

1: Add a set method with two parameters (degrees and scale) to allow set Temperature object's degreesInKelvin by passing in a temperature and a corresponding scale. For example,
public void set(double temperature, String scale)
temp1.set(100, "C") means set temp1's degreesInKelvin to the equivalent of 100° Celsius
temp1.set(35, "F") means set temp1's degreesInKelvin to the equivalent of 35° Fahrenheit
temp1.set(50, "K") means set temp1's degreesInKelvin to 50° Kelvin

2: Add a Boolean method to compare two Temperature objects for equality plus or minus the margin of error
public Boolean isNearlyEqual(Temperature t2)
Remember when comparing doubles to allow for a margin of error because of approximation errors.
Use a named constant to set the margin of error at 0.001

the code so far is


help please
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jamie keene wrote:


Try describing the steps in English before Java. What is the process you want to follow in this method?

If scale is "C" then...
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't know what its asking for or what to do
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jamie keene wrote:...1: Add a set method with two parameters (degrees and scale) to allow set Temperature object's degreesInKelvin by passing in a temperature and a corresponding scale. For example,
public void set(double temperature, String scale)
temp1.set(100, "C") means set temp1's degreesInKelvin to the equivalent of 100° Celsius...


Your class already has methods that will perform the conversions and set the variable: setDegreesFromCelsius, setDegreesFromFahrenheit, and setDegreesFromKelvin.

The new method you are writing should call one of these existing methods based on what scale is given ("C", "F", or "K").

So if scale is "C" then what do you want to do with the temperature value?
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does that mean


?
 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a private variable that represents the degrees in Kelvin. Someone gives you the new value for this variable, but the scale may be different. So what do you need to set the variable to:
- if the scale is Kelvin (scale.equals("K"))
- if the scale is Fahrenheit (scale.equals("F"))
- if the scale is Celsius (scale.equals("C"))
- any other scale

In the first three cases, you can just delegate the call to one of your other methods.

Note that I used scale.equals("K"). That's the way to compare Strings. Using == you will compare on Object reference equality, not content equality.
 
Marshal
Posts: 76415
365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unrelated: You have a mysterious number "273.15" appearing several times. You ought not to have such "magic numbers". Replace it by a constant, which can probably be public, and give it a suitable name, eg TAX_RATE or FREEZING_POINT_WATER, whichever is more appropriate. Then you can say things likeNow everybody knows that you have a tax rate to subtract from the temperature, and if you use the javadoc tool, you get an html document which tells you the value of the constants.

The constant also protects you from mistakes like writing "273.15" in some places and "273" in others.
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic