From dc70fd4fb3a86a364b0c5f75b98c5281cf1b518a Mon Sep 17 00:00:00 2001 From: SimonDHG <@> Date: Tue, 12 Dec 2023 12:09:26 +0100 Subject: [PATCH] yay contains --- Set.java | 61 +++++++++++++++++++++++++++++----- __SHELL4.java | 7 ++++ package.bluej | 91 ++++++++++++++++++++++++++++++++++++++++++++------- test.java | 7 ++-- 4 files changed, 143 insertions(+), 23 deletions(-) create mode 100644 __SHELL4.java diff --git a/Set.java b/Set.java index 7d524fb..4dd2434 100644 --- a/Set.java +++ b/Set.java @@ -9,7 +9,7 @@ public class Set */ public Set(){ this.first = null; - length = 0; + this.length = 0; this.last= null; } @@ -27,7 +27,7 @@ public class Set return this.length; } - public T get (int n){ + private T get (int n){ Node current = this.first; for ( int i = 0; i < n ; i++ ){ if (current == null)return null; @@ -54,9 +54,11 @@ public class Set this.first = added; this.last = added; } - else{ this.last.next = added; + else{ + this.last.next = added; this.last = added; } + this.length++; } } @@ -76,6 +78,7 @@ public class Set current = current.next; } current.next.next = current.next; + this.length--; } public Set intersection(Set s){ @@ -88,14 +91,56 @@ public class Set return tmp; } + public Set unionUnoptimized(Set s){ + Set tmp = new Set(); + for (int i = 0; i < s.size(); i++){ + tmp.add(s.get(i)); + } + for (int i = 0; i < this.length; i++){ + if (!tmp.contains(this.get(i))) tmp.add(this.get(i)); + } + return tmp; + } - /*public String toString(){ - Node current = this.first; - String tmp = ""; + public Set union(Set s){ + Set tmp = new Set(); + Node current = s.first; while (current != null){ - tmp = tmp + (String)current.wert; + tmp.add(current.wert); + current = current.next; + } + current = this.first; + while (current != null){ + if (!tmp.contains(current.wert)) tmp.add(current.wert); current = current.next; } return tmp; - }*/ + } + + public Set difference(Set s){ + Set tmp = new Set(); + Node current = this.first; + while (current!=null){ + if(!s.contains(current.wert))tmp.add(current.wert); + current = current.next; + } + return tmp; + } + + public boolean subset(Set s){ + Node current = this.first; + while (current != null){ + if (!s.contains(current.wert)) return false; + } + return true; + } + public String toString(){ + Node current = this.first; + String tmp = ""; + while (current != null){ + tmp = tmp + current.wert + ", "; + current = current.next; + } + return tmp; + } } diff --git a/__SHELL4.java b/__SHELL4.java new file mode 100644 index 0000000..dd5feac --- /dev/null +++ b/__SHELL4.java @@ -0,0 +1,7 @@ + +public class __SHELL4 extends bluej.runtime.Shell { +public static void run() throws Throwable { + +test.testset(); + +}} diff --git a/package.bluej b/package.bluej index 9cc9bb8..01099bc 100644 --- a/package.bluej +++ b/package.bluej @@ -1,20 +1,47 @@ #BlueJ package file -editor.fx.0.height=0 -editor.fx.0.width=0 -editor.fx.0.x=0 -editor.fx.0.y=0 +dependency1.from=LinkedList +dependency1.to=Node +dependency1.type=UsesDependency +dependency2.from=List +dependency2.to=Node +dependency2.type=UsesDependency +dependency3.from=Queue +dependency3.to=Node +dependency3.type=UsesDependency +dependency4.from=Stack +dependency4.to=Node +dependency4.type=UsesDependency +dependency5.from=test +dependency5.to=LinkedList +dependency5.type=UsesDependency +dependency6.from=test +dependency6.to=Node +dependency6.type=UsesDependency +dependency7.from=test +dependency7.to=List +dependency7.type=UsesDependency +dependency8.from=test +dependency8.to=Set +dependency8.type=UsesDependency +dependency9.from=Set +dependency9.to=Node +dependency9.type=UsesDependency +editor.fx.0.height=919 +editor.fx.0.width=2238 +editor.fx.0.x=80 +editor.fx.0.y=145 objectbench.height=100 objectbench.width=776 package.divider.horizontal=0.6 package.divider.vertical=0.8003731343283582 package.editor.height=422 package.editor.width=661 -package.editor.x=504 -package.editor.y=60 +package.editor.x=431 +package.editor.y=271 package.frame.height=600 package.frame.width=800 -package.numDependencies=0 -package.numTargets=1 +package.numDependencies=9 +package.numTargets=7 package.showExtends=true package.showUses=true project.charset=UTF-8 @@ -24,9 +51,51 @@ readme.width=48 readme.x=10 readme.y=10 target1.height=70 -target1.name=Node +target1.name=Set target1.showInterface=false target1.type=ClassTarget target1.width=120 -target1.x=70 -target1.y=10 +target1.x=10 +target1.y=250 +target2.height=70 +target2.name=test +target2.showInterface=false +target2.type=ClassTarget +target2.width=120 +target2.x=380 +target2.y=110 +target3.height=70 +target3.name=Node +target3.showInterface=false +target3.type=ClassTarget +target3.width=120 +target3.x=70 +target3.y=10 +target4.height=70 +target4.name=List +target4.showInterface=false +target4.type=ClassTarget +target4.width=120 +target4.x=140 +target4.y=90 +target5.height=70 +target5.name=Queue +target5.showInterface=false +target5.type=ClassTarget +target5.width=120 +target5.x=10 +target5.y=170 +target6.height=70 +target6.name=LinkedList +target6.showInterface=false +target6.type=ClassTarget +target6.width=120 +target6.x=10 +target6.y=90 +target7.height=70 +target7.name=Stack +target7.showInterface=false +target7.type=ClassTarget +target7.width=120 +target7.x=140 +target7.y=170 diff --git a/test.java b/test.java index c3d0aa9..b8a2721 100644 --- a/test.java +++ b/test.java @@ -34,12 +34,11 @@ public class test } public static void testset(){ Set test = new Set(); - test.add(1); - test.add(2); - test.add(3); + for (int i = 0; i<20 ; i++){ + test.add(i); + } System.out.println(test); test.remove(2); System.out.println(test); - } }