2 Problems. Unable to retrieve correct output for area of Triangle, and unsure of what else-if state to use so my input can recognize a square

huangapple go评论68阅读模式
英文:

2 Problems. Unable to retrieve correct output for area of Triangle, and unsure of what else-if state to use so my input can recognize a square

问题

以下是您提供的内容的翻译:

这是我的TestShape文件。如您所见,根据我通过命令行或终端提供的参数数量,它将创建以下对象并调用getDetails()方法。我的第一个问题是,我不确定要在Square上使用什么else-if条件,因为Square和Circle都需要一个用户输入的变量来创建它们的对象。

我的第二个问题是,当我触发三角形的else-if语句,并调用t.getDetails()时,一切都正确返回,除了面积没有值。请查看下面的其他文件。

这是我触发三角形的输出:

输入:java TestShape 10 10 20

输出:

类型:三角形

边1:10

边2:10

边3:20

周长:40.0

面积:-0.0

我多次检查了getArea()方法并查找错误。

英文:

Here is my TestShape file. As you can see depending on how many arguments I provide through the command line or terminal, it will create the following object and call getDetails(). My first problem is that I am unsure on what else-if condition to use for the Square as both, Square and Circle eg. This is for circle -->(args.length == 1) require one user inputted variable to create their objects.

My second problem is that when I trigger the Triangle else-if statement, and it calls the t.getDetails() it returns everything correct except I don't receive a value for the area. Check the other files down below.

Here is my output for the Triangle trigger:

Input: java TestShape 10 10 20 <ENTER>

Output:

Type: Triangle

Side1: 10

Side2: 10

Side3: 20

Perimeter: 40.0

Area: -0.0

//TestShape.java
public class TestShape {
    public static void main(String[] args) {
        if (args.length == 1) {
            Circle c = new Circle(Integer.parseInt(args[0]));
            c.getDetails();
            
        } else if (args.length == 2) {
            Rectangle r = new Rectangle(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
            r.getDetails();
            
        } else if (args.length == 3) {
            Triangle t = new Triangle(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Integer.parseInt(args[2]));
            t.getDetails();
            
        } else if (/* Unsure on what to write here.*/) { 
            Square s = new Square(Integer.parseInt(args[0]));
            s.getDetails();

        } else {
            System.out.println("Invalid Input");
        }
    }
}
//Triangle.java
public class Triangle implements Shape {
    public int side1;
    public int side2;
    public int side3;

    public Triangle(int side1, int side2, int side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }
    public double s = (side1 + side2 + side3)/2;

    @Override
    public double getPerimeter() {
        return side1 + side2 + side3;
    }

    @Override
    public double getArea() {
        return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
    }

    @Override
    public void getDetails() {
        System.out.println("Type: Triangle");
        System.out.println("Side1: " + side1);
        System.out.println("Side2: " + side2);
        System.out.println("Side3: " + side3);
        System.out.println("Perimeter: " + getPerimeter());
        System.out.println("Area: " + getArea());
    }
}
//Circle.java
public class Circle implements Shape {
    public double PI = Math.PI;
    public int radius;

    public Circle(int radius) {
        this.radius = radius;
    }
    @Override
    public double getPerimeter() {
        return 2 * PI * radius;
    }

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

    @Override
    public void getDetails() {
        System.out.println("Type: Circle");
        System.out.println("Radius: " + radius);
        System.out.println("Perimeter: " + getPerimeter());
        System.out.println("Area: " + getArea());
    }
}
public class Square implements Shape {
    public int side;

    public Square(int side) {
        this.side = side;
    }

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

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

    @Override
    public void getDetails() {
        System.out.println("Type: Square");
        System.out.println("Side: " + side);
        System.out.println("Perimeter: " + getPerimeter());
        System.out.println("Area: " + getArea());
    }
}

I looked over the getArea() method multiple times and checked for errors.

答案1

得分: 1

将这段代码

放入getArea方法中。

目前它在字段被设置之前被调用。

英文:

Put this code

public double s = (side1 + side2 + side3)/2;

into the getArea method.

Currently it is being called before the fields are set.

huangapple
  • 本文由 发表于 2023年4月19日 14:38:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定