gpt4 book ai didi

java - 尺寸比较(周长/平方周长)等

转载 作者:行者123 更新时间:2023-12-02 01:49:13 25 4
gpt4 key购买 nike

3个不同的 1个用于处理Circle实例,1个用于Square实例,第3个用于比较之间他们(main) 。在 main 函数中,我找到了 circle (在 c1..c4 之间)和 square (在 s1...s5)并分别打印它们的最大周长和面积。[因此圆与圆和方与方的比较]

<强>!!!注意:只有半径或边长较大的周长或面积最大,所以我只使用 ra 进行比较。我不知道如果我使用area/circumference方法是否可以返回this(不,因为那么我只会处理数字?) .请纠正我。

现在我想打印具有最大周长几何形状(圆形/正方形)的特征(x,y,r/a)。我怎样才能做到这一点 ?在哪里比较?新类(class)?[方圆比较]

public class Circle {
public double x,y,r;
public double circumference() {
return 2*(3.14)*r;
}
public double area() {
return 3.14*r*r;
}
public Circle bigger(Circle c){
if(c.r>r) return c; else return this;
}
public Circle(double x, double y, double r) {
this.x=x;
this.y=y;
this.r=r;
}
}

public class Square {
public double x,y,a;
public double perimeter() {
return 4*a;
}
public double area() {
return a*a;
}
public Square bigger(Square s){
if(s.a>a) return s; else return this;
}
public Square(double x, double y, double a) {
this.x=x;
this.y=y;
this.a=a;
}
}

public class CircleAndSquareTest {

public static void main(String[] args) {
Circle c1 = new Circle(0.0,0.0,1.0);
Circle c2 = new Circle(1.0,0.0,2.0);
Circle c3 = new Circle(0.0,2.0,4.0);
Circle c4 = new Circle(1.0,3.0,1.0);
Circle cb = c1.bigger(c2).bigger(c3).bigger(c4);
System.out.println("The circle with the biggest circumference has:\n");
System.out.println("x-axis value: " + cb.x + " y-axis value: " + cb.y + " radius: " + cb.r+"\n");

Square s1 = new Square(0.0,0.0,1.0);
Square s2 = new Square(0.0,0.0,1.0);
Square s3 = new Square(0.0,0.0,5.0);
Square s4 = new Square(4.0,2.0,2.0);
Square s5 = new Square(0.0,0.0,1.0);
Square sb = s1.bigger(s2).bigger(s3).bigger(s4).bigger(s5);
System.out.println("The square with the biggest area has:\n");
System.out.println("x-axis value: " + sb.x + " y-axis value: " +
sb.y + " side: " + sb.a);
}
}

最佳答案

以下是如何使用比较器和 Collections 类来查找最大值。这尚未经过测试,但它应该可以满足您的要求。请注意,我在这里使用静态内部类,但如果需要,它们可以是在自己的文件中定义的标准类 - 这只是为了创建快速答案。

public interface Shape {
double getPerimeter();
double getArea();
}

public static class PerimeterComparator implements Comparator<Shape> {
@Override
public int compare(Shape a, Shape b) {
return Double.compare(a.getPerimeter(), b.getPerimeter());
}
}

public static class AreaComparator implements Comparator<Shape> {
@Override
public int compare(Shape a, Shape b) {
return Double.compare(a.getArea(), b.getArea());
}
}

public static class Circle implements Shape {
private final double x, y, r;

@Override
public double getPerimeter() {
return 2 * (3.14) * r;
}

@Override
public double getArea() {
return 3.14 * r * r;
}

public Circle(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}

public double getX() {
return x;
}

public double getY() {
return y;
}

public double getR() {
return r;
}
}

public static class Square implements Shape{
private final double x, y, a;

@Override
public double getPerimeter() {
return 4 * a;
}

@Override
public double getArea() {
return a * a;
}

public Square(double x, double y, double a) {
this.x = x;
this.y = y;
this.a = a;
}

public double getX() {
return x;
}

public double getY() {
return y;
}

public double getA() {
return a;
}
}


public static void main(String[] args) {
List<Shape> shapes = new ArrayList<>();
List<Circle> circles = new ArrayList<>();
circles.add(new Circle(0.0,0.0,1.0));
circles.add(new Circle(1.0,0.0,2.0));
circles.add(new Circle(0.0,2.0,4.0));
circles.add(new Circle(1.0,3.0,1.0));

Circle largestCircle = Collections.max(circles, new PerimeterComparator());

System.out.println("The circle with the biggest circumference has:\n");
System.out.println("x-axis value: " + largestCircle.getX() + " y-axis value: " + largestCircle.getY() + " radius: " + largestCircle.getPerimeter() +"\n");

List<Square> squares = new ArrayList<>();
squares.add(new Square(0.0,0.0,1.0));
squares.add(new Square(0.0,0.0,1.0));
squares.add(new Square(0.0,0.0,5.0));
squares.add(new Square(4.0,2.0,2.0));
squares.add(new Square(0.0,0.0,1.0));

Square largestSquare = Collections.max(squares, new PerimeterComparator());

System.out.println("The square with the biggest area has:\n");
System.out.println("x-axis value: " + largestSquare.getX() + " y-axis value: " + largestSquare.getY() + " side: " + largestSquare.getA());

shapes.addAll(circles);
shapes.addAll(squares);

Shape largestPerimeter = Collections.max(shapes, new PerimeterComparator());
Shape largestArea = Collections.max(shapes, new AreaComparator());

System.out.printf("\nThe shape with the biggest perimeter is a %s and has has: a perimeter of: %f\n", largestPerimeter.getClass().getSimpleName(), largestPerimeter.getPerimeter());
System.out.printf("The shape with the biggest area is a %s and has has: an area of: %f\n", largestArea.getClass().getSimpleName(), largestArea.getArea());
}

关于java - 尺寸比较(周长/平方周长)等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53239159/

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