• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused  RSS feed

 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
i don't know what its asking for or what to do
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
does that mean


?
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!