Hi Leutz,
vielleicht liegt es auch an der Uhrzeit^^, aber ich sollte folgende Klassen erst in ein Interface binden was super geklappt hat und das Interface dann in eine abstrakte Klasse wandeln. Leider schimpft der Compoiler und möchte die abstrakte Klasse in ein Interface wieder setzen^^. Warum?
vielleicht liegt es auch an der Uhrzeit^^, aber ich sollte folgende Klassen erst in ein Interface binden was super geklappt hat und das Interface dann in eine abstrakte Klasse wandeln. Leider schimpft der Compoiler und möchte die abstrakte Klasse in ein Interface wieder setzen^^. Warum?
Code:
import java.util.Random;
import inheritance.miguel.gutierrez.Shape;
public class Circle extends Shape{
Random rondom= new Random();
double a;
public Circle(double a) {
super();
}
public Circle() {
super();
}
public double calculateArea() {
a = rondom.nextDouble();
return Math.PI*Math.sqrt(a);
}
public double calculateScope() {
a = rondom.nextDouble();
return 2*Math.PI*a;
}
@Override
public String toString() {
return "[Circle Flächeninhalt: "+calculateArea()+ "] [Kreisumfang "+ calculateScope()+"]";
}
}
Code:
Random rondom =new Random();
double a;
double b;
public Rectangle(double a, double b) {
super();
this.a = a;
this.b = b;
}
public Rectangle() {
super();
}
public double calculateArea() {
a = rondom.nextDouble();
b = rondom.nextDouble();
return a*b;
}
public double calculateScope() {
a = rondom.nextDouble();
b = rondom.nextDouble();
return (2*(a+b));
}
@Override
public String toString() {
return "[Rechteck: Flächeninhalt: "+ calculateArea()+"] [Umfang: "+calculateScope()+"]";
}
}
Code:
import java.util.Random;
import inheritance.miguel.gutierrez.Shape;
public class Triangle extends Shape{
Random rondom= new Random();
double a;
double b;
double c;
double s;
public Triangle(double a, double b, double c, double s) {
this.a = a;
this.b = b;
this.c = c;
this.s = s;
}
public Triangle() {
super();
}
public double calculateArea() {
a= rondom.nextDouble();
b = rondom.nextDouble();
c= rondom.nextDouble();
s = (a+b+c)/2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
public double calculateScope() {
a= rondom.nextDouble();
b = rondom.nextDouble();
c= rondom.nextDouble();
return a+b+c;
}
@Override
public String toString() {
return "[Dreieck Flächeninhalt "+calculateArea()+"] [ Umfang "+calculateScope()+"]";
}
}
Code:
public abstract class Shape {
double a;
public Shape(double a) {
this.a=a;
}
public Shape() {
}
abstract double calculateArea();
abstract double calculateScope();
}
Code:
import java.util.ArrayList;
import java.util.Random;
import inheritance.miguel.gutierrez.Shape;
public class Start {
public static void main(String[] args) {
ArrayList<Shape> shapes = new ArrayList<>();
Random random = new Random();
int rand = random.nextInt(5);
for (int i = 0; i < rand; i++) {
shapes.add((Shape) new Circle());
}
rand = random.nextInt(5);
for (int i = 0; i < rand; i++) {
shapes.add((Shape) new Rectangle());
}
rand = random.nextInt(5);
for (int i = 0; i < rand; i++) {
shapes.add((Shape) new Triangle());
}
for(int i=0;i<shapes.size();i++) {
System.out.println(shapes.get(i).toString());
}
}
}