Vararg und Vererbung

kleinerRech
Kai Sellmann 2023-05-30 14:31:38 +02:00
parent 8e305277a7
commit 09a63d8d9c
10 changed files with 114 additions and 43 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<fileset-config file-format-version="1.2.0" simple-config="false" sync-formatter="true"> <fileset-config file-format-version="1.2.0" simple-config="false" sync-formatter="false">
<local-check-config name="maven-checkstyle-plugin validate" location="file:/C:/Users/KTS%20Nitro%205/git/uebungen/sources/checkstyle.xml" type="remote" description="maven-checkstyle-plugin configuration validate"> <local-check-config name="maven-checkstyle-plugin validate" location="file:/C:/Users/KTS%20Nitro%205/git/uebungen/sources/checkstyle.xml" type="remote" description="maven-checkstyle-plugin configuration validate">
<property name="checkstyle.header.file" value="C:\Users\KTS Nitro 5\eclipse-workspace\PR2\.metadata\.plugins\org.eclipse.core.resources\.projects\sources\com.basistech.m2e.code.quality.checkstyleConfigurator\checkstyle-header-validate.txt"/> <property name="checkstyle.header.file" value="C:\Users\KTS Nitro 5\eclipse-workspace\PR2\.metadata\.plugins\org.eclipse.core.resources\.projects\sources\com.basistech.m2e.code.quality.checkstyleConfigurator\checkstyle-header-validate.txt"/>
<property name="checkstyle.cache.file" value="${project_loc}/target/checkstyle-cachefile"/> <property name="checkstyle.cache.file" value="${project_loc}/target/checkstyle-cachefile"/>

View File

@ -10,11 +10,6 @@
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand>
<name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand> <buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name> <name>org.eclipse.m2e.core.maven2Builder</name>
<arguments> <arguments>
@ -24,6 +19,5 @@
<natures> <natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature> <nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
<nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
</natures> </natures>
</projectDescription> </projectDescription>

View File

@ -1,26 +1,28 @@
package pr2.vererbung.singleton_einfach; package pr2.vererbung.singleton_einfach;
public class Singleton { public final class Singleton {
private static String name; private String name;
Singleton sl = new Singleton("Alleine");
public Singleton(String name) { public static final Singleton SL = new Singleton();
if (sl == null) {
this.setName(name); private Singleton() {
} else {
getInfo();
}
}
public String getName() {
return name;
}
public void setName(String name) {
Singleton.name = name;
}
public String getInfo() {
return "Der Singleton exsistiert bereits";
}
}
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;
}
}
} }

View File

@ -1,15 +1,31 @@
package pr2.vererbung.singleton_einfach; package pr2.vererbung.singleton_einfach;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test; import org.junit.Test;
public class SingletonTest { public class SingletonTest {
@SuppressWarnings("static-access")
@Test @Test
public void testVomSingleton() { 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());
} }
} }

View File

@ -1,5 +1,13 @@
package pr2.vererbung.ueberschreiben_lebewesen; package pr2.vererbung.ueberschreiben_lebewesen;
public class Amoebe { public class Amoebe extends Lebewesen {
public Amoebe() {
}
@Override
public void bewegen() {
System.out.println("Schleimen");
}
} }

View File

@ -1,5 +1,17 @@
package pr2.vererbung.ueberschreiben_lebewesen; 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");
}
} }

View File

@ -2,4 +2,11 @@ package pr2.vererbung.ueberschreiben_lebewesen;
public class Lebewesen { public class Lebewesen {
public Lebewesen() {
}
public void bewegen() {
}
} }

View File

@ -3,6 +3,26 @@ package pr2.vererbung.ueberschreiben_lebewesen;
public class Main { public class Main {
public static void main(String[] args) { 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");
}
} }
} }

View File

@ -2,4 +2,16 @@ package pr2.vererbung.vararg_summe;
public class Summator { 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;
}
} }

View File

@ -9,16 +9,16 @@ public class SummatorTest {
@Test @Test
void testSum() { void testSum() {
// TODO: Einkommentieren
// Summator s = new Summator(); Summator s = new Summator();
// assertEquals(3, s.sum(1, 2)); assertEquals(3, s.sum(1, 2));
// assertEquals(1, s.sum(-1, 2)); assertEquals(1, s.sum(-1, 2));
// assertEquals(6, s.sum(1, 2, 3)); assertEquals(6, s.sum(1, 2, 3));
// assertEquals(0, s.sum(1, 2, -3)); assertEquals(0, s.sum(1, 2, -3));
// assertEquals(10, s.sum(1, 2, 3, 4)); assertEquals(10, s.sum(1, 2, 3, 4));
// assertEquals(2, s.sum(1, 2, 3, -4)); assertEquals(2, s.sum(1, 2, 3, -4));
// assertEquals(15, s.sum(1, 2, 3, 4, 5)); assertEquals(15, s.sum(1, 2, 3, 4, 5));
// assertEquals(5, 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)); assertEquals(36, s.sum(1, 2, 3, 4, 5, 6, 7, 8));
} }
} }