如何编写简单的布尔方法代码? (不记得如何编写下面的代码)
我需要创建一个方法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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做:
You can do it like this:
你的骨架基本上就在那里,现在记下你想要做的事情的英文单词:
并将其转换为代码:
Your skeleton is basically there, now take the English words of what you want to do:
And turn it in to code: