diff --git a/sources/.checkstyle b/sources/.checkstyle index 069d5e6..3a5dd87 100644 --- a/sources/.checkstyle +++ b/sources/.checkstyle @@ -1,6 +1,6 @@ - + diff --git a/sources/.project b/sources/.project index 09e671d..801ddf8 100644 --- a/sources/.project +++ b/sources/.project @@ -10,11 +10,6 @@ - - net.sf.eclipsecs.core.CheckstyleBuilder - - - org.eclipse.m2e.core.maven2Builder @@ -24,6 +19,5 @@ org.eclipse.m2e.core.maven2Nature org.eclipse.jdt.core.javanature - net.sf.eclipsecs.core.CheckstyleNature diff --git a/sources/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java b/sources/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java index eaf2be4..8fd4f0b 100644 --- a/sources/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java +++ b/sources/src/main/java/pr2/vererbung/singleton_einfach/Singleton.java @@ -1,26 +1,28 @@ package pr2.vererbung.singleton_einfach; - public class Singleton { + public final class Singleton { - private static String name; - Singleton sl = new Singleton("Alleine"); + private String name; - public Singleton(String name) { - if (sl == null) { - this.setName(name); - } else { - getInfo(); - } - } - public String getName() { - return name; - } - public void setName(String name) { - Singleton.name = name; - } - - public String getInfo() { - return "Der Singleton exsistiert bereits"; - } + public static final Singleton SL = new Singleton(); + + private Singleton() { + } + + private Singleton(String name) { + this.name = name; + } + + public static String getName() { + return SL.name; + } + + public static Singleton getInstance(String name) { + if (SL == null) { + return new Singleton(name); + } else { + return SL; + } + } } diff --git a/sources/src/main/java/pr2/vererbung/singleton_einfach/SingletonTest.java b/sources/src/main/java/pr2/vererbung/singleton_einfach/SingletonTest.java index 2426806..531452e 100644 --- a/sources/src/main/java/pr2/vererbung/singleton_einfach/SingletonTest.java +++ b/sources/src/main/java/pr2/vererbung/singleton_einfach/SingletonTest.java @@ -1,15 +1,31 @@ package pr2.vererbung.singleton_einfach; +import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.Test; public class SingletonTest { + @SuppressWarnings("static-access") @Test public void testVomSingleton() { - Singleton sl = new Singleton("Walter"); - assertEquals("Walter", sl.getName()); + + assertEquals("Alleine", Singleton.getName()); + + assertEquals("Alleine", Singleton.getInstance("Alleine").getName()); + + assertFalse(false); + } + + public void testNeueInstanz() { + + Singleton NN = Singleton.getInstance("Hubert"); + + assertEquals("Hubert", Singleton.getName()); + + } } diff --git a/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Amoebe.java b/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Amoebe.java index 02ea122..0042b99 100644 --- a/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Amoebe.java +++ b/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Amoebe.java @@ -1,5 +1,13 @@ package pr2.vererbung.ueberschreiben_lebewesen; - public class Amoebe { + public class Amoebe extends Lebewesen { + public Amoebe() { + + } + + @Override + public void bewegen() { + System.out.println("Schleimen"); + } } diff --git a/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Dackel.java b/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Dackel.java index 1c5fd8a..fdce359 100644 --- a/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Dackel.java +++ b/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Dackel.java @@ -1,5 +1,17 @@ package pr2.vererbung.ueberschreiben_lebewesen; - public class Dackel { + public class Dackel extends Lebewesen { + public Dackel() { + + } + + @Override + public void bewegen() { + System.out.println("Auf kurzen Beinen vorwärts wackeln"); + } + + public void bellen() { + System.out.println("Wau, wau"); + } } diff --git a/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Lebewesen.java b/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Lebewesen.java index acde914..9888077 100644 --- a/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Lebewesen.java +++ b/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Lebewesen.java @@ -2,4 +2,11 @@ package pr2.vererbung.ueberschreiben_lebewesen; public class Lebewesen { + public Lebewesen() { + + } + + public void bewegen() { + + } } diff --git a/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Main.java b/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Main.java index ee3f3ba..e269a1d 100644 --- a/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Main.java +++ b/sources/src/main/java/pr2/vererbung/ueberschreiben_lebewesen/Main.java @@ -3,6 +3,26 @@ package pr2.vererbung.ueberschreiben_lebewesen; public class Main { public static void main(String[] args) { - // TODO: Lebewesen testen + + + Lebewesen l1 = new Dackel(); + Lebewesen l2 = new Amoebe(); + + l1.bewegen(); + l2.bewegen(); + + if ((l1) instanceof Dackel) { + ((Dackel) l1).bellen(); + } else { + System.out.println("l1 ist noch keine Instanz von Dackel"); + } + + if (((Dackel) l1) instanceof Dackel) { + ((Dackel) l1).bellen(); + } else { + System.out.println("l1 ist keine Instanz von Dackel"); + } + + } } diff --git a/sources/src/main/java/pr2/vererbung/vararg_summe/Summator.java b/sources/src/main/java/pr2/vererbung/vararg_summe/Summator.java index f38b0ac..a10f74c 100644 --- a/sources/src/main/java/pr2/vererbung/vararg_summe/Summator.java +++ b/sources/src/main/java/pr2/vererbung/vararg_summe/Summator.java @@ -2,4 +2,16 @@ package pr2.vererbung.vararg_summe; public class Summator { + public Summator() { + + } + + public int sum(int...a) { + int b = 0; + for (int i = 0; i < a.length; i++) { + b = b + a[i]; + } + return b; + } + } diff --git a/sources/src/main/java/pr2/vererbung/vararg_summe/test/SummatorTest.java b/sources/src/main/java/pr2/vererbung/vararg_summe/test/SummatorTest.java index 2b8ab32..6fa32f2 100644 --- a/sources/src/main/java/pr2/vererbung/vararg_summe/test/SummatorTest.java +++ b/sources/src/main/java/pr2/vererbung/vararg_summe/test/SummatorTest.java @@ -9,16 +9,16 @@ public class SummatorTest { @Test void testSum() { -// TODO: Einkommentieren -// Summator s = new Summator(); -// assertEquals(3, s.sum(1, 2)); -// assertEquals(1, s.sum(-1, 2)); -// assertEquals(6, s.sum(1, 2, 3)); -// assertEquals(0, s.sum(1, 2, -3)); -// assertEquals(10, s.sum(1, 2, 3, 4)); -// assertEquals(2, s.sum(1, 2, 3, -4)); -// assertEquals(15, s.sum(1, 2, 3, 4, 5)); -// assertEquals(5, s.sum(1, 2, 3, 4, -5)); -// assertEquals(36, s.sum(1, 2, 3, 4, 5, 6, 7, 8)); + + Summator s = new Summator(); + assertEquals(3, s.sum(1, 2)); + assertEquals(1, s.sum(-1, 2)); + assertEquals(6, s.sum(1, 2, 3)); + assertEquals(0, s.sum(1, 2, -3)); + assertEquals(10, s.sum(1, 2, 3, 4)); + assertEquals(2, s.sum(1, 2, 3, -4)); + assertEquals(15, s.sum(1, 2, 3, 4, 5)); + assertEquals(5, s.sum(1, 2, 3, 4, -5)); + assertEquals(36, s.sum(1, 2, 3, 4, 5, 6, 7, 8)); } }