diff --git a/Assignement12/.classpath b/Assignement12/.classpath new file mode 100644 index 0000000..1cd9d95 --- /dev/null +++ b/Assignement12/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/Assignement12/.project b/Assignement12/.project new file mode 100644 index 0000000..e0eb974 --- /dev/null +++ b/Assignement12/.project @@ -0,0 +1,17 @@ + + + Assignement12 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Assignement12/.settings/org.eclipse.jdt.core.prefs b/Assignement12/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..910a770 --- /dev/null +++ b/Assignement12/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=14 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=14 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=14 diff --git a/Assignement12/Lab12-Part-A.drawio b/Assignement12/Lab12-Part-A.drawio new file mode 100644 index 0000000..dc85f14 --- /dev/null +++ b/Assignement12/Lab12-Part-A.drawio @@ -0,0 +1 @@ +7Vnfb9owEP5rkLYHJpJASh8htNsDraoxdc8mcROvTg455lf/+p2xnRACLWPNqDQkhOLP54t933c+K255Qbr6KsgsuYOI8pbbiVYtb9Ry3euuj/8KWGvAd7oaiAWLNOSUwIS9UAN2DDpnEc0rhhKASzargiFkGQ1lBSNCwLJq9gS8+tYZiWkNmISE19GfLJKJRvu9Tol/oyxO7JudjulJiTU2QJ6QCJZbkHfT8gIBIPVTugooV7GzcdHjbg/0FhMTNJPHDPh1z10nX4drcv/IbtvtRzIZt22YF4TPzYqHZrpybWOQL1nKSYat4RNkcmJ6HGwTzuIMn0OcBBUILKiQDMM3MB0SZoiGCePRmKxhrqaaSxI+29YwAcFe0C3hxid2C2mU4PoVi4kaiXAHUUFztHmw63d2oDuyqhiOSS4NEALnZJazabGMlIiYZUOQElJjZOKCy6GrgxF3Ch5R/xRSKsUaTewA30TXaN/tmGRYlkoqbJJtFVkREaPeuPBdEowPhuM/4dup8d1W2UkkGba8gRqOa9sVAIZAbogR8EwD4IBMjzLQimCc70BWFJw+yYOSyGckZFk83tiMuiXy3cRBQYBjn/gmaxIWRTRTdILE2WruFFEzwClv4tQb4g8jF3S+9Fo9nHiAbads40+ZCxlAhmshbEMjRWEsqRLHcZy/kkd1JVjmjyTe2r0/726N9xrLnG3Y0yzbzc45ieIUyeK05PSHonzUdmq8e3XevT0cczKl/AFyJhko/0Lb7nB/Nnp77nH09pti19vD7lCVu0+fdVYvAB0odXmO3txUb2x7LznfgCisAN4SRbepvd6viSK4lPaGSrvnX527tF8drOzBJctPz3L/Yxf2/qWuN0buucv69aWqf7R8P3tRr5/jB5ei3lBR7x5JttNpageon+ttTR9s5bjPFS9TgU+xFr9G5iWgNoaITuexHjYF4JRk1hDntmX7n24Z26miXCcytQo/fTdx3+X04HUb0lf3cnpojNxjS0VTe0fvcnr496eHNyTRP/Ppwakn/M1K0izKa1wjOFD3J9iaclBVfIiQKfOOr5u3TL1//3ZJo9rNyk4hxvfBXIT0tQmbSOARI6YnBH5fpAXlRLJFdXL7Qm3cPcAmF+wpoV89JBRurQe9JjNo+z5mx0/B8iFHes01RxviiyX+hRbqG8TH1oJ/pBQOfZdrQAtur8qhu/t152gx7DhyPP+dxIDN8o5Rm5cXtd7Nbw== \ No newline at end of file diff --git a/Assignement12/Lab12-Part-A.png b/Assignement12/Lab12-Part-A.png new file mode 100644 index 0000000..66de72f Binary files /dev/null and b/Assignement12/Lab12-Part-A.png differ diff --git a/Assignement12/Lab12-Part-B.drawio b/Assignement12/Lab12-Part-B.drawio new file mode 100644 index 0000000..c515d4a --- /dev/null +++ b/Assignement12/Lab12-Part-B.drawio @@ -0,0 +1 @@ +7Vrbbts4EP0aA90HB7rbfvQlSQukW2+drXf7xkiMRIQWDYpO5H79Dk3K1s2XpLKTBQQYhmY0HJE8Z4YzkDr2eJHecrSMvrIA045lBGnHnnQsa+B48C8Va6XwzJ5ShJwESmXuFDPyC2ulobUrEuCkYCgYo4Isi0qfxTH2RUGHOGcvRbNHRotPXaIQVxQzH9Gqdk4CESlt3zV2+s+YhFH2ZNPQdxYoM9aKJEIBe8mp7OuOPeaMCXW1SMeYyr3L9kWNu9lzdzsxjmNxygC/697NBg/231/+QvNb9jQ1Zl7XVV6eEV3pBV+nAsdBoucs1tlGgHIo9xOkB8r8p449ApUGzPSUeEPgkfbEACkSC3lpyhspEf9I9ZWrpX+1kbyepHlhrQX1fBxU0NktV6sStuK+thr86UeWd/PjpzCD6T1Z9v1v825GG8RDrIcGN7E7RN/TcObfTuar+YymPWWn9+4WswUWfA3jXnawm57GkmOKBHkuzg1pqoXbsVt3U0Zg1paho6Kv3WQx4TlFD2pJelAe0pIfyzniSK254gguckvcqTaMeQV7vEuwp0EmuB+OCaZVRNDpvZEKtll0ZPcvS4XeZROJYw5yqaRrXBmmdTCfSGGKOYF1Yt58jhl8PGbZJWaVCXFyknHtK6PnDSxb/ZtFt8Zledav8OwUgiUwK1HDOk0zMzPJyb+dgfbjfwJP3IvxxCllINt6I0/c/hFHzTHj0DGfY8ZXlE6QQBOSLClaV3iSvJAFRTFIo0cWi5m+IxFHlIQxXPsAsEwXo2fMBYGqcKhvCLYErR8RGtyhNVtJLIFB/lMmjSLGyS9wiwoE02nO8goWMzlSJyWOE7CZZtwySypYU8HwDiVCK3xGKVom5GG7jAXsOIlHTAi2KOS8GurKFeI0p6qyLztqSoklKzrqyBnlauO+t5+bBTK8Fnmrgnx3U4CnP5RmKB1o0uUJAOsVG2A4e8JjRpk8GGKmGAGZoKTKSEHxo9hLiWSJfBKHdxubibPTfNcbIVUMxj7STTKKSBDgWMLJBHBVYSeBWso42eyUO4If7OdY1s8uTHwMsrmT4SfNuRizGNaCyAZZDMR4wZIcNZgfDKDjRNDIW95pwFvnAt4+fhhQssFOYZx1cOabAF4AVBTvEL2XgE+6ZgV1u4q6XYMwRQ+YTllCBGHSP1e2JeTfC1zXOjGqz4StU4MtLNZYLQMk8Kc/VFg/M3DSxnWz0Gcwv1tcDyrYjxgPMG9P8+ZPc+etx3mmaxx9s1rJtWn9SKQ0fmafK62b1WKtzesXwt4+MbKbyOuHut8c9m1Gbz6jlxs0qzc4Dfftu4vGga/v0CDg0T0bYU0AHKjYl5xoY3+L/8FI+pC9Wu2M217tfOBesFernfC+Xi3EcisErDY71+WWqJ2SRJMvLeSutOf/RWhyal/nnKuyr75t/kwSwUKOFm0hcP5C4P1bu+pbwvYQOBIr/5vOru7NXJvYLwL9uzd21cReW8S3ifxtidwtfdxhOdWObvvFXKGjayKT1yJe/fRHdnS+qvZUqA85R+s7OOCh1qMSoC9wMwRwLS9U0bAtAeVY9pBgDnuS7BldqBAyD20iOVQhNnGG1PLKcc5Eq7ZAOB+4vWr5XwvuubrEfQXCa7pEFAT3LLMGs0JVUbIVbCY4IJZ5VVLVpYCzIPqUSy+HnMZMkMf1kNJvWbpqK5tzcrZ/ImetV5MWxN3H1+ojpN0X7Pb1fw== \ No newline at end of file diff --git a/Assignement12/Lab12-Part-B.png b/Assignement12/Lab12-Part-B.png new file mode 100644 index 0000000..f07cddb Binary files /dev/null and b/Assignement12/Lab12-Part-B.png differ diff --git a/Assignement12/bin/A.class b/Assignement12/bin/A.class new file mode 100644 index 0000000..cce7344 Binary files /dev/null and b/Assignement12/bin/A.class differ diff --git a/Assignement12/bin/B.class b/Assignement12/bin/B.class new file mode 100644 index 0000000..31a0a2d Binary files /dev/null and b/Assignement12/bin/B.class differ diff --git a/Assignement12/bin/BorderDataDisplay.class b/Assignement12/bin/BorderDataDisplay.class new file mode 100644 index 0000000..28fee5b Binary files /dev/null and b/Assignement12/bin/BorderDataDisplay.class differ diff --git a/Assignement12/bin/C.class b/Assignement12/bin/C.class new file mode 100644 index 0000000..a2c500b Binary files /dev/null and b/Assignement12/bin/C.class differ diff --git a/Assignement12/bin/Data.class b/Assignement12/bin/Data.class new file mode 100644 index 0000000..d8e2b65 Binary files /dev/null and b/Assignement12/bin/Data.class differ diff --git a/Assignement12/bin/DataDisplay.class b/Assignement12/bin/DataDisplay.class new file mode 100644 index 0000000..8196c7d Binary files /dev/null and b/Assignement12/bin/DataDisplay.class differ diff --git a/Assignement12/bin/HistogramDataDisplay.class b/Assignement12/bin/HistogramDataDisplay.class new file mode 100644 index 0000000..66578d3 Binary files /dev/null and b/Assignement12/bin/HistogramDataDisplay.class differ diff --git a/Assignement12/bin/MaxDataDisplay.class b/Assignement12/bin/MaxDataDisplay.class new file mode 100644 index 0000000..a30bb41 Binary files /dev/null and b/Assignement12/bin/MaxDataDisplay.class differ diff --git a/Assignement12/bin/PartA.class b/Assignement12/bin/PartA.class new file mode 100644 index 0000000..41edfb8 Binary files /dev/null and b/Assignement12/bin/PartA.class differ diff --git a/Assignement12/bin/PartB.class b/Assignement12/bin/PartB.class new file mode 100644 index 0000000..f36092e Binary files /dev/null and b/Assignement12/bin/PartB.class differ diff --git a/Assignement12/src/A.java b/Assignement12/src/A.java new file mode 100644 index 0000000..562e42e --- /dev/null +++ b/Assignement12/src/A.java @@ -0,0 +1,21 @@ + +public class A { + private int dataA; + public static boolean debug; + + public A(int a) { + if (A.debug) { + System.out.println("constructor A is being called!"); + } + this.dataA = a; + } + + public void f() { + + System.out.println("method f() from class A is being called"); + } + + public int g() { + return this.dataA + 1; + } +} diff --git a/Assignement12/src/B.java b/Assignement12/src/B.java new file mode 100644 index 0000000..0544b39 --- /dev/null +++ b/Assignement12/src/B.java @@ -0,0 +1,23 @@ + +public class B extends A { + private int dataB; + + public B(int a, int b) { + super(a); + if (A.debug) { + System.out.println("constructor B is being called!"); + } + this.dataB = b; + } + + @Override + public void f() { + + System.out.println("method f() from class B is being called"); + } + + @Override + public int g() { + return 2 * super.g() + dataB; + } +} diff --git a/Assignement12/src/BorderDataDisplay.java b/Assignement12/src/BorderDataDisplay.java new file mode 100644 index 0000000..29de8e9 --- /dev/null +++ b/Assignement12/src/BorderDataDisplay.java @@ -0,0 +1,15 @@ + +public class BorderDataDisplay extends DataDisplay { + + public BorderDataDisplay(String name, Data subject) { + super(name, subject); + } + + @Override + public void update() { + System.out.println("*".repeat(25)); + super.update(); + System.out.println("*".repeat(25)); + } + +} diff --git a/Assignement12/src/C.java b/Assignement12/src/C.java new file mode 100644 index 0000000..a3d4ce6 --- /dev/null +++ b/Assignement12/src/C.java @@ -0,0 +1,22 @@ + +public class C extends B { + private int dataC; + + public C(int a, int b, int c) { + super(a, b); + if (A.debug) { + System.out.println("constructor C is being called!"); + } + this.dataC = c; + } + + @Override + public void f() { + System.out.println("method f() from class B is being called"); + } + + @Override + public int g() { + return super.g() - 10 + dataC * 3; + } +} diff --git a/Assignement12/src/Data.java b/Assignement12/src/Data.java new file mode 100644 index 0000000..2c770df --- /dev/null +++ b/Assignement12/src/Data.java @@ -0,0 +1,40 @@ +import java.util.ArrayList; +import java.util.Arrays; + +public class Data { + + private ArrayList observers = new ArrayList(); + private ArrayList content; + + public Data() { + content = new ArrayList(); + } + + public int[] getContent() { + int[] result = new int[content.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = content.get(i); + } + return result; + } + + public void addToContent(int x) { + content.add(x); + System.out.println("\nData: " + x + " is added."); + notifyAllObservers(); + } + + public String toString() { + return Arrays.toString(content.toArray()); + } + + public void attach(DataDisplay observer) { + observers.add(observer); + } + + public void notifyAllObservers() { + for (DataDisplay observer : observers) { + observer.update(); + } + } +} diff --git a/Assignement12/src/DataDisplay.java b/Assignement12/src/DataDisplay.java new file mode 100644 index 0000000..adfbc67 --- /dev/null +++ b/Assignement12/src/DataDisplay.java @@ -0,0 +1,19 @@ + +public class DataDisplay { + String name; + private Data dataToBeDisplayed; + + public DataDisplay(String name, Data subject) { + dataToBeDisplayed = subject; + this.name = name; + } + + public int[] getContent() { + return dataToBeDisplayed.getContent(); + } + + public void update() { + System.out.println(getClass() + ": " + name); + System.out.println("\t" + dataToBeDisplayed); + } +} diff --git a/Assignement12/src/HistogramDataDisplay.java b/Assignement12/src/HistogramDataDisplay.java new file mode 100644 index 0000000..7bfabff --- /dev/null +++ b/Assignement12/src/HistogramDataDisplay.java @@ -0,0 +1,25 @@ +import java.util.Arrays; + +public class HistogramDataDisplay extends DataDisplay { + + public HistogramDataDisplay(String name, Data subject) { + super(name, subject); + } + + @Override + public void update() { + System.out.println(getClass() + ": " + name); + int[] sortedArray = super.getContent(); + Arrays.sort(sortedArray); + int lastVal = -1; + for (int i : sortedArray) { + if (i == lastVal) + System.out.print("*"); + else { + System.out.format("%s%d\t*",(lastVal==-1)?"":"\n", i); + lastVal=i; + } + } + System.out.println(); + } +} diff --git a/Assignement12/src/MaxDataDisplay.java b/Assignement12/src/MaxDataDisplay.java new file mode 100644 index 0000000..2fffae2 --- /dev/null +++ b/Assignement12/src/MaxDataDisplay.java @@ -0,0 +1,15 @@ + +public class MaxDataDisplay extends DataDisplay { + int maxValue=0; + public MaxDataDisplay(String name, Data subject) { + super(name, subject); + } + @Override + public void update() { + System.out.println(getClass() + ": " + name); + for(int i : super.getContent()) this.maxValue = Math.max(this.maxValue, i); + System.out.println(this.maxValue); + + } + +} diff --git a/Assignement12/src/PartA.java b/Assignement12/src/PartA.java new file mode 100644 index 0000000..c535284 --- /dev/null +++ b/Assignement12/src/PartA.java @@ -0,0 +1,63 @@ + +public class PartA { + public static void main(String[] args) { + testConstructor(); + A a = new A(2); + A b = new B(3, 4); + A c = new C(5, 6, 7); + testF(a, b, c); + testG(a, b, c); + testA(); + testB(); + testC(); + } + private static void testF(A a, A b, A c) { + System.out.println("--- Test f() of A, B, C ---"); + a.f(); + System.out.println("------------------------------"); + b.f(); + System.out.println("------------------------------"); + c.f(); + System.out.println("------------------------------"); + } + private static void testG(A a, A b, A c) { + System.out.println("--- Test g() of A, B, C ------"); + System.out.println(a.getClass() + ": " + a.g()); + System.out.println("------------------------------"); + System.out.println(b.getClass() + ": " + b.g()); + System.out.println("------------------------------"); + System.out.println(c.getClass() + ": " + c.g()); + System.out.println("------------------------------"); + } + public static void testConstructor() { + A.debug = true; + A a = new A(2); + System.out.println("------------------------------"); + A b = new B(3, 4); + System.out.println("------------------------------"); + A c = new C(5, 6, 7); + System.out.println("------------------------------"); + A.debug = false; + } + + public static void testA() { + System.out.format("new A(2).g() -> %d, [expected] -> %d\n", new A(2).g(), 3); + System.out.format("new A(-1).g() -> %d, [expected] -> %d\n", new A(-1).g(), 0); + System.out.format("new A(-10).g() -> %d, [expected] -> %d\n", new A(-10).g(), -9); + System.out.format("new A(0).g() -> %d, [expected] -> %d\n", new A(0).g(), 1); + } + + public static void testB() { + System.out.format("new B(0, 0).g() -> %d, [expected] -> %d\n", new B(0, 0).g(), 2); + System.out.format("new B(-2, 3).g() -> %d, [expected] -> %d\n", new B(-2, 3).g(), 1); + System.out.format("new B(2, -3).g() -> %d, [expected] -> %d\n", new B(2, -3).g(), 3); + System.out.format("new B(4, 5).g() -> %d, [expected] -> %d\n", new B(4, 6).g(), 16); + } + + public static void testC() { + System.out.format("new C(0, 0, 0).g() -> %d, [expected] -> %d\n", new C(0, 0, 0).g(), -8); + System.out.format("new C(2, 4, 3).g() -> %d, [expected] -> %d\n", new C(2, 4, 3).g(), 9); + System.out.format("new C(-1, -3, -5).g() -> %d, [expected] -> %d\n", new C(-1, -3, 5).g(), 2); + System.out.format("new C(5, 2, 10).g() -> %d, [expected] -> %d\n", new C(5, 2, 10).g(), 34); + } +} diff --git a/Assignement12/src/PartB.java b/Assignement12/src/PartB.java new file mode 100644 index 0000000..64421e2 --- /dev/null +++ b/Assignement12/src/PartB.java @@ -0,0 +1,26 @@ +import java.util.Scanner; + +public class PartB { + public static void main(String[] args) { + Data d = new Data(); + DataDisplay display = new DataDisplay("General Display", d); + DataDisplay maxDisplay = new MaxDataDisplay("MAX-Display", d); + DataDisplay histogram = new HistogramDataDisplay("Histogram", d); + DataDisplay border = new BorderDataDisplay("Display-with-border", d); + d.attach(display); + d.attach(maxDisplay); + d.attach(histogram); + d.attach(border); + + Scanner kbd = new Scanner(System.in); + while (true) { + System.out.println("------------------------------"); + System.out.print("Enter an integer >= 0: "); + int x = kbd.nextInt(); + if (x < 0) + break; + d.addToContent(x); + } + System.out.println("--- FINISH ---"); + } +}