Compare commits

..

17 Commits

44 changed files with 760 additions and 24 deletions

10
Assignement12/.classpath Normal file
View File

@ -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>

17
Assignement12/.project Normal file
View File

@ -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>

View File

@ -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

View File

@ -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

View File

@ -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/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==</diagram></mxfile>

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

BIN
Assignement12/bin/A.class Normal file

Binary file not shown.

BIN
Assignement12/bin/B.class Normal file

Binary file not shown.

Binary file not shown.

BIN
Assignement12/bin/C.class Normal file

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.

21
Assignement12/src/A.java Normal file
View File

@ -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;
}
}

23
Assignement12/src/B.java Normal file
View File

@ -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;
}
}

View File

@ -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));
}
}

22
Assignement12/src/C.java Normal file
View File

@ -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() {
super.f();
System.out.println("method f() from class C is being called");
}
@Override
public int g() {
return super.g() - 10 + dataC * 3;
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,16 @@
public class HistogramDataDisplay extends DataDisplay {
public HistogramDataDisplay(String name, Data subject) {
super(name, subject);
}
@Override
public void update() {
System.out.println(getClass() + ": " + name);
for (int i = 0; i < super.getContent().length; i++)
System.out.format("%s%d\t%s", (i == 0) ? "" : "\n", super.getContent()[i],
"*".repeat(super.getContent()[i]));
System.out.println();
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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 ---");
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -13,29 +13,17 @@ public class RecursiveExercise {
int head = MyArrayUtil.head(ht);
return MyArrayUtil.addAsFirst(head, l);
}
public static int[] reverse(int[] ht) throws Exception {
// fill your code here
if(ht == null) return null;
if (ht == null)
return null;
if (ht.length == 1)
return ht.clone();
int temp = ht[0];
ht[0] = ht[ht.length - 1];
ht[ht.length - 1] = temp;
return recursiveInnerSandwich(reverse(MyArrayUtil.tail(removeLast(ht, new int[ht.length-1], 0))),ht.clone());
}
//This method make an array sandwich excluding the bread (index 0 and index last)
public static int[] recursiveInnerSandwich(int[] src,int[] dest) throws Exception{
if (src==null)return dest;
dest[dest.length-src.length-1] = src[0];
recursiveInnerSandwich(MyArrayUtil.tail(src),dest);
return dest;
}
//Remove the last element
public static int[] removeLast(int[] ia1,int[] ia2,int i) {
ia2[i]=ia1[i];
if (i==ia2.length-1)return ia2;
return removeLast(ia1,ia2,i+1);
return append(reverse(MyArrayUtil.tail(ht)),new int[] {MyArrayUtil.head(ht)});
}
public static boolean isIn(int x, int[] a) throws Exception {
// fill your code here
if (a[0] == x)
@ -58,12 +46,15 @@ public class RecursiveExercise {
public static int[] shift(int[] ht, int n) throws Exception {
// fill your code here
if(ht==null)return null;
if(n==0)return ht;
if (ht == null)
return null;
if (n == 0)
return ht;
int temp = ht[0];
ht = reverse(MyArrayUtil.addAsFirst(temp, reverse(MyArrayUtil.tail(ht))));
return shift(ht,n-1);
return shift(ht, n - 1);
}
public static void main(String[] args) throws Exception {
// test append, when appending empty array with {1,2,3}
int[] a = null;

View File

@ -0,0 +1,162 @@
import java.util.Arrays;
public class RecursiveExerciseSandwich {
// return an array that has all data of ht and n,
// just like concatenating ht and n.
public static int[] append(int[] ht, int[] n) throws Exception {
if (ht == null)
return n;
if (n == null)
return ht;
int[] l = append(MyArrayUtil.tail(ht), n);
int head = MyArrayUtil.head(ht);
return MyArrayUtil.addAsFirst(head, l);
}
public static int[] reverse(int[] ht) throws Exception {
// fill your code here
if(ht == null) return null;
if (ht.length == 1)
return ht.clone();
int temp = ht[0];
ht[0] = ht[ht.length - 1];
ht[ht.length - 1] = temp;
return recursiveInnerSandwich(reverse(MyArrayUtil.tail(removeLast(ht, new int[ht.length-1], 0))),ht.clone());
}
//This method make an array sandwich excluding the bread (index 0 and index last)
public static int[] recursiveInnerSandwich(int[] src,int[] dest) throws Exception{
if (src==null)return dest;
dest[dest.length-src.length-1] = src[0];
recursiveInnerSandwich(MyArrayUtil.tail(src),dest);
return dest;
}
//Remove the last element
public static int[] removeLast(int[] ia1,int[] ia2,int i) {
ia2[i]=ia1[i];
if (i==ia2.length-1)return ia2;
return removeLast(ia1,ia2,i+1);
}
public static boolean isIn(int x, int[] a) throws Exception {
// fill your code here
if (a[0] == x)
return true;
if (a.length == 1)
return false;
return isIn(x, MyArrayUtil.tail(a.clone()));
}
public static boolean subArray(int[] a1, int[] a2) throws Exception {
// fill your code here
if (a1 == null)
return true;
if (isIn(a1[0], a2))
return subArray(MyArrayUtil.tail(a1.clone()), a2);
if (a1.length == 1)
return true;
return false;
}
public static int[] shift(int[] ht, int n) throws Exception {
// fill your code here
if(ht==null)return null;
if(n==0)return ht;
int temp = ht[0];
ht = reverse(MyArrayUtil.addAsFirst(temp, reverse(MyArrayUtil.tail(ht))));
return shift(ht,n-1);
}
public static void main(String[] args) throws Exception {
// test append, when appending empty array with {1,2,3}
int[] a = null;
int[] b = { 1, 2, 3 };
int[] ans1 = append(a, b);
System.out.println("Append - Expected: 1, 2, 3.");
System.out.print("Get: ");
MyArrayUtil.print(ans1);
System.out.println("");
// test append, when appending 2 non-empty arrays.
int[] c = { 1, 2, 3 };
int[] d = { 4, 5, 6 };
ans1 = append(c, d);
System.out.println("Append - Expected: 1, 2, 3, 4, 5, 6.");
System.out.print("Get: ");
MyArrayUtil.print(ans1);
System.out.println("");
// test reverse
int[] e = { 1, 2, 3, 4, 5 };
ans1 = reverse(e);
System.out.println("Reverse - Expected: 5, 4, 3, 2, 1.");
System.out.print("Get: ");
MyArrayUtil.print(ans1);
System.out.println("");
// test isIn
boolean b1 = isIn(3, e);
System.out.println("isIn - Expected: true.");
System.out.println("Get: " + b1 + ".");
boolean b2 = isIn(7, e);
System.out.println("isIn - Expected: false.");
System.out.println("Get: " + b2 + ".");
// test subArray
int[] f = { 1, 3, 5 };
int[] g = { 1, 2, 3, 4, 5 };
int[] h = { 1, 2, 3, 4, 5 };
int[] i = null;
int[] j = { 1, 4, 5 };
b1 = subArray(f, g);
System.out.println("subArray - Expected: true.");
System.out.println("Get: " + b1 + ".");
b1 = subArray(g, h);
System.out.println("subArray - Expected: true.");
System.out.println("Get: " + b1 + ".");
b1 = subArray(i, h);
System.out.println("subArray - Expected: true.");
System.out.println("Get: " + b1 + ".");
b1 = subArray(f, j);
System.out.println("subArray - Expected: false.");
System.out.println("Get: " + b1 + ".");
b1 = subArray(h, j);
System.out.println("subArray - Expected: false.");
System.out.println("Get: " + b1 + ".");
// test shift
int[] k = shift(g, 3);
System.out.println("shift - Expected: 4, 5, 1, 2, 3.");
System.out.print("Get: ");
MyArrayUtil.print(k);
System.out.println("");
k = shift(g, 2);
System.out.println("shift - Expected: 3, 4, 5, 1, 2.");
System.out.print("Get: ");
MyArrayUtil.print(k);
System.out.println("");
k = shift(g, 8);
System.out.println("shift - Expected: 4, 5, 1, 2, 3.");
System.out.print("Get: ");
MyArrayUtil.print(k);
System.out.println("");
k = shift(g, 0);
System.out.println("shift - Expected: 1, 2, 3, 4, 5.");
System.out.print("Get: ");
MyArrayUtil.print(k);
System.out.println("");
k = shift(null, 5);
System.out.println("shift - Expected: Array empty");
System.out.print("Get: ");
MyArrayUtil.print(k);
System.out.println("");
}
}

11
Assignment11/.classpath Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<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="lib" path="class"/>
<classpathentry kind="output" path="bin"/>
</classpath>

17
Assignment11/.project Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Assignment11</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>

View File

@ -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

Binary file not shown.

Binary file not shown.

189
Assignment11/src/Witch.java Normal file
View File

@ -0,0 +1,189 @@
public class Witch {
// Static Fields
private static int totalWitches = 0;
// Non-Static Fields
private String name;
private String school;
private int magicPower;
// Static Constants
static final int MAX_LEVEL = 100;
static final int MIN_LEVEL = 0;
// Constructors
/**
* This is the default constructor
*
* @category constructor
* @param None This constructor takes no parameter
*/
public Witch() {
this.name = "no one";
this.school = "unknown school";
this.magicPower = 10;
Witch.totalWitches++;
}
/**
* Initialize this witch object with data from parameter
*
* @category constructor
* @param name The name of the witch
* @param school The school that the witch belong to
* @param magicPower The magicPower of the witch
*/
public Witch(String name, String school, int magicPower) {
this.name = name;
this.school = school;
this.magicPower = magicPower;
Witch.totalWitches++;
}
/**
* Initialize this witch object with data from w
*
* @category constructor
* @param w Witch to clone from
*/
public Witch(Witch w) {
this.magicPower = w.getMagicPower();
this.name = w.getName();
this.school = w.getSchool();
Witch.totalWitches++;
}
// Getters
/**
* Get the number of witch created
*
* @category getter
* @return totalWitches The total number of witch created
*/
public static int getTotalWitches() {
return Witch.totalWitches;
}
/**
* Get the name of the witch
*
* @category getter
* @return name name of the witch
*/
public String getName() {
return this.name;
}
/**
* Get the magic power of the witch
*
* @category getter
* @return magicPower The magic power of the witch
*/
public int getMagicPower() {
return this.magicPower;
}
/**
* Get the school of the witch
*
* @category getter
* @return school The school of the witch
*/
public String getSchool() {
return this.school;
}
// Setters
/**
* Set the totalWitches field
*
* @category setter
* @param totalWitches The value of totalWitches to set
*/
public static void setTotalWitches(int totalWitches) {
Witch.totalWitches = totalWitches;
}
/**
* Set the name of the witch
*
* @category setter
* @param name The name of the witch to set
*/
public void setName(String name) {
this.name = name;
}
/**
* Set the magic power of the witch
*
* @category setter
* @param magicPower the magic power to set (0<=magicPower<=100), numbers out of
* range will be rounded up/down to MAX_LEVEL and MIN_LEVEL
*/
public void setMagicPower(int magicPower) {
if (magicPower < MIN_LEVEL)
magicPower = MIN_LEVEL;
if (magicPower > MAX_LEVEL)
magicPower = MAX_LEVEL;
this.magicPower = magicPower;
}
/**
* Set the school of the witch
*
* @category setter
* @param school The school that the witch belong to.
*/
public void setSchool(String school) {
this.school = school;
}
// Utilities
/**
* Return the object as a formatted string
*
* @category utility
* @Override java.lang.Object.toString()
* @return WitchString The formatted string containing all field of Object Witch
*/
@Override
public String toString() {
return String.format("Witchname: %s, School: %s, Magic Power: %s", this.name, this.school, this.magicPower);
}
/**
* Compare the power level of this witch to witch w
*
* @category utility
* @param w The second witch object
* @return intPowerCompare 1 if this>w, 2 if this==w, 3 if this<w
*/
public int compareTo(Witch w) {
if (this.magicPower > w.magicPower)
return 1;
else if (this.magicPower == w.magicPower)
return 0;
else if (this.magicPower < w.magicPower)
return -1;
return 9999; // This statement should not be called
}
/**
* Increase the witch's magicPower
*
* @category utility
* @param increase a positive integer to increase the level of the witch by
* (this.magicPower+increase must be less than MAX_LEVEL)
*/
public void levelUp(int increase) {
if (increase < 0)
return;
if (this.magicPower + increase > MAX_LEVEL)
this.magicPower = MAX_LEVEL;
else
this.magicPower += increase;
}
}

View File

@ -0,0 +1,49 @@
public class WitchGame {
static final String LINE_BREAK = "====================";
public static void main(String[] args) {
//Object Creation and Initialization
Witch witch1 = new Witch();
Witch witch2 = new Witch("Akko", "Luna Nova", 25);
//toString() method test
System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Printing 2 witches", LINE_BREAK);
System.out.format("%s%n%s%n", witch1.toString(), witch2.toString());
//Getters and Setters test
witch1.setName("Hermione Granger");
witch1.setSchool("Hogwarts");
witch1.setMagicPower(85);
//compareTo(Witch w) test
System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Printing the comparison", LINE_BREAK);
System.out.format("%s%n", parseCompareTo(witch1,witch2));
//Copy Constructor test
Witch witch3 = new Witch(witch1);
witch3.levelUp(80);
System.out.format("%s%n", parseCompareTo(witch2,witch3));
//Print all witch
System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Now, printing all witches.", LINE_BREAK);
System.out.format("%s%n%s%n%s%n",witch1.toString(),witch2.toString(),witch3.toString());
//Print Total witches
System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Now, printing total number of witches", LINE_BREAK);
System.out.println(Witch.getTotalWitches());
}
static String parseCompareTo(Witch witch1,Witch witch2) {
return switch (witch1.compareTo(witch2)) {
case 1:
yield witch1.toString()+" has higher magic power.";
case 0:
yield "2 witch have equal magic power.";
case -1:
yield witch2.toString()+" has higher magic power.";
default:
yield "";
};
}
}

View File

@ -1,6 +1,6 @@
Manifest-Version: 1.0
Built-By: siwat
Build-Jdk: 14.0.2
Build-Jdk: 15
Class-Path: log4j-1.2.17.jar jsp-api-2.2.jar
Main-Class: it.ramecera.screwdriver.Hammer
Created-By: Maven Integration for Eclipse

View File

@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Thu Oct 29 16:39:34 ICT 2020
#Wed Nov 18 13:58:23 ICT 2020
m2e.projectLocation=C\:\\Users\\siwat\\GitHub\\ise-comprog-after-midterm\\screwdriver-3.5.6
m2e.projectName=screwdriver-3.5.6
groupId=it.ramecera.ict

View File

@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Tue Nov 03 01:02:01 ICT 2020
#Wed Nov 18 13:58:25 ICT 2020
m2e.projectLocation=C\:\\Users\\siwat\\GitHub\\ise-comprog-after-midterm\\waxds-copper
m2e.projectName=waxds-copper
groupId=it.ramecera.ict