junaidkaps
junaidkaps

Reputation: 59

How do I code a simple boolean method? (Can't recall how to to code the one below)

I need to create a method largerThan(See below) which takes a Rectangle object as an argument and returns true if the invoking object has a greater area than the object which is the argument and will return false otherwise. I've done this before but simply can't recall how to complete the code in this part of the method. Any help will be truly appreciated! NOTE: Professor doesn't want us to use the "this" operator! :-(

public class Rectangle
{

  private double length;

  private double width; 

    public Rectangle()
    {
      length = 0; 
      width = 0; 
    }
    public Rectangle(double l, double w)
    {
      length = l;
      width = w;
    }
    public void setRectangle(double l, double w)
    {
      length = l; 
      width = w; 
    }
    public double getLength()
    {
      return length;
    }
    public double getWidth()
    {
      return width;
    }
    public double perimeter()
    {
      return length + width; 
    }
    public double Area()
    {
      return length*width;
    }
    **public boolean largerThan(Rectangle r1)
    {
      if()
        return True;
      else
        return False; 
    }**
    public String toString()
    {
       return "Length is " + length + " width is " + width; 
    }
}

Upvotes: 0

Views: 624

Answers (3)

Timeout
Timeout

Reputation: 7909

public boolean largerThan(Rectangle otherRec){
    return this.Area() > otherRec.Area();
}

Upvotes: 3

thekaveman
thekaveman

Reputation: 4399

Your skeleton is basically there, now take the English words of what you want to do:

returns true if the invoking object has a greater area than the object which is the argument and will return false otherwise

And turn it in to code:

public boolean largerThan(Rectangle r1)
{
  if(this.Area() > r1.Area())
    return True;
  else
    return False; 
}

Upvotes: 1

plucury
plucury

Reputation: 1120

You can do it like this:

public boolean largerThan(Rectangle r1){
    return this.Area() > r1.Area();
}

Upvotes: 1

Related Questions