Help with equals/compareTo methods
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Help with equals/compareTo methods

  1. #1
    Join Date
    Feb 2012
    Posts
    1

    Help with equals/compareTo methods

    Okay so I've been trying to figure this out for about 4 hours now and cannot figure out how to make these two methods work correctly. My instructor told me he wanted this for the two methods.
    Write an equals method that returns true if two Temperature objects are within 1/10 of a degree of each other.

    Write a compareTo method that returns -1 if the invoking object has a lower temp, 0 if they are the same, and 1 if the invoking object is larger.

    Here is my code
    Code:
    public class Temperature {
    
    	private double degrees;
    	private char scale;
    
    	
    	
    	public Temperature()
    	{
    		this(0,'C');
    		
    	}
    	public Temperature(double degrees)
    	{
    		this.scale = 'C';
    	}
    	
    	public Temperature(char scale)
    	{
    		this.degrees = 0;
    	}
    	public Temperature(double degrees, char scale)
    	{
    		this.degrees = degrees;
    		this.scale=scale;
    		
    	}
    	public void setScale(char scale)
    	{
    		this.scale=scale;
    		
    	}
    	public char getScale()
    	{
    		return scale;
    	}
    	public void setDegrees(double degrees)
    	{
    		this.degrees=degrees;
    		
    	}
    	public double getDegrees()
    	{
    		return degrees;
    	}
    	
    	public double getC()
    	{
    		if(this.scale == 'C')
    		{
    			return degrees;
    		}
    		else
    		{
    			return (5*(degrees-32))/9;
    		}
    	}
    
    
    	public double getF()
    	{
    		if(this.scale == 'F')
    		{
    			return degrees;
    		}
    		else
    		{
    			return (9*(degrees)/5)+32;
    			
    		}
    	}
    
    //	 public boolean equals(Object obj )
    //	  {
    //		  if(!(obj instanceof Temperature))
    //		  {
    //			  return false;
    //		  }
    //		  Temperature other = (Temperature)obj;
    //		  return this.degrees.equals(other.degrees);
    //	  }
    
    	public int compareTo(Temperature other)
    	  {
    		  if(this.getF()< this.getF() ||
    		     this.getC() < this.getC())
    
    		  {
    			  return -1;
    		  }
    		  if(this.getF()> this.getF() ||
    				     this.getC() > this.getC())
    
    		  {
    			return 1;
    		  }
    		  return 0;
    	  }
    	  public String toString()
    	  {
    		  return "Value in Farenheit " + this.getF() + "\n" +
    		         "Value in Celcius " + this.getC();	        
    	  }
    }

    And here is the TestTemperature that he gave us.
    Code:
    
    public class TestTemperature {
    
    
    	
    	
    	public static void main(String[] args) {
    		// degrees = 0   scale = C
    		Temperature temp1 = new Temperature();
    
    		// degrees = 80   scale = C
    		Temperature temp2 = new Temperature(80);
    		 
    
    		// degrees = 0   scale = F
    		Temperature temp3 = new Temperature('F');
    		 
    
    		// degrees = 32   scale = F
    		Temperature temp4 = new Temperature(32, 'F');
    
    		System.out.println("Temp4 in C is: " + temp1.getC());
    
    		System.out.println("Temp4 in F is: " + temp1.getF());
    
    		// should be same
    		if(temp1.equals(temp4))
    		{
    		   System.out.println("same");
    		}
    		else
    		{
    		   System.out.println("not same");
    		}
    
    		// to string showing both scales
    		System.out.println(temp4);
    
    		// will print -1, 1, 0
    		System.out.println(temp1.compareTo(temp2));
    		System.out.println(temp2.compareTo(temp1));
    		System.out.println(temp1.compareTo(temp4));
    
    	}
    
    }
    Could anyone help me? I'm just really lost as to how to figure this out.

  2. #2
    Join Date
    May 2009
    Location
    Surrey, UK
    Posts
    285

    Re: Help with equals/compareTo methods

    The problem with your equals method is that degrees is a double (primitive) variable and you try to call .equals() as if it were Double:
    Code:
    return this.degrees.equals(other.degrees);
    You are also keeping the temperature in the degrees variable but it could be either F or C, so just comparing the values of this.degrees and other.degrees is not enough (that would only work for -40C = -40F).

    Have a look at your compareTo method and see where you use other. Also, why are you comparing the temperatures in F and C?

    And finally, check your constructors, particularly those two taking a single argument.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Help with equals/compareTo methods

    As well as jcaccia's very valid comments, just forget the code for a moment and write down how you would solve this if presented with it as problem to solve with pen and paper.

    So how would you check if 2 numbers are "within 1/10 of a degree of each other"?

    The question doesn't mention scale so it must be within 1/10 of a degree of whatever scale this object is using. I say 'this object' rather than 'the objects' as they may have different scales, so the first task is to get them both into this object's scale. Then do the comparison (hint: it may be easier if you subtract one from the other and check the result).

    Ok, so now that you have written a out algorithm of the steps to take you can convert it to code.

    Finally use the same technique for the compare method.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center