package uebungen; /** *

Title: Java-Übungen

*

Description: Übungsaufgaben zu "Informatik IIb"

*

Company: Fachhochschule Jena

* @author Oliver Jack * @version 1.0 */ abstract class GrafikElement { protected double [] x; abstract public void draw(); } /******************************************************************************/ class Punkt extends GrafikElement { public Punkt() {} public Punkt(double [] x) { this.x = x; } public void draw() { System.out.println("Zeichne Punkt an " + x[0] + "," + x[1]); } } /******************************************************************************/ class Linie extends Punkt { protected double [] y; public Linie() {} public Linie(double [] x, double [] y) { super(x); this.y = y; } public void draw() { System.out.println("Zeichne Linie von " + x[0] + "," + x[1] + " bis " + y[0] + "," + y[1]); } } /******************************************************************************/ class Rechteck extends Linie { public Rechteck(double [] x, double [] y) { super(x, y); } public void draw() { System.out.println("Zeichne Rechteck an " + x[0] + "," + x[1] + " bis " + y[0] + "," + y[1]); } } /******************************************************************************/ class Kreis extends Punkt { protected int radius; public Kreis() {} public Kreis(double [] x, int radius) { super(x); this.radius = radius; } public void draw() { System.out.println("Zeichne Kreis an " + x[0] + "," + x[1] + " mit Radius " + radius); } } /******************************************************************************/ class Dreieck extends Linie { protected double [] z; public Dreieck(double [] x, double [] y, double [] z) { super(x, y); this.z = z; } public void draw() { System.out.println("Zeichne Dreieck von " + x[0] + "," + x[1] + " über " + y[0] + "," + y[1] + " bis " + z[0] + "," + z[1] ); } } /******************************************************************************/ class GrafikElementContainer { protected GrafikElement [] elemente; protected int counter; public GrafikElementContainer(int n) { elemente = new GrafikElement[n]; counter = 0; } public void add(GrafikElement x) { elemente[counter] = x; counter++; } public void draw() { for(int i = 0; i < counter; i++) elemente[i].draw(); } } /******************************************************************************/ interface Element { public String toString(); public boolean equals(Element other); } /******************************************************************************/ abstract class Container { abstract public void add(Element e); abstract public int size(); abstract public Element get(int i); abstract public String toString(); abstract public void remove(int i); } /******************************************************************************/ class Vector extends Container { protected Element [] elemente; protected int counter; public Vector(int n) { elemente = new Element[n]; counter = 0; } public void add(Element e) { elemente[counter++] = e; } public int size() { return counter; } public Element get(int i) { return elemente[i]; } public String toString() { String s = ""; for(int i=0; i < counter; i++) s = s + elemente[i].toString() + " "; return s; } public void remove(int i) { // Ursprüngliche Reihenfolge wird NICHT beibehalten elemente[i] = elemente[counter - 1]; counter--; } public void remove(Element x) { int i = 0; while(i < counter) { if (x.equals(elemente[i])) remove(i); else i++; } } } /******************************************************************************/ class IntElement implements Element { protected int value; public IntElement(int x) { value = x; } public String toString() { return Integer.toString(value); } public boolean equals(Element other) { return value == ((IntElement)other).value; } } /******************************************************************************/ public class Uebung4 { /** * Constructor von Übung 4 */ public Uebung4() { } /** * Lösung von Aufgabe 1 */ public void aufgabe1() { double [] x1 = { 23, 56 }; double [] x2 = { 203, 11 }; double [] x3 = { 230, 33 }; double [] x4 = { 231, 46 }; double [] x5 = { 13, 506 }; double [] x6 = { 3, 56 }; double [] x7 = { 2, 6 }; GrafikElementContainer container = new GrafikElementContainer(42); Kreis kreis = new Kreis(x1, 12); container.add(kreis); container.add(new Punkt(x4)); container.add(new Linie(x3, x5)); container.add(new Dreieck(x6, x1, x4)); container.add(new Rechteck(x4, x7)); container.add(new Kreis(x4, 45)); container.draw(); } /** * Lösung von Aufgabe 2 */ public void aufgabe2() { Vector vector = new Vector(1000); for(int i = 0; i < 12; i++) vector.add(new IntElement(i + 42)); System.out.println(vector.toString()); vector.remove(7); System.out.println(vector.toString()); vector.remove(new IntElement(50)); System.out.println(vector.toString()); } public static void main(String [] args) { Uebung4 uebung4 = new Uebung4(); uebung4.aufgabe1(); uebung4.aufgabe2(); } }