Add Assignment 12
This commit is contained in:
parent
47c42b0c6a
commit
760af16e58
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-14">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Assignement12</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -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
|
|
@ -0,0 +1 @@
|
|||
<mxfile host="Electron" modified="2020-11-15T04:09:32.474Z" agent="5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/13.7.9 Chrome/85.0.4183.121 Electron/10.1.3 Safari/537.36" etag="Qsv6C8H1Drkr9tHO2TkY" version="13.7.9" type="device"><diagram id="bmmjwFzcDrLNMM-SirQ1" name="Page-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==</diagram></mxfile>
|
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
|
@ -0,0 +1 @@
|
|||
<mxfile host="Electron" modified="2020-11-15T16:58:23.106Z" agent="5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/13.7.9 Chrome/85.0.4183.121 Electron/10.1.3 Safari/537.36" etag="SDiABLDUwlX9_6uIxOXt" version="13.7.9" type="device"><diagram id="r-wSGhtTe-w-saC_SL-o" name="Page-1">7Vrbbts4EP0aA90HB7rbfvQlSQukW2+drXf7xkiMRIQWDYpO5H79Dk3K1s2XpLKTBQQYhmY0HJE8Z4YzkDr2eJHecrSMvrIA045lBGnHnnQsa+B48C8Va6XwzJ5ShJwESmXuFDPyC2ulobUrEuCkYCgYo4Isi0qfxTH2RUGHOGcvRbNHRotPXaIQVxQzH9Gqdk4CESlt3zV2+s+YhFH2ZNPQdxYoM9aKJEIBe8mp7OuOPeaMCXW1SMeYyr3L9kWNu9lzdzsxjmNxygC/697NBg/231/+QvNb9jQ1Zl7XVV6eEV3pBV+nAsdBoucs1tlGgHIo9xOkB8r8p449ApUGzPSUeEPgkfbEACkSC3lpyhspEf9I9ZWrpX+1kbyepHlhrQX1fBxU0NktV6sStuK+thr86UeWd/Pj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ifxtidwtfdxhOdWObvvFXKGjayKT1yJe/fRHdnS+qvZUqA85R+s7OOCh1qMSoC9wMwRwLS9U0bAtAeVY9pBgDnuS7BldqBAyD20iOVQhNnGG1PLKcc5Eq7ZAOB+4vWr5XwvuubrEfQXCa7pEFAT3LLMGs0JVUbIVbCY4IJZ5VVLVpYCzIPqUSy+HnMZMkMf1kNJvWbpqK5tzcrZ/ImetV5MWxN3H1+ojpN0X7Pb1fw==</diagram></mxfile>
|
Binary file not shown.
After Width: | Height: | Size: 39 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Data {
|
||||
|
||||
private ArrayList<DataDisplay> observers = new ArrayList<DataDisplay>();
|
||||
private ArrayList<Integer> content;
|
||||
|
||||
public Data() {
|
||||
content = new ArrayList<Integer>();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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 ---");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue