Results 1 to 7 of 7
  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default return type- error in code

    What is wrong with this. I am getting the error on the first line and it says," Method must return a result of type int"
    1,0,-1 are of type int, isn't it?



    Java Code:
    public int isLargerThan(ComparableObjects r2){		if(this.getArea(this.width, this.height) > r2.getArea(r2.width, r2.height))
    			return 1;
    		else if(this.getArea(this.width, this.height) < r2.getArea(r2.width, r2.height))
    			return -1;
    		else if(this.getArea(this.width, this.height) == r2.getArea(r2.width, r2.height))
    			return 0;
    	}
    Don't forget to smile :-)

  2. #2
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: return type- error in code

    This is the entire source code.
    Java Code:
    package referenceTypes;
    
    public interface Relatable {
    
    
        int isLargerThan(Relatable r2);
    
    
    }
    
    public class ComparableObjects implements Relatable { /*********The type ComparableObjects must implement the inherited abstract method Relatable.isLargerThan(Relatable)*************************************/
    
    
        int width;
        int height;
        
        ComparableObjects(int width, int height){
            this.width=width;
            this.height=height;
        }
        
        public int getArea(int w, int h){
            return w*h;
        }
        
        public int isLargerThan(ComparableObjects r2){  //**********************The Method must return a rest of type int **************************
            if(this.getArea(this.width, this.height) > r2.getArea(r2.width, r2.height))
                return 1;
            else if(this.getArea(this.width, this.height) < r2.getArea(r2.width, r2.height))
                return -1;
            else if(this.getArea(this.width, this.height) == r2.getArea(r2.width, r2.height))
                return 0;
        }
    
    
    
    
    }
    
    public class Main {
    
    
        public static void main(String[] args) {
            ComparableObjects rectOne= new ComparableObjects (20, 30);
            ComparableObjects rectTwo= new ComparableObjects (20, 30);
            
            if(rectOne.isLargerThan(rectTwo)<-1)
            System.out.println("rectOne is smaller than rectTwo.");
            
            else if(rectOne.isLargerThan(rectTwo)==1)
                System.out.println("rectOne is greater than rectTwo.");
            
            else if(rectOne.isLargerThan(rectTwo)>0)
                System.out.println("rectOne is equal to rectTwo.");
        }
    
    
    }
    Don't forget to smile :-)

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: return type- error in code

    The compiler thinks that there is a possibility that none of the if statements would be true and wants to make sure the method returns a value when none of the if statements are true.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: return type- error in code

    The compiler thinks that there is a possibility that none of the if statements would be true and wants to make sure the method returns a value when none of the if statements are true.



    Thank you very much! I gave an else condition and it compiled successfully, but i havn't been able to figure out what the compiler might be thinking, since the areas of two rectangles can not have any other relation between them than >, < or == isn't it?
    Don't forget to smile :-)

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: return type- error in code

    The compiler does not go into the methods that are called to make sure of that. The getArea() method could have a bug and return the wrong value some time.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: return type- error in code

    The compiler does not go into the methods that are called to make sure of that. The getArea() method could have a bug and return the wrong value some time.
    Thank you
    Don't forget to smile :-)

  7. #7
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: return type- error in code

    OOT: why this forum template haven't been fixed? it doesn't show the entire source code...

Similar Threads

  1. Replies: 17
    Last Post: 05-28-2012, 08:30 PM
  2. What Should be my Return type....
    By Anagha in forum New To Java
    Replies: 6
    Last Post: 04-14-2011, 11:29 AM
  3. Replies: 3
    Last Post: 06-05-2010, 08:05 PM
  4. Static String Return Type
    By Java_Developer in forum New To Java
    Replies: 17
    Last Post: 10-03-2009, 06:07 PM
  5. The return type
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:28 AM

Posting Permissions

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