Java 如何重写rectangle类中基于周长进行比较的compareTo方法?
这里有一个矩形类,我需要重写compareTo方法,以便它根据矩形的周长进行比较。我知道目前的比较方法是错误的,但我不知道如何修复它 代码如下:Java 如何重写rectangle类中基于周长进行比较的compareTo方法?,java,Java,这里有一个矩形类,我需要重写compareTo方法,以便它根据矩形的周长进行比较。我知道目前的比较方法是错误的,但我不知道如何修复它 代码如下: public class Rectangle implements Comparable<Rectangle> { private double length; private double width; private double perimeter; public Rectangle(dou
public class Rectangle implements Comparable<Rectangle> {
private double length;
private double width;
private double perimeter;
public Rectangle(double l, double w){
this.length = l;
this.width = w;
}
public double getLength() {
return length;
}
public double getWidth(){
return width;
}
public void setLength(double l){
length= l;
}
public void setWidth(double w){
width = w;
}
public double getPerimeter(){
perimeter = 2*(width + length);
return perimeter;
}
@Override
public int compareTo(Rectangle other){
String t=Double.toString(perimeter);
String o=Double.toString(other.perimeter);
int comp = t.compareTo(o);
return comp;
}
@Override
public String toString(){
return "Rectangle: "+ width +" by "+ length ;
}
}
公共类{
私人双倍长度;
私人双宽度;
私人双周界;
公共矩形(双l,双w){
这个长度=l;
这个宽度=w;
}
公共双getLength(){
返回长度;
}
公共双getWidth(){
返回宽度;
}
公共空间设置长度(双l){
长度=l;
}
公共空间设置宽度(双w){
宽度=w;
}
公共双边界{
周长=2*(宽度+长度);
返回周长;
}
@凌驾
公共整数比较(矩形其他){
字符串t=双。toString(周长);
字符串o=Double.toString(其他周长);
int comp=t.compareTo(o);
返回补偿;
}
@凌驾
公共字符串toString(){
返回矩形:“+宽度+”乘以“+长度;
}
}
试试这个
public int compareTo(Rectangle otherRectangle) {
return Double.compare(this.getPerimeter(), otherRectangle.getPerimeter());
}
试试这个
public int compareTo(Rectangle otherRectangle) {
return Double.compare(this.getPerimeter(), otherRectangle.getPerimeter());
}
试试这个:
您需要使用GetPermission,因为您的计算逻辑就在这里:
@Override
public int compareTo(Rectangle other){
return Double.compare(getPerimeter(), other.getPerimeter());
}
试试这个:
您需要使用GetPermission,因为您的计算逻辑就在这里:
@Override
public int compareTo(Rectangle other){
return Double.compare(getPerimeter(), other.getPerimeter());
}
几件事-
矩形的参数,而不是getter作为
public Rectangle(double l, double w) {
this.length = l;
this.width = w;
this.perimeter = 2 * (this.width + this.length);
}
然后你的
周长s getter和setter会像-
public double getPerimeter() {
return perimeter;
}
public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
Double
classcompare
方法进行比较,如下所示
@Override
public int compareTo(CompareToRectangle other) {
return Double.compare(this.perimeter, other.perimeter);
}
public static void testRectangle() {
Rectangle first = new Rectangle(2.00, 3.00);
Rectangle second = new Rectangle(4.00, 3.00);
if (first.compareTo(second) == 0) {
System.out.println("Similar rectangles.");
} else if(first.compareTo(second) == 1) {
System.out.println("First rectangle bigger than second.");
} else {
System.out.println("Second rectangle is bigger than first.");
}
}
矩形的参数,而不是getter作为
public Rectangle(double l, double w) {
this.length = l;
this.width = w;
this.perimeter = 2 * (this.width + this.length);
}
然后你的
周长s getter和setter会像-
public double getPerimeter() {
return perimeter;
}
public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
Double
classcompare
方法进行比较,如下所示
@Override
public int compareTo(CompareToRectangle other) {
return Double.compare(this.perimeter, other.perimeter);
}
public static void testRectangle() {
Rectangle first = new Rectangle(2.00, 3.00);
Rectangle second = new Rectangle(4.00, 3.00);
if (first.compareTo(second) == 0) {
System.out.println("Similar rectangles.");
} else if(first.compareTo(second) == 1) {
System.out.println("First rectangle bigger than second.");
} else {
System.out.println("Second rectangle is bigger than first.");
}
}
所有其他答案都是正确的。这个答案只是添加了一个矩形数组,并根据它们的周长使用
java.util.array.sort
对它们进行排序,以证明比较器正在工作。此外,周长的计算被移动到构造函数中,而不是getperiment
方法
import java.util.Arrays;
public class Rectangle implements Comparable<Rectangle> {
private double length;
private double width;
private double perimeter;
public Rectangle(double l, double w){
this.length = l;
this.width = w;
perimeter = 2 *(width+length);
}
public double getLength() {
return length;
}
public double getWidth(){
return width;
}
public void setLength(double l){
length= l;
}
public void setWidth(double w){
width = w;
}
public double getPerimeter(){
perimeter = 2*(width + length);
return perimeter;
}
@Override
public int compareTo(Rectangle other){
return Double.compare(perimeter, other.perimeter);
}
@Override
public String toString(){
return "Rectangle: "+ width +" by "+ length ;
}
public static void main(String args[]) {
Rectangle [] arrayOfRectangles = new Rectangle [3];
arrayOfRectangles[0] = new Rectangle(1.0,1.0);
arrayOfRectangles[1] = new Rectangle(6.0,5.0);
arrayOfRectangles[2] = new Rectangle(2.0,3.0);
Arrays.sort(arrayOfRectangles);
for (int i = 0; i < 3; i++) {
System.out.println(arrayOfRectangles[i]);
}
}
}
导入java.util.array;
公共类矩形实现可比较{
私人双倍长度;
私人双宽度;
私人双周界;
公共矩形(双l,双w){
这个长度=l;
这个宽度=w;
周长=2*(宽度+长度);
}
公共双getLength(){
返回长度;
}
公共双getWidth(){
返回宽度;
}
公共空间设置长度(双l){
长度=l;
}
公共空间设置宽度(双w){
宽度=w;
}
公共双边界{
周长=2*(宽度+长度);
返回周长;
}
@凌驾
公共整数比较(矩形其他){
返回Double.compare(周长,其他周长);
}
@凌驾
公共字符串toString(){
返回矩形:“+宽度+”乘以“+长度;
}
公共静态void main(字符串参数[]){
矩形[]arrayOfRectangles=新矩形[3];
arrayOfRectangles[0]=新矩形(1.0,1.0);
arrayOfRectangles[1]=新矩形(6.0,5.0);
arrayOfRectangles[2]=新矩形(2.0,3.0);
Arrays.sort(arrayOfRectangles);
对于(int i=0;i<3;i++){
System.out.println(arrayOfRectangles[i]);
}
}
}
所有其他答案都是正确的。这个答案只是添加了一个矩形数组,并根据它们的周长使用java.util.array.sort
对它们进行排序,以证明比较器正在工作。此外,周长的计算被移动到构造函数中,而不是getperiment
方法
import java.util.Arrays;
public class Rectangle implements Comparable<Rectangle> {
private double length;
private double width;
private double perimeter;
public Rectangle(double l, double w){
this.length = l;
this.width = w;
perimeter = 2 *(width+length);
}
public double getLength() {
return length;
}
public double getWidth(){
return width;
}
public void setLength(double l){
length= l;
}
public void setWidth(double w){
width = w;
}
public double getPerimeter(){
perimeter = 2*(width + length);
return perimeter;
}
@Override
public int compareTo(Rectangle other){
return Double.compare(perimeter, other.perimeter);
}
@Override
public String toString(){
return "Rectangle: "+ width +" by "+ length ;
}
public static void main(String args[]) {
Rectangle [] arrayOfRectangles = new Rectangle [3];
arrayOfRectangles[0] = new Rectangle(1.0,1.0);
arrayOfRectangles[1] = new Rectangle(6.0,5.0);
arrayOfRectangles[2] = new Rectangle(2.0,3.0);
Arrays.sort(arrayOfRectangles);
for (int i = 0; i < 3; i++) {
System.out.println(arrayOfRectangles[i]);
}
}
}
导入java.util.array;
公共类矩形实现可比较{
私人双倍长度;
私人双宽度;
私人双周界;
公共矩形(双l,双w){
这个长度=l;
这个宽度=w;
周长=2*(宽度+长度);
}
公共双getLength(){
返回长度;
}
公共双getWidth(){
返回宽度;
}
公共空间设置长度(双l){
长度=l;
}
公共空间设置宽度(双w){
宽度=w;
}
公共双边界{
周长=2*(宽度+长度);
返回周长;
}
@凌驾
公共整数比较(矩形其他){
返回Double.compare(周长,其他周长);
}
@凌驾
公共字符串toString(){
返回矩形:“+宽度+”乘以“+长度;
}
公共静态void main(字符串参数[]){
矩形[]arrayOfRectangles=新矩形[3];
arrayOfRectangles[0]=新矩形(1.0,1.0);
arrayOfRectangles[1]=新矩形(6.0,5.0);
arrayOfRectangles[2]=新矩形(2.0,3.0);
Arrays.sort(arrayOfRectangles);
对于(int i=0;i<3;i++){
System.out.println(arrayOfRectangles[i]);
}
}
}
为什么要转换为字符串?将“return comp;
”替换为“return periment-other.periment;
”并删除创建t
和o
字符串的行。但是如何比较周长呢?我不明白你返回perimeter-other是什么意思。perimeterIn与之相比,你必须返回0,一个负数或正数,这取决于