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.
Die klasse Circ sieht so aus:
Ich würde jetzt so anfangen:
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
Liebe Grüße
Katchy
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.
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
Liebe Grüße
Katchy