gpt4 book ai didi

java - 在 Java 中设置 CompareTo() 方法

转载 作者:太空宇宙 更新时间:2023-11-04 06:37:44 25 4
gpt4 key购买 nike

请注意:我之前创建了一个帖子,其中包含这个问题以及其他几个问题,但被告知由于我在同一篇帖子中提出了很多问题,因此最好将其分解为单独的问题。因此,请不要将其标记为重复,是的,说明是相同的,是的,正在使用相同的代码,但问题本身是不同的。谢谢。

我正在按照以下说明编写一个程序:

Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all 8 sides of the octagon are of equal size. The area can be computed using the following formula

area = (2 + 4/square root of 2) * side * side 

Write a program (driver) to read in a series of values from a file, display the area and perimeter, create a clone and compare the object and its clone (based on the area). In addition, your program should compare the current object (just read in) with the first object read in. The program ends when a negative number is read from the file.

这是我到目前为止的代码,这是我的 GeometricObject 类:

public abstract class GeometricObject {

public abstract double getArea();
public abstract double getPerimeter();
}

我的八角形类:

public class Octagon extends GeometricObject implements Comparable<Octagon> {
private double side = 1.0;
protected native Object clone() throws CloneNotSupportedException;
private static int numberOfObjects = 0;

public Octagon() {
}

public Octagon(double side) {
this.side = side;
numberOfObjects++;
}

public double getSide() {
return side;
}

public void setSide(double side) {
this.side = side;
}

public double getArea() {
return (2 + (4 / (Math.sqrt(2))) * side * side);
}

public double getPerimeter() {
return side * 8;
}

public String toString() {
return "Octagon " + numCreated + ": Area: " + getArea() + "\nPerimeter: "
+ getPerimeter() + "\nClone Compare: " + Cloneable + "\nFirst Compare: "
+ comparisson;
}

public int compareTo(Octagon octagon) {
if(getArea() > octagon.getArea())
return 1;
else if(getArea() < octagon.getArea())
return -1;
else
return 0;
}

public interface Cloneable {
}
}

还有我的Driver或测试器类:(这是我最需要帮助的地方)

import java.util.*;
import java.io.*;

public class Driver {
public static void main(String[] args) {
int comparisson = compareTo(octagon);
java.io.File file = new java.io.File("prog7.dat");
Scanner fin = new Scanner(file);

while(fin.hasNext()) {
double side = fin.nextDouble();
if(side < 0.0) break;
Octagon first = new Octagon(side);
System.out.println("Octagon 1: " + first);

}

}
}

这是用于获取输入的文件。每条线都是一个八边形:

5.0
7.5
3.26
0.0
-1.0

我在设置 compareTo() 方法时遇到问题。我以前从未使用过它,虽然我阅读了文档并大致了解了它的工作原理,但我无法弄清楚如何将它实现到我的特定程序中。

沿着同样的思路,我知道 compareTo() 方法根据当前对象是否小于、大于或等于与其进行比较的对象,返回 -1、0 或 1 的整数值。但是,正如您从示例输出中看到的那样,我应该显示类似“第一个较小”的内容,而不是显示此整数值。我很确定要执行此操作,我需要将返回值分配给变量,然后使用 if 语句来确定应打印的内容,但我无法弄清楚应该如何以及在代码中的什么位置完成此操作?

最佳答案

基于您的这一行要求:此外,您的程序应该将当前对象(刚刚读入)与读入的第一个对象进行比较。下面是一些执行此操作的代码。基本上,这会读取 Octagon 直到读取到负数,并将每个数字与读入的第一个 Octagon 进行比较。

Octagon first = null;
int i = 0;
while(fin.hasNext())
{
double side = fin.nextDouble();
if(side < 0.0)
break;
Octagon oct = new Octagon(side);
System.out.print("Octagon " + i + ": \"" + oct.toString() + "\"");
if (first == null)
first = oct;
else
{
// Here is where we compare the current Octagon to the first one
int comparison = oct.compareTo(first);
if (comparison < 0)
System.out.print(", less than first");
else if (comparison > 0)
System.out.print(", greater than first");
else System.out.print(", equal to first");
}
Octagon first = new Octagon(side);
System.out.println();
}

关于java - 在 Java 中设置 CompareTo() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25128158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com