diff --git a/PR2_UIB/src/Console.java b/PR2_UIB/src/Console.java new file mode 100644 index 0000000..4d0cc5a --- /dev/null +++ b/PR2_UIB/src/Console.java @@ -0,0 +1,29 @@ +import java.util.Scanner; +public class Console { + + private static Scanner input; + + private Console() { + + } + public static String readString() { + + System.out.println("Text eingeben: "); + String text = input.nextLine(); + return "Gelesener Text" + text; + } + public static char[] readCharArray(){ + System.out.println("Text eingeben: "); + char [] ca = input.next().toCharArray(); + System.out.println("Gelesenes char-Feld: "); + for(char celement: ca) + System.out.print(celement); + System.out.println(); + return ca; + } + public static boolean readBoolean() { + System.out.println("Boolean eingeben: "); + boolean b = input.nextBoolean(); + return b ; + } +} diff --git a/PR2_UIB/src/InOutPut_Fortsetzung/Buchungen.java b/PR2_UIB/src/InOutPut_Fortsetzung/Buchungen.java new file mode 100644 index 0000000..9d404ed --- /dev/null +++ b/PR2_UIB/src/InOutPut_Fortsetzung/Buchungen.java @@ -0,0 +1,39 @@ +package InOutPut_Fortsetzung; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; + +public class Buchungen { + + public static void main(String[] args) throws IOException { + int buchungsNummer = 0; + + String buchungsKonto; + double buchungsBetrag = 0; + + int wertUnter = 0; + int wertUeber = 0; + ArrayList werteListe = new ArrayList <> (); + + BufferedReader reader = new BufferedReader(new FileReader("src/InOutPut_Fortsetzung/buchungen.text")); + String zeile = reader.readLine(); + + while (zeile != null) { + String [] wert = zeile.split(" "); + zeile = reader.readLine(); + System.out.println(zeile); + + for(String w : wert) { + werteListe.add(w); + } + + } + if (wertUnter < 1000) { + + } + + } + } + diff --git a/PR2_UIB/src/InOutPut_Fortsetzung/buchungen.text b/PR2_UIB/src/InOutPut_Fortsetzung/buchungen.text new file mode 100644 index 0000000..b64acdc --- /dev/null +++ b/PR2_UIB/src/InOutPut_Fortsetzung/buchungen.text @@ -0,0 +1,7 @@ +1 22.03.2024 "Einzahlung" 1000.00 +2 22.03.2024 "Einzahlung" 50.00 +3 23.03.2024 "Einkauf Edeka" -45.45 +4 24.03.2024 "Gehalt" 1200.00 +5 31.03.2024 "Mietzins Wohnung" -1100.00 +6 31.03.2024 "Nebenkostenpauschale Wohnung" -250.00 +7 04.04.2024 "Einkauf Netto" 42.02 diff --git a/PR2_UIB/src/Uebung_15_04/Alien.java b/PR2_UIB/src/Uebung_15_04/Alien.java new file mode 100644 index 0000000..a7179f9 --- /dev/null +++ b/PR2_UIB/src/Uebung_15_04/Alien.java @@ -0,0 +1,96 @@ +package Uebung_15_04; + +/** + * Ein Alien. + */ +public class Alien implements Cloneable { + + /** + * Name des Aliens. + */ + private final String name; + + /** + * Raumanzug des Aliens. + */ + private Raumanzug raumanzug; + + /** + * Erzeugt ein neues Alien. + * + * @param name Name des Aliens. + * @param raumanzug Anzug. + */ + public Alien(String name, Raumanzug raumanzug) { + this.name = name; + this.raumanzug = raumanzug; + } + + /** + * Gibt den Namen des Aliens zurück. + * + * @return Name des Aliens. + */ + public String getName() { + return name; + } + + /** + * Gibt den Anzug zurück. + * + * @return der Anzug. + */ + public Raumanzug getAnzug() { + return raumanzug; + } + + /** + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((raumanzug == null) ? 0 : raumanzug.hashCode()); + return result; + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Alien other = (Alien) obj; + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + if (raumanzug == null) { + return other.raumanzug == null; + } else { + return raumanzug.equals(other.raumanzug); + } + } + + @Override + protected Alien clone() throws CloneNotSupportedException { + Alien alien1 = (Alien) super.clone(); + alien1.raumanzug = (Raumanzug) super.clone(); + + return alien1; + } + +} \ No newline at end of file diff --git a/PR2_UIB/src/Uebung_15_04/AlienCloneTest.java b/PR2_UIB/src/Uebung_15_04/AlienCloneTest.java new file mode 100644 index 0000000..a38a71c --- /dev/null +++ b/PR2_UIB/src/Uebung_15_04/AlienCloneTest.java @@ -0,0 +1,35 @@ +package Uebung_15_04; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; + +/** + * Test für die Clone-Methoden. + */ +public class AlienCloneTest { + + /** + * Test-Methode. + * + * @throws CloneNotSupportedException wird geworfen, wenn clone_alien nicht + * korrekt implementiert wurde. + */ + @Test + void testClone() throws CloneNotSupportedException { + // TODO: Einkommentieren (strg+shift+c) + Raumanzug r1 = new Raumanzug(); + Alien a1 = new Alien("Predator", r1); + + Alien a2 = (Alien) a1.clone(); + Raumanzug r2 = a2.getAnzug(); + + assertNotSame(a1, a2); + assertNotSame(r1, r2); + + assertEquals(a1, a2); + assertEquals(r1, r2); + assertEquals(r1.getSauerstoffVorrat(), r2.getSauerstoffVorrat(), 0.0001); + } +} \ No newline at end of file diff --git a/PR2_UIB/src/Uebung_15_04/BufferedReader_Writer.java b/PR2_UIB/src/Uebung_15_04/BufferedReader_Writer.java new file mode 100644 index 0000000..c68b843 --- /dev/null +++ b/PR2_UIB/src/Uebung_15_04/BufferedReader_Writer.java @@ -0,0 +1,38 @@ +package Uebung_15_04; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + + +public class BufferedReader_Writer { + + public static void main(String[] args) throws IOException { + + BufferedReader reader = new BufferedReader(new FileReader("src/Uebung_15_04/Mondnacht.txt")); + int i = 0; + int counter = 0; + int wortAnzahl = 0; + + String zeile = reader.readLine(); + + while (zeile != null) { + i++; + System.out.println("Zeile " + i + ": " + zeile); + + String[] w = zeile.split(" "); + wortAnzahl += w.length; + + for (String wort : w) { + counter += wort.length(); + } + zeile = reader.readLine(); + + } + reader.close(); + System.out.println("Anzahl der Wörter: " + wortAnzahl); + System.out.println("Anzahl der Buchstaben: " + counter); + System.out.println("Anzahl der Zeilen: " + i); + } + +} diff --git a/PR2_UIB/src/Uebung_15_04/DoubleFormatter.java b/PR2_UIB/src/Uebung_15_04/DoubleFormatter.java new file mode 100644 index 0000000..a2ff189 --- /dev/null +++ b/PR2_UIB/src/Uebung_15_04/DoubleFormatter.java @@ -0,0 +1,25 @@ +package Uebung_15_04; + +public class DoubleFormatter { + + public static void printDouble(double zahl, int nachkommastellen) { + int multiplikator = 1; + for (int i = 0; i <= nachkommastellen; i++) { + multiplikator *= 10; + + } + zahl = zahl * (nachkommastellen * multiplikator) / (nachkommastellen * multiplikator); + System.out.println("Ausgabe: " + zahl); + } + + public static void main(String[] args) { + printDouble(1.0, 1); // Erwartete Ausgabe: 1.0 + printDouble(10.1, 1); // Erwartete Ausgabe: 10.1 + printDouble(2.01, 2); // Erwartete Ausgabe: 2.01 + printDouble(2.006, 2); // Erwartete Ausgabe: 2.01 + printDouble(2.0001, 2); // Erwartete Ausgabe: 2.00 + printDouble(2.0005, 3); // Erwartete Ausgabe: 2.001 + printDouble(123.456, 1); // Erwartete Ausgabe: 123.4 + printDouble(9876.54321, 3); // Erwartete Ausgabe: 9876.543 + } +} \ No newline at end of file diff --git a/PR2_UIB/src/Uebung_15_04/MainSerialisierung.java b/PR2_UIB/src/Uebung_15_04/MainSerialisierung.java new file mode 100644 index 0000000..306f59a --- /dev/null +++ b/PR2_UIB/src/Uebung_15_04/MainSerialisierung.java @@ -0,0 +1,10 @@ +package Uebung_15_04; + +public class MainSerialisierung { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/PR2_UIB/src/Uebung_15_04/Mondnacht.txt b/PR2_UIB/src/Uebung_15_04/Mondnacht.txt new file mode 100644 index 0000000..a3f8a6c --- /dev/null +++ b/PR2_UIB/src/Uebung_15_04/Mondnacht.txt @@ -0,0 +1,5 @@ +Mondnacht +Es war, als hätt' der Himmel +Die Erde still geküsst, +Dass sie im Blütenschimmer +Von ihm nun träumen müsst. \ No newline at end of file diff --git a/PR2_UIB/src/Uebung_15_04/Product.java b/PR2_UIB/src/Uebung_15_04/Product.java new file mode 100644 index 0000000..9f9ce14 --- /dev/null +++ b/PR2_UIB/src/Uebung_15_04/Product.java @@ -0,0 +1,45 @@ +package Uebung_15_04; + +import java.io.Serializable; + +public class Product implements Serializable { + /** + * + */ + private static final long serialVersionUID = 1L; + private String name; + private String description; + private double price; + + public Product() {} + + public Product(String name, String description, double price) { + this.name = name; + this.description = description; + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } +} \ No newline at end of file diff --git a/PR2_UIB/src/Uebung_15_04/Raumanzug.java b/PR2_UIB/src/Uebung_15_04/Raumanzug.java new file mode 100644 index 0000000..215c291 --- /dev/null +++ b/PR2_UIB/src/Uebung_15_04/Raumanzug.java @@ -0,0 +1,72 @@ +package Uebung_15_04; + +/** + * Ein Raumanzug. + */ +public class Raumanzug implements Cloneable { + + /** + * Sauerstoffvorrat, der noch im Raumanzug ist. + */ + private double sauerstoffVorrat; + + /** + * Ertzeugt einen neuen Raumanzug. + */ + public Raumanzug() { + sauerstoffVorrat = Math.random(); + } + + /** + * Sauerstoffvorrat im Anzug. + * + * @return Vorrat in % (0.0-1.0) + */ + public double getSauerstoffVorrat() { + return sauerstoffVorrat; + } + + /** + * Tankt den Anzug auf. + */ + public void auftanken() { + sauerstoffVorrat = 1.0; + } + + /** + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(sauerstoffVorrat); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Raumanzug other = (Raumanzug) obj; + return Double.doubleToLongBits(sauerstoffVorrat) == Double.doubleToLongBits(other.sauerstoffVorrat); + } + + @Override + protected Raumanzug clone() throws CloneNotSupportedException { + return (Raumanzug) super.clone(); + } + +} \ No newline at end of file