Vererbung Octagon von Circle

katchy

Aktives Mitglied
Hallöchen:)
Vielleicht könnt ihr mir ja weiterhelfen.
Meine Aufgabe:
Write a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represend an octagon inscribed into a circle of a given radius (inherited from Circle) and introduce no any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method getSide() for computing Octagon's side. Its area then can be computed according to the formula:
area = (2 + 4/sqrt(2)) * side * side.

Write a test program that creates an array of shapes consisting of several instances of Circ, Rect, and Octagon and displays their area and perimeter.

  • Create a new Octagon object by using the clone() method and compare the two objects using the compareTo() method.
  • Include in your test program the method that returns the largest object in an array of objects. The method header is
    public static Object max(Object[] a)
    All the objects are assumed to be the instances of the Comparable interface.

  • Apply the method max to your array of shapes to find and display the maximum one.
Therefore, your project should include the following classes: Circ, Rect, Shape, Octagon, and Ex1_1 (the main class containing the main() method). Redesign the first two classes to implement the Comparable and Cloneable interfaces.

Die klasse Circ sieht so aus:

Code:
public class Circ extends Shape
{
    private int radius;

    public Circ()
    {
    super();
    radius = 1;
    }

    public Circ(int xLoc, int yLoc, int radius)
    {
    super(xLoc, yLoc);
    this.radius = radius;
    }

    public int getRadius()
    {
    return radius;
    }

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

    @Override
    public void stretchBy(double factor)
    {
    radius = (int)Math.round(radius * factor);
    }

    @Override
    public String toString()
    {
    String str = "CIRCLE \n"
                 + super.toString()
                 + "Radius: " + radius + "\n"
                 + "Area: " + getArea();
    return str;
    }
}

Ich würde jetzt so anfangen:
Code:
public class Octagon extends Circ implements Cloneable, Comparable {
   
    private double side;
   
    Octagon(){ 
        super();
    }
   
    Octagon(int radius){
     super();  
    }
   
    public double getSide(){
       return side;
    }

In den 2. Konstruktor müsste ja eig noch side mit rein... laut Aufgabe soll ich das aber nicht machen, und eig soll ich ja auch keine neuen Variablen initialisieren.... kennt ihr vllt einen Weg nur mithilfe des Radius zu arbeiten? Eig muss ich doch die seiten als neue Variable initialisieren.

Dringend Hilfe :D

Liebe Grüße
Katchy
 

katchy

Aktives Mitglied
ne nicht genau :D da bei mir explizit steht keine neuen Variablen initialisieren, die dürfen das in deren Aufgaben ;) deswegen helfen mir die links nicht weiter
 

JCODA

Top Contributor
In den 2. Konstruktor müsste ja eig noch side mit rein... laut Aufgabe soll ich das aber nicht machen, und eig soll ich ja auch keine neuen Variablen initialisieren.... kennt ihr vllt einen Weg nur mithilfe des Radius zu arbeiten? Eig muss ich doch die seiten als neue Variable initialisieren.

Du kannst einfach die side mit Winkeln und dem Radius ausrechnen, mach dir eine kleine Skizze mit einem Dreieck mit einem 360/8 = 45° Winkel, dann zeichne die Höhe auf der gegenüberliegenden Seite ... dann kann man mit Sinus/Cosinus oder per Pythagoras die fehlende Seite ausrechnen. Beim Konstruktor mit Radius solltest du den Radius noch in der geerbten Variable radius abspeichern.
 

katchy

Aktives Mitglied
also so:
double side = (radius/(360/8));

und der super Konstruktor müsste dann so aussehen:
super(radius, radius, radius)? Da müssen nämlich aufjedenfall 3 Variablen rein
 

thecain

Top Contributor
Nein, super(xLoc, yLoc, radius)... steht ja da...

Und Pythagoras wird nicht gehen, aber du hast ja den Winkel und zwei Seitenlängen

und (radius * tan(22.5°))*2 sollte eine Seite ergeben
 

Neue Themen


Oben