Skip to content
Snippets Groups Projects
Commit fd826096 authored by Daniel Meißner's avatar Daniel Meißner
Browse files

Added lesson 1 exercise 4.

parent b680bc64
No related branches found
No related tags found
No related merge requests found
public class Aufgabenblatt1Aufgabe4DreidimensionaleKoerper {
public static void main(String[] args) {
// Test Klassenhierarchie
// - erstelle zwei unterschiedliche Typen von Koerpern
// - Test der Funktionalität
Koerper[] koerper = new Koerper[2];
koerper[0] = new Kugel( 0, 0, 0, 2 );
koerper[1] = new Quader( 0, 0, 0, 2, 2, 2 );
for ( int i = 0; i < koerper.length; i++ ) {
System.out.println("----- Neuer Koerper -----\n");
// Beschaffenheit des Koerpers nach Erstellung
koerper[i].getMetaDaten();
System.out.println("");
// Veraenderung des Koerpers
System.out.println("Verändere Körper mit Farbe = gruen, verschiebe horizontal und vertikal um 2 und skaliere um 1.");
koerper[i].setFarbe("gruen");
koerper[i].verschiebeHorizontal(2);
koerper[i].verschiebeVertikal(2);
koerper[i].skaliere(1);
System.out.println("");
// Beschaffenheit des Koerpers nach Veraenderungen
koerper[i].getMetaDaten();
System.out.println("");
// Test ob ein bestimmter Punkt innerhalb des Koerpers liegt oder nicht
System.out.println("P(1,4,4) befindet sich im Koerper: " + koerper[i].istImKoerper(1, 4 , 4));
System.out.println("\n----- Koerper Ende -----\n");
}
}
}
\ No newline at end of file
public abstract class Koerper {
// . z
// .
// .
// .
// .
// .
// . +>>>>>>>>>>>>+
// +./|.........../| y
// . +<<<<<<<<<<<<+ |
// . c| +>>>>>>>>>>|>+
// . |/ a |/
// . +<<<<<<<<<<<<+
// . x b
private double x;
private double y;
private double z;
private String farbe = "black";
public Koerper( double x, double y, double z ) {
this.x = x;
this.y = y;
this.z = z;
}
// abstract methods
public abstract double berechneOberflaeche();
public abstract double berechneVolumen();
public abstract String getParameter();
public abstract void skaliere(double alpha);
public abstract boolean istImKoerper( double x, double y, double z );
// final methods
public final String istEin() {
if( this instanceof Quader ) {
return "Quader";
} else {
return "Kugel";
}
}
public final void verschiebeHorizontal(double schritte) {
this.y = this.y + schritte;
}
public final void verschiebeVertikal(double schritte) {
this.z = this.z + schritte;
}
public final void verschiebeTiefe(double schritte) {
this.x = this.x + schritte;
}
public final void setzeNeuePostion( double x, double y, double z ) {
this.x = x;
this.y = y;
this.z = z;
}
// public methods
public void setFarbe(String farbe) {
this.farbe = farbe;
}
public String getFarbe() {
return this.farbe;
}
public String getPosition() {
return "x-Koordinate " + this.x + " y-Koordinate " + this.y + " z-Koordinate " + this.z;
}
public void getMetaDaten() {
System.out.println("Art: " + this.istEin());
System.out.println("Lage: " + this.getPosition());
System.out.println("Parameter: " + this.getParameter());
System.out.println("Volumen: " + this.berechneVolumen());
System.out.println("Oberfläche: " + this.berechneOberflaeche());
System.out.println("Farbe: " + this.getFarbe());
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public double getZ() {
return this.z;
}
}
\ No newline at end of file
public class Kugel extends Koerper {
private double radius;
public Kugel( double x, double y, double z, double radius ) {
super(x, y, z);
this.radius = radius;
}
public double berechneOberflaeche() {
return 4 * Math.PI * Math.pow(radius, 2);
}
public double berechneVolumen() {
return (4 * Math.PI * Math.pow(radius, 3))/3;
}
public String getParameter() {
return this.istEin() + " mit dem Radius " + radius;
}
public boolean istImKoerper( double x, double y, double z ) {
if ( (Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)) <= Math.pow(radius, 2) ) {
return true;
} else {
return false;
}
}
public void skaliere(double alpha) {
radius = radius + alpha;
}
}
\ No newline at end of file
public class Quader extends Koerper {
private double a;
private double b;
private double c;
public Quader( double x, double y, double z, double a, double b, double c ) {
super(x, y, z);
this.a = a;
this.b = b;
this.c = c;
}
public double berechneOberflaeche() {
return 2 * a * b + 2 * a * c + 2 * b * c;
}
public double berechneVolumen() {
return a * b * c;
}
public String getParameter() {
return this.istEin() + " mit den Maßen " + this.a + " x " + this.b + " x " + this.c ;
}
// TODO: implement the right algorithm to check that a point is inside a square
public boolean istImKoerper( double x, double y, double z ) {
if ( (x <= this.getX() + a) || (x >= this.getX() + a) ) {
return false;
} else if ( (y <= this.getY() + b) || (y >= this.getY() + b) ) {
return false;
} else if ( (z <= this.getZ() + c) || (z >= this.getZ() + c) ) {
return false;
} else {
return true;
}
}
public void skaliere(double alpha) {
a = a + alpha;
b = b + alpha;
c = c + alpha;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment