如何编写简单的布尔方法代码? (不记得如何编写下面的代码)

发布于 2025-01-03 11:27:44 字数 1011 浏览 1 评论 0原文

我需要创建一个方法largerThan(见下文),它接受 Rectangle 对象作为参数,如果调用对象的面积大于作为参数的对象,则返回 true,否则返回 false。我以前已经这样做过,但只是不记得如何完成该方法这部分中的代码。任何帮助将不胜感激!注意:教授不希望我们使用“this”运算符! :-(

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; 
    }
}

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; 
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万人眼中万个我 2025-01-10 11:27:44
public boolean largerThan(Rectangle otherRec){
    return this.Area() > otherRec.Area();
}
public boolean largerThan(Rectangle otherRec){
    return this.Area() > otherRec.Area();
}
超可爱的懒熊 2025-01-10 11:27:44

你可以这样做:

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

You can do it like this:

public boolean largerThan(Rectangle r1){
    return this.Area() > r1.Area();
}
孤单情人 2025-01-10 11:27:44

你的骨架基本上就在那里,现在记下你想要做的事情的英文单词:

如果调用对象的面积大于作为参数的对象的面积,则返回 true,否则返回 false

并将其转换为代码:

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

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; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文