Results 1 to 6 of 6
  1. #1
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default OOP newbie, booleans not working

    I am simply trying to test if two temperatures, one in F and one in C, are the same. But no matter what it comes out true. I am confused on how to get it compare the two values, and do the math to determine if the values are the same.

    Thanks - Gary :confused:

    Java Code:
    
    public class Temperature
    {
    	private double tempValue;
    	private char scale;
    
    
    	//constructors
    
    public Temperature(double tempValue)
    {
    	this.tempValue = tempValue;
    	scale = 'C';
    }
    
    public Temperature(char scale)
    {
    	 tempValue = 0;
    	 this.scale = scale;//should this be C?
    }
    
    public Temperature(double tempValue, char scale)
    {
    	 this.tempValue = tempValue;
    	 this.scale = scale;
    }
    
    public Temperature()
    {
    	 tempValue = 0;
    	 scale = 'C';
    }
    
    //mutators or setters
     public void setTempValue(double tempValue)
    	 {
    	 	this.tempValue = tempValue;
    	 }
    
    public void setScale(char scale)
    	 {
    	 	this.scale = scale;
    	 }
    
    public void setBoth(double tempValue, char scale)
    {
    	this.tempValue = tempValue;
    	this.scale = scale;
    }
    
    //getters
    
    public double getTempValue() //not sure I need this but put it in so boolean works
    {
    	double tempValue = 0;
    	return tempValue;
    }
    
    public double getTemperatureF()
    {
    
    	 double degreesF = (9 * (tempValue / 5)) + 32;
    	 return degreesF;
    }
    public double getTemperatureC()
    {
    
    	 double degreesC = 5 *(tempValue - 32) /9;
    	 return degreesC;
    }
    
    //boolean needed to 1) test whether two temps are equal, and 2) test if one temp is less han the other
    
    public boolean equals(Temperature otherTemperature)
    {
    	return(this.getTempValue() == otherTemperature.getTempValue());
    
    }
    public boolean isLessThan(Temperature otherTemperature)
    {
    	return(getTemperatureF() < getTemperatureC());
    
    }
    
    public boolean isGreaterThan(Temperature otherTemperature)
    {
    	return(getTemperatureF() > getTemperatureC());
    }
    
    //toString
    
    public String toString()
    {
       return tempValue + " " + scale;
    }
    }
    AND HERE IS THE PART THAT TESTS IT

    Java Code:
    import java.util.*;
    
    
    public class TempDemo
    {
    
    public static void main(String[] args)
    {
    	//use all of the constructors
    	Temperature zeroC = new Temperature();
        zeroC.setTempValue(0.0);
    	System.out.println("Freezing Water in Celsius: " + zeroC.toString());
    
       	Temperature freezeF = new Temperature(32.0, 'F');
        freezeF.setTempValue(32.0);
    	freezeF.setScale('F');
        System.out.println("Freezing Water in Farenheit: " + freezeF.toString());
    
     	Temperature negativeC = new Temperature();
        negativeC.setTempValue(-40.0);
        System.out.println("Negative number in Celcius: " + negativeC.toString());
    
    	Temperature negativeF = new Temperature();
    	negativeF.setTempValue(-40.0);
    	negativeF.setScale('F');
        System.out.println("Negative number in Farenheit: " + negativeF.toString());
    
    	Temperature positiveC = new Temperature();
    	positiveC.setTempValue(100.0);
    	System.out.println("Positive number in Celsius: " + positiveC.toString());
    
    	Temperature positiveF = new Temperature();
    	positiveF.setTempValue(212.0);
    	negativeF.setScale('F');
        System.out.println("Positive number in Farenheit: " + positiveF.toString());
    
    // test for equality
        System.out.println(zeroC + " = " + freezeF + " is " + zeroC.equals(freezeF));
    
     	System.out.println(negativeC + " = " + negativeF + " is " + negativeC.equals(negativeF));
    }
    
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You're comparing two objects' "getTempValue" results, which is always zero:
    Java Code:
    public double getTempValue() //not sure I need this but put it in so boolean works
    {
    	double tempValue = 0;
    	return tempValue;
    }
    Since 0 == 0 always, it will always be true.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gnng View Post
    Java Code:
    public double getTempValue() //not sure I need this but put it in so boolean works
    {
    	double tempValue = 0;
    	return tempValue;
    }
    This method always returns 0.0 which is not what you want.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    public double getTempValue() //not sure I need this but put it in so boolean works
    {
    	double tempValue = 0;
    	return tempValue;
    }
    
    public boolean equals(Temperature otherTemperature)
    {
    	return([COLOR="red"]this[/COLOR].getTempValue() == otherTemperature.getTempValue());
    
    }
    no you don't need to put that in to make your boolean work, your boolean isn't working because this.getTempValue() returns Temperature.getTempValue() -> the method you just made up.

    i'm not sure how to code in the way you want so that you can use object.equals(otherObject) but if you're willing to take a different approach just throw both temperatures into parameters e.g. boolean isEqual(Temperature a, Temperature b);

  5. #5
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    You are trying to achieve very little from too much of code.

    You seriously need to refactor your code.

    Just a few things which one must take care of:
    1. If you are not using the arguments in a method, then do not declare them as part of method signature. The thing you are doing in isLessThan & isGreaterThan methods.
    2. No need to write three different methods for comparison. All three comparisons can be done in a single method, Just put a little thought into this.
    3. You have unwanted contructors in your class. Just think do you need them all.

  6. #6
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default

    OK, so I am pretty confused! Let me first state that I am taking this class online, meaning NO teacher interaction. So I am reading a book and doing the best I can trying to figure out how to do this.

    Could someone explain what code I should change and what I should change it to? For example, you note that this code is wrong - double tempValue = 0; But I don't know enough to know what the right answer is. I had thought you had to declare it as 0 based on previous exercises. I can't change it to (), and I can't just put a semi-colon after tempValue.

    Thanks for your help.

Similar Threads

  1. Noob question about using ints in booleans
    By elfdreaming in forum New To Java
    Replies: 6
    Last Post: 03-14-2011, 11:07 AM
  2. Java thread issue. booleans don't seem to work
    By Fortu in forum New To Java
    Replies: 2
    Last Post: 12-31-2010, 11:14 PM
  3. Having problems with static booleans
    By Medieve in forum New To Java
    Replies: 4
    Last Post: 12-08-2010, 06:07 PM
  4. arrays with booleans
    By hoosierfan24 in forum New To Java
    Replies: 24
    Last Post: 10-04-2010, 01:07 AM
  5. Finding Strings, booleans and Integers
    By Pez in forum SWT / JFace
    Replies: 1
    Last Post: 07-19-2009, 02:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •