From 3cfcc9d9c1e01a04101d7cd7e7918fac49543bac Mon Sep 17 00:00:00 2001 From: student Date: Wed, 1 Apr 2026 12:41:08 +0200 Subject: [PATCH] Erster JUnitTest --- PR2pvl/src/Knoten.java | 37 ++++++++++++++++++++++++++++++---- PR2pvl/src/LinkedList.java | 4 ++-- PR2pvl/src/LinkedListTest.java | 26 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 PR2pvl/src/LinkedListTest.java diff --git a/PR2pvl/src/Knoten.java b/PR2pvl/src/Knoten.java index e90c7ac..d655bca 100644 --- a/PR2pvl/src/Knoten.java +++ b/PR2pvl/src/Knoten.java @@ -1,6 +1,7 @@ public class Knoten { + private static final Exception IndexOutOfBoundsException = null; private int wert; private Knoten nachfolger; @@ -19,13 +20,41 @@ public class Knoten { - public void auslesenAnPos(int pos) { - if(pos >0 && this.nachfolger != null) { - nachfolger.auslesenAnPos(pos--); + public int auslesenAnPos(int pos) throws Exception { + pos--; + if(nachfolger==null && pos>1) { + throw IndexOutOfBoundsException; + + } + if(pos >0 ) { + nachfolger.auslesenAnPos(pos); + } + else { + return this.wert; + } + + + + return 0; + + } + + + public void loeschenAnPos(int pos) throws Exception{ + if(this.nachfolger==null) { + throw IndexOutOfBoundsException; + } + if(pos>1) { + this.nachfolger = nachfolger.nachfolger; }else { - System.out.println(this.wert); + pos --; } } + + public int getWert() {return this.wert;} + + + // End of Class } diff --git a/PR2pvl/src/LinkedList.java b/PR2pvl/src/LinkedList.java index 59d2d8f..7bc867f 100644 --- a/PR2pvl/src/LinkedList.java +++ b/PR2pvl/src/LinkedList.java @@ -11,9 +11,9 @@ public class LinkedList { kopf.einfuegenAmEnde(wert); } - public void auslesenAnPos(int pos) throws Exception { + public int auslesenAnPos(int pos) throws Exception { - kopf.auslesenAnPos(pos); + return kopf.auslesenAnPos(pos); } public void loeschenAnPos(int pos) throws Exception { diff --git a/PR2pvl/src/LinkedListTest.java b/PR2pvl/src/LinkedListTest.java new file mode 100644 index 0000000..9aae55f --- /dev/null +++ b/PR2pvl/src/LinkedListTest.java @@ -0,0 +1,26 @@ +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.Assert; +import org.junit.function.ThrowingRunnable; +import org.junit.jupiter.api.Test; + +class LinkedListTest { + + private static final ThrowingRunnable IndexOutOfBoundsException = null; + + @Test + void test() throws Exception { + LinkedList L = new LinkedList(11); + L.einfuegenAmEnde(10); + L.einfuegenAmEnde(9); + L.einfuegenAmEnde(8); + L.einfuegenAmEnde(7); + + + + LinkedList R = new LinkedList(10); + R.einfuegenAmEnde(3); + Assert.assertEquals(3, R.auslesenAnPos(2)); + } + +}