1 / 7

Java-Kurs - 8. Übung

Java-Kurs - 8. Übung. Besprechung der Hausaufgabe. Hausaufgabe 6: Polygon. public class Polygon{ //Attribute //Array der Stützpunkte private Punkt[] Punkte; //1. Konstruktor; erhält Array von Punkten public Polygon(Punkt[] p) { //Erzeugen desw Array

mignon
Télécharger la présentation

Java-Kurs - 8. Übung

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java-Kurs - 8. Übung • Besprechung der Hausaufgabe

  2. Hausaufgabe 6: Polygon public class Polygon{ //Attribute //Array der Stützpunkte private Punkt[] Punkte; //1. Konstruktor; erhält Array von Punkten public Polygon(Punkt[] p) { //Erzeugen desw Array Punkte = new Punkt[p.length]; //Erzeugen der Punkte implements Array for(int i = 0 ; i < p.length ; i = i + 1) { Punkte[i] = new Punkt(p[i]); } } //2. Konstruktor, erhält Array von x-Koordinaten und Array von y-Koordinaten public Polygon(double[] x_Koordinaten , double[] y_Koordinaten) { //Erzeugen des Array Punkte = new Punkt[x_Koordinaten.length]; //Erzeugen der Punkte des Array for(int i = 0 ; i < x_Koordinaten.length ; i = i + 1) { Punkte[i] = new Punkt(x_Koordinaten[i] , y_Koordinaten[i]); } } void verschiebe(double d_x, double d_y) { for (int i=0; i < Punkte.length; i= i+1) { Punkte[i].verschiebe(d_x, d_y); } }

  3. Hausaufgabe 6: Polygon (Fortsetzung) //Berechnet Umfang double umfang(){ double umfang = 0; for (int i = 0 ; i < Punkte.length - 1 ; i = i + 1) { umfang = umfang + Punkte[i].abstand(Punkte[i+1]); } umfang = umfang + Punkte[Punkte.length - 1].abstand(Punkte[0]); return umfang; } void ausgabe() { for (int i=0; i<Punkte.length; i= i + 1) { Punkte[i].ausgabe(); } } //Berechnet Flächeninhalt double flaeche() { double flaeche=0; double f=0; for (int i=0; i < Punkte.length-1; i++) { f = f +(( Punkte[i].x- Punkte[i+1].x)*( Punkte[i].y+ Punkte[i+1].y)); } f = f +(( Punkte[Punkte.length-1].x- Punkte[0].x)* //Die Schleife schliesst das ( Punkte[Punkte.length-1].y+ Punkte[0].y)); //Polygon nicht, da wieder //auf den 1. Pkt zugegriffen flaeche = f/2; //werden muss. deshalb muss return Math.abs(flaeche); //noch 1 Schritt addiert werden. }

  4. Hausaufgabe 6: Polygon (Fortsetzung) //liefert true, wenn korrekten Polygon vorliegt. boolean gueltig(){ //mindestens 3 Stützpunkte if(Punkte.length < 3) return false; //Referenzen im Array ungleich Null for(int i = 0 ; i < Punkte.length ; i = i + 1) { if( Punkte[i] == null) return false; } //keine doppelten Stützpunkte for(int i = 0 ; i < Punkte.length ; i = i + 1){ for(int j = i+1 ; j < Punkte.length ; j = j + 1){ if(Punkte[i].gleich(Punkte[j])) return false; } } //ToDo: Schnittfreiheit, kein Stützpunkt auf Kante return true; } }

  5. Hausaufgabe 6: Polygon (Fortsetzung) public static void main (String args[]) { //Test des 1. Konstruktors Punkt[] punktArray = new Punkt[7]; punktArray[0] = new Punkt(3.9 , 6.1); punktArray[1] = new Punkt(3.9 , 6.1); punktArray[2] = new Punkt(4.1 , 1.9); punktArray[3] = new Punkt(2.2 , 4.6); punktArray[4] = new Punkt(1.9 , 4.5); punktArray[5] = new Punkt(4.8 , 2.3); punktArray[6] = new Punkt(9.9 , 5.1); Polygon p1 = new Polygon(punktArray); System.out.println("Test des 1. Konstruktors"); p1.ausgabe(); p1.verschiebe(3,9); System.out.println("Verschieben um 3/9"); p1.ausgabe(); System.out.println("Umfang:" + p1.umfang()); System.out.println("Flächeninhalt: " + p1.flaeche()); System.out.println("Gültig: " + p1.gueltig()); //Test des 2. Konstruktors double[] x = {0 , 0 , 1 , 1}; double[] y = {0 , 1 , 1 , 0}; Polygon p2 = new Polygon(x,y); System.out.println("Test des 2. Konstruktors"); p2.ausgabe(); p2.verschiebe(3,9); System.out.println("Verschieben um 3/9"); p2.ausgabe(); System.out.println("Umfang:" + p2.umfang()); System.out.println("Flächeninhalt: " + p2.flaeche()); System.out.println("Gültig: " + p2.gueltig()); } }

  6. Hausaufgabe 6: Punkt public class Punkt { double x; //x-Koordinate double y; //y-Koordinate /** Creates new Punkt */ public Punkt(double xstartwert, double ystartwert){ x = xstartwert; y = ystartwert; } public Punkt(Punkt p){ x = p.x; y = p.y; } double abstand(Punkt p){ return Math.sqrt( Math.pow(p.x - x,2) + Math.pow(p.y - y,2) ); } boolean gleich(Punkt p){ return x == p.x && y == p.y; } void verschiebe (double dx, double dy) { x = x + dx; y = y + dy; } void ausgabe() { System.out.print("x = "); System.out.print(x); System.out.print(" , y = "); System.out.println(y); } } } double flaeche() { return Math.PI*halbachse_a*halbachse_b; } }

  7. Hausaufgabe 7 • Entwerfen Sie die Klasse Student, die alle zur Verwaltung eines Studentenregisters wesentlichen Informationen beinhaltet. • Welche Methoden können Sie sich dazu vorstellen? • Welche Informationen (Variablen & Methoden) sind auch auf Dozenten oder Verwaltungsangestellte anwendbar?

More Related