Problem początkującego z zadaniem

0

Witam
Mam problem z zadaniem, które wydaje się proste, ale nie potrafię sobie poradzić.
Stworzyłem klasy figur geometrycznych (kwadrat, prostokąt, koło) i tutaj pojawia się problem:

  • mam wypisać wszystkie figury które znajdują się na liscie/tablicy
  • znaleŹć figurę z największym polem
    Prosiłbym o pomoc


class Circle extends Shape {
    private int radius;

    public Circle(int radius) {
        this.radius = radius;
    }

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

    @Override
    public double getCircumference() {
        return 2 * Math.PI * radius;
    }

    @Override
    public String toString() {
        return "Circle (" + radius + ")";
    }

}


class Rectangle extends Shape {
    private int a, b;

    public Rectangle(int a, int b) {
        this.a = a;
        this.b = b;
    }

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

    @Override
    public double getCircumference() {
        return 2 * (a + b);
    }

    @Override
    public String toString() {
        return "Rectangle (" + a + ", " + b + ")";
    }
}


class Triangle extends Shape {
    private int side;

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

    @Override
    public double getArea() {
        return Math.sqrt(3) * side * side / 4.0;
    }

    @Override
    public double getCircumference() {
        return 3 * side;
    }

    @Override
    public String toString() {
        return "Triangle (" + side + ")";
    }

}


public class Square extends Shape{

        private int side;

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

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

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

        @Override
        public String toString() {
            return "Square (" + side + ")";
        }

    }


abstract class Shape {

    public abstract double getArea();
    public abstract double getCircumference();


0
MalarzAr napisał(a):
  • mam wypisać wszystkie figury które znajdują się na liscie/tablicy

Póki co, nie masz żadnej listy/tablicy z figurami.

0

Panowie edytowałem kod, Moim problem jest implementacja kodu, który wypisuje figurę z największym polem i figury znajdujace sie na liscie

0

Wstawiłem przykładową listę ale jak wypisać figurę z największym polem i figury znajdujace sie na liscie??


public class ShapeTester {

    public static void main(String[] args) {

        Shape[] shapes = new Shape[4];

        shapes[0] = new Circle(4);
        shapes[1] = new Rectangle(4, 3);
        shapes[2] = new Triangle(4);
        shapes[3] = new Square(4);

        for (int i = 0; i < shapes.length; i++) {

            System.out.println("Area is:" + shapes[i].getArea());


        }
    }
}

1

@MalarzAr:

A co takiego w "Shape" jest, że masz dziedziczenie? Może lepiej interfejs? Tu masz podobny przykład nawet:

https://www.geeksforgeeks.org/difference-between-abstract-class-and-interface-in-java/

0
MalarzAr napisał(a):

jak wypisać figurę z największym polem??

Instrukcje warunkowe + operatory porównania/relacyjne

2

Zdecydowanie lepiej gdybys korzystal z kolekcji i streamow, bylo by prostsze.

public class ShapeTester {

    public static void main(String[] args) {

        Shape[] shapes = new Shape[4];

        shapes[0] = new Circle(4);
        shapes[1] = new Rectangle(4, 3);
        shapes[2] = new Triangle(4);
        shapes[3] = new Square(4);

       Shape maxArea = null;
       Set<String> shapesSet = new HashSet<>();
        for (int i = 0; i < shapes.length; i++) {
            if (maxArea == null || maxArea.getArea() < shapes[i].getArea()) {
                maxArea = shapes[i];
            }
            shapesSet.add(shapes[i].name());
        }
        System.out.println(String.join(", ", shapesSet));
        System.out.println("Max area is:" + maxArea.getArea());
    }
}

static abstract class Shape {

        public abstract double getArea();

        public abstract double getCircumference();

        public abstract String name();
    }

Piszę w okienku komentarza na telefonie, wiec moga byc bledy :)

0

@PanamaJoe: Shape ma być klasą abstrakcyjną i dlatego jest dziedziczenie.
@Krzysztof Pluciński tak wyłapałem bład:). Ale jak wypisać wszystkie figury?

1 użytkowników online, w tym zalogowanych: 0, gości: 1