Skip to content

Instantly share code, notes, and snippets.

@3c7
Last active August 29, 2015 14:19
Show Gist options
  • Save 3c7/698eefa0f070e4dfa6f5 to your computer and use it in GitHub Desktop.
Save 3c7/698eefa0f070e4dfa6f5 to your computer and use it in GitHub Desktop.
Datenstrukturen und Algorithmen - Übungsblatt 3 - Aufgabe 1
/**
* Created by Nils on 13.04.2015.
*/
public abstract class Koerper implements Skalierbar {
// Bezugspunkt
public Punkt p;
public void verschiebe(double dx, double dy, double dz) {
this.p = new Punkt(p.x + dx, p.y + dy, p.z + dz);
}
public abstract void skaliere(double a);
public abstract boolean enthaelt(Punkt a);
public abstract double volumen();
public abstract double oberflaeche();
public abstract double durchmesser();
static boolean between(double a, double min, double max) {
if (a >= min && a <= max) return true;
return false;
}
}
/**
* Created by Nils on 21.04.2015.
*/
public class KoerperCompVol implements Skalierbar, Comparable {
private Koerper k;
public KoerperCompVol(Koerper k) {
this.k = k;
}
public void skaliere(double a) {
this.k.skaliere(a);
}
public void verschiebe(double dx, double dy, double dz) {
this.k.verschiebe(dx, dy, dz);
}
public boolean enthaelt(Punkt a) {
return this.k.enthaelt(a);
}
public double volumen() {
return this.k.volumen();
}
public double oberflaeche() {
return this.k.oberflaeche();
}
public double durchmesser() {
return this.k.durchmesser();
}
public int compareTo(KoerperCompVol a) {
if (this.k.volumen() < a.volumen()) return -1;
if (this.k.volumen() > a.volumen()) return 1;
return 0;
}
public int compareTo(Object o) {
double v = ((KoerperCompVol) o).volumen();
if (this.k.volumen() < v) return -1;
if (this.k.volumen() > v) return 1;
return 0;
}
}
/**
* Created by Nils on 13.04.2015.
*/
public class Kugel extends Koerper implements Skalierbar {
// Radius
private double r;
// Konstruktor
public Kugel(Punkt p, double r) {
this.p = new Punkt(p.x, p.y, p.z);
this.r = r;
}
// Methoden
public void skaliere(double a) {
this.r = this.r * a;
}
public boolean enthaelt(Punkt a) {
if (Math.sqrt(Math.pow(this.p.x - a.x,2) + Math.pow(this.p.y - a.y,2) + Math.pow(this.p.z - a.z,2)) <= this.r)
return true;
return false;
}
public double volumen() {
return (4.0 / 3.0) * Math.PI * Math.pow(this.r, 3);
}
public double oberflaeche() {
return 4 * Math.PI * Math.pow(this.r, 2);
}
public double durchmesser() {
return 2 * this.r;
}
}
/**
* Created by Nils on 13.04.2015.
*/
public class Punkt {
protected double x, y, z;
public Punkt(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public void verschiebe(double dx, double dy, double dz) {
this.x = x + dx;
this.y = y + dy;
this.z = z + dz;
}
}
/**
* Created by Nils on 13.04.2015.
*/
public class Quader extends Koerper implements Skalierbar {
// Breite, Tiefe, H�he
private double b, t, h;
public Quader(Punkt p, double b, double t, double h) {
this.p = new Punkt(p.x, p.y, p.z);
this.b = b;
this.t = t;
this.h = h;
}
// Methoden
public void skaliere(double a) {
this.b = this.b * a;
this.t = this.t * a;
this.h = this.h * a;
}
public boolean enthaelt(Punkt a) {
if (between(a.x, this.p.x, this.p.x + this.b) && between(a.y, this.p.y, this.p.y + this.t) && between(a.z, this.p.z, this.p.z + this.h))
return true;
return false;
}
public double volumen() {
return this.b * this.t * this.h;
}
public double oberflaeche() {
return 2 * this.b * this.t + 2 * this.b * this.h + 2 * this.t * this.h;
}
public double durchmesser() {
return Math.sqrt(Math.pow(this.b, 2) + Math.pow(this.t, 2) + Math.pow(this.h, 2));
}
}
/**
* Created by Nils on 13.04.2015.
*/
public interface Skalierbar {
void skaliere(double a);
}
public class Sort {
// Sortieren durch Auswaehlen
// vgl. "Einfuehrung in die Programmierung"
public static void sortiere(int[] a) {
int tmp;
int imin;
// finde ab jeder Position i das kleinste verbleibende Element
for (int i = 0; i < a.length - 1; i++) {
// zunaechst ist das Element an Position i das kleinste
imin = i;
// suche ab Position i+1 nach kleineren Elementen
for (int j = i + 1; j < a.length; j++) {
// wenn ein kleineres gefunden wurde
if (a[j] < a[imin]) {
// merke mir dessen Position
imin = j;
}
}
// Invariante:
// Element an Position imin ist kleinstes Element der Positionen i bis a.length-1
// vertausche Elemente an den Positionen i und imin
tmp = a[i];
a[i] = a[imin];
a[imin] = tmp;
// Invariante:
// die i+1 kleinsten Elemente stehen aufsteigend sortiert an Positionen 0 bis i
}
}
public static void sortiere(Comparable[] a) {
Comparable tmp;
int min;
for (int i=0; i<a.length-1;i++) {
min = i;
for(int j = i+1;j<a.length;j++) {
if(a[j].compareTo(a[min]) == -1)
min = j;
}
tmp = a[i];
a[i] = a[min];
a[min] = tmp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment