this is my code, the problem the compiler is saying is i need an extra parentheses after 5 in get fahr, but i know that cant be it... help meeeeeee please :)

public double getFahr()
{   
    if (tempUnit.equals('C'))
    {
        return (5(tempNum-32)/9);
    }
    else
    {
        return tempNum;
    }
}
 public double getCels()
{   
    if (tempUnit.equals('F'))
    {
        return(9(tempNum)/5)+32);
    }
    else
    {
        return tempNum;
    }
}

Recommended Answers

All 16 Replies

these are just the methods im having the problems with not all the code btw..

That looks OK - maybe the parens are already out of step before that code?
Also, I don't know what the data type for tempUnit is, but it's unusual to see a char as parameter for equals. char is an integer primitive type, for which == is appropriate.

same remark: they indeed seem to be chars, not objects. second remark: you can simplify that code a bit:

public double getFahr()
{   
    if (tempUnit =='C') //assuming they are chars
    {
        return (5(tempNum-32)/9);
    }
        return tempNum;
}
commented: Haha thanks for the advice on shortening it :) +0

Thank you for mentioning that infor about char James, but here is what i have before those two methods up to and including them. Still after I put == as the operator it still wants another paren. :/

public class Temperature
{
   private float tempNum;
   private char tempUnit;
   //Constructors
   public Temperature(){//default constructor
       tempNum = 0;
       tempUnit = 'C';}
       public Temperature(double newTempNum){
           tempNum = newTempNum;
           tempUnit = 'C';}
           public Temperature(char newTempUnit){
               tempNum = 0;
               tempUnit = newTempUnit;}
               public Temperature(double newTempNum, char newTempUnit){
                   tempNum = newTempNum;
                   tempUnit = newTempUnit;}
   public double getFahr()
{   
    if (tempUnit==('C'))
    {
        return (5(tempNum-32)/9);
    }
    else
    {
        return tempNum;
    }
}
 public double getCels()
{   
    if (tempUnit==('F'))
    {
        return(9(tempNum)/5)+32);
    }
    else
    {
        return tempNum;
    }
}

OK. got it
5(tempNum-32)/9
isn't a valid expression. You must specify the multiplication explicitly. It's just not one of the compiler's best error messages ;)
(ps no point having doubles for the results when the input (tempNum) is just a float.

commented: haha! wow thanks james can't believe it was just me needing to put an asterisk! +0

well that fixed my problem for those two methods thank you so much again, having another problem unfortunately with this, it is giving me an incomparable types "double" and "temperature".

public boolean equals(Temperature testObj)
{
    if((tempNum)==(testObj))
    {
        System.out.println("These temperatures are equal");
    }
    else
    {
        System.out.prinln("These temperatures are not equal");
    }
}
public boolean isGreaterTemp(Temperature testObj)
{
    if((tempNum)>(testObj))
    {
        System.out.println(tempNum+" is greater than "+testObj);
    }
    else
    {
        System.out.println(tempNum+" is lesser than "+testObj);
    }
}
public boolean isLesserTemp(Temperature testObj)
{
    if((tempNum)<(testObj))
    {
        System.out.println(tempNum+" is lesser than "+testobj);
    }
    else
    {
        System.out.println(tempNum+" is greater than "+testObj);
    }
}

(tempNum)==(testObj)

tempNum is a double primitive
testObj is an object of class Temperature

they are so different in kind that using == makes no sense - its like asking "is London equal to Tuesday?"

For equals you want to know if two Temperature objects are "equal", where it's up to you to decide what "equals" means for Temperatures. The obvious answer is that the value of both Temperatures expressed in the same units (C or F) should be the same, within the limits of their floating point precision. You already have the get methods you need to get the values to compare.

ps: good coding guide part XIV: <soapbox>
the names you use for your variables and methods tells everything about your code and you as a programmer. "tempNum" is a horrible name for a temperature value that isn't temporary or just a number.
When you try to think of a good name you get into things like "currentTemperatureInTheUnitsSpecifiedByTempUnit" - which is onbviously silly, but a clear sign that the variable itself is confusing.
Personally that signals to me that I should simplify my design to the point where its easy to explain what the variables are. Eg in this case I would chose a unit to hold the temperature in (eg degrees Kelvin or Absolute) , and always convert F or C into that unit. Now I can name the variable "degreesKelvin", get rid of the tempUnit variable altogether, and the code is instantly 100% clearer - easier to read, easier to understand, easier to debug, easier to fix, easier to avoid mistakes in the first place. </soapbox>

What im trying to do is compare the molecular motion in those statements and not the units they are in(c or F) like whether 0C is equal to 32F(true), but im at a block here still... I cant figure out how to get these booleans to test for equality in this program.

So your definition of "equal" is sonething like:
Temperatures t1 and t2 are "equal" if and only if t1 in degrees Celcius == t2 in degrees Celcius, within the precision of their double values.
(or exactly the same with both Farenheit values)

I can't explain that more clearly without just giving you the code (which we don't do for homework, as I'm sure you understand)

okay thank you james i will give it a try and let you know if i am still having issues.

When you said i already have the get methods what do you mean by getting the values to compare?

I just meant that the English specification "t1 in degrees Celcius" is easily implemented in your program by t1.getCels()

ok so i cant use the binary operators here at all then? if its not to revealing could i ask for a way to compare equality between objects of a class type instead of primitive.

The only way to compare equality between objects of a class is to use the class's equals method - the very one that you are writing now!
But if you use getCels() to get the value of each Temperature in degrees Celcius then that value is a primitive double, so you can use binary operators eg t1.getCels() > t2.getCels() because both those values are just doubles.
(Hint: == is a problem with floating point values, so don't be surprised if it doesn't work exactly as you would hope)

Thank you for all your help James, i figured it out!

Excellent!
Please mark this "solved" for our knowledge base. You should start a new thread if you run into any new problems.
cheers
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.