From 921615945e32ed20de127e50497ae70096dc6649 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Mon, 2 Oct 2023 22:25:57 +0800 Subject: [PATCH 01/38] Committed on or around 2023/10/02 --- .../Interface/Using_Interface_References.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Chapter8/Interface/Using_Interface_References.java diff --git a/Chapter8/Interface/Using_Interface_References.java b/Chapter8/Interface/Using_Interface_References.java new file mode 100644 index 0000000..13eebb9 --- /dev/null +++ b/Chapter8/Interface/Using_Interface_References.java @@ -0,0 +1,21 @@ +package Interface; + +// An interface specifies what to do, not how to do. +interface Seriess{ + int getNext(); + void reset(); + void setStart(int x); +} + +class ByTwoss{ + +} + +public class Using_Interface_References { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} From facb3e5fbfe51b48181da4b33cbf03bcca01827d Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Mon, 2 Oct 2023 22:45:14 +0800 Subject: [PATCH 02/38] Committed on or around 2023/10/02 --- .../Interface/Using_Interface_References.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Chapter8/Interface/Using_Interface_References.java b/Chapter8/Interface/Using_Interface_References.java index 13eebb9..b9c38bb 100644 --- a/Chapter8/Interface/Using_Interface_References.java +++ b/Chapter8/Interface/Using_Interface_References.java @@ -7,7 +7,33 @@ interface Seriess{ void setStart(int x); } -class ByTwoss{ +class ByTwoss implements Seriess{ + + int start; + int value; + + public ByTwoss() { + start = 0; + value = 0; + } + + @Override + public int getNext() { + value += 2; + return value; + } + + @Override + public void reset() { + value = start; + + } + + @Override + public void setStart(int x) { + // TODO Auto-generated method stub + + } } From 59c52d6171b873e4a8c1e8ce372a42afb3568c99 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Mon, 2 Oct 2023 22:46:58 +0800 Subject: [PATCH 03/38] Committed on or around 2023/10/02 --- Chapter8/Interface/Using_Interface_References.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Chapter8/Interface/Using_Interface_References.java b/Chapter8/Interface/Using_Interface_References.java index b9c38bb..e921e50 100644 --- a/Chapter8/Interface/Using_Interface_References.java +++ b/Chapter8/Interface/Using_Interface_References.java @@ -32,7 +32,8 @@ public void reset() { @Override public void setStart(int x) { // TODO Auto-generated method stub - + start = x; + value = x; } } From 072f35af22ce02ccf05b559a6f76f09726b9da8c Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Mon, 2 Oct 2023 22:49:31 +0800 Subject: [PATCH 04/38] Committed on or around 2023/10/02 --- Chapter8/Interface/Using_Interface_References.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Chapter8/Interface/Using_Interface_References.java b/Chapter8/Interface/Using_Interface_References.java index e921e50..fbcec26 100644 --- a/Chapter8/Interface/Using_Interface_References.java +++ b/Chapter8/Interface/Using_Interface_References.java @@ -42,6 +42,8 @@ public class Using_Interface_References { public static void main(String[] args) { // TODO Auto-generated method stub + + } From 9dcc395be203661f30e6717fe395ae13670190eb Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Mon, 2 Oct 2023 22:51:36 +0800 Subject: [PATCH 05/38] Committed on or around 2023/10/02 --- .../Interface/Using_Interface_References.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Chapter8/Interface/Using_Interface_References.java b/Chapter8/Interface/Using_Interface_References.java index fbcec26..8a7689b 100644 --- a/Chapter8/Interface/Using_Interface_References.java +++ b/Chapter8/Interface/Using_Interface_References.java @@ -38,6 +38,38 @@ public void setStart(int x) { } + +class ByThreess implements Seriess{ + + int start; + int value; + + public ByThreess() { + start = 0; + value = 0; + } + + @Override + public int getNext() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void reset() { + // TODO Auto-generated method stub + + } + + @Override + public void setStart(int x) { + // TODO Auto-generated method stub + + } + +} + + public class Using_Interface_References { public static void main(String[] args) { From 97c2b48e42aff7bdc00c71394e89e1fa7c92159b Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Mon, 2 Oct 2023 22:54:16 +0800 Subject: [PATCH 06/38] Committed on or around 2023/10/02 --- Chapter8/Interface/Using_Interface_References.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Chapter8/Interface/Using_Interface_References.java b/Chapter8/Interface/Using_Interface_References.java index 8a7689b..2c1d173 100644 --- a/Chapter8/Interface/Using_Interface_References.java +++ b/Chapter8/Interface/Using_Interface_References.java @@ -51,8 +51,10 @@ public ByThreess() { @Override public int getNext() { - // TODO Auto-generated method stub - return 0; + + value += 3; + + return value; } @Override @@ -73,7 +75,6 @@ public void setStart(int x) { public class Using_Interface_References { public static void main(String[] args) { - // TODO Auto-generated method stub From 98c333b5c718ab89b66c27f0202155315e26ad26 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Mon, 2 Oct 2023 22:56:32 +0800 Subject: [PATCH 07/38] Committed on or around 2023/10/02 --- Chapter8/Interface/Using_Interface_References.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Chapter8/Interface/Using_Interface_References.java b/Chapter8/Interface/Using_Interface_References.java index 2c1d173..7f6af18 100644 --- a/Chapter8/Interface/Using_Interface_References.java +++ b/Chapter8/Interface/Using_Interface_References.java @@ -60,13 +60,14 @@ public int getNext() { @Override public void reset() { // TODO Auto-generated method stub - + value = start; } @Override public void setStart(int x) { // TODO Auto-generated method stub - + start = x; + value = x; } } From 6cf58336c7fa0c2ba54df7ad4c9d6ec007354c69 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Mon, 2 Oct 2023 23:06:07 +0800 Subject: [PATCH 08/38] Committed on or around 2023/10/02 --- Chapter8/Interface/Using_Interface_References.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Chapter8/Interface/Using_Interface_References.java b/Chapter8/Interface/Using_Interface_References.java index 7f6af18..468bfb2 100644 --- a/Chapter8/Interface/Using_Interface_References.java +++ b/Chapter8/Interface/Using_Interface_References.java @@ -77,7 +77,21 @@ public class Using_Interface_References { public static void main(String[] args) { + ByTwoss twoOb = new ByTwoss(); + ByThreess threeOb = new ByThreess(); + Seriess ob; + for (int i = 0; i < 5; i++) { + + ob = twoOb; + + System.out.println("Next ByTwoss value is " + ob.getNext()); + + ob = threeOb; + + System.out.println("Next ByThreesb value is " + ob.getNext()); + + } } From 6ab7e4c684546bb877fc74faef579779cb5672d8 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Mon, 2 Oct 2023 23:08:30 +0800 Subject: [PATCH 09/38] Committed on or around 2023/10/02 --- Chapter8/Interface/Using_Interface_References.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Chapter8/Interface/Using_Interface_References.java b/Chapter8/Interface/Using_Interface_References.java index 468bfb2..fd89725 100644 --- a/Chapter8/Interface/Using_Interface_References.java +++ b/Chapter8/Interface/Using_Interface_References.java @@ -85,11 +85,11 @@ public static void main(String[] args) { ob = twoOb; - System.out.println("Next ByTwoss value is " + ob.getNext()); + System.out.println("Next ByTwos value is " + ob.getNext()); ob = threeOb; - System.out.println("Next ByThreesb value is " + ob.getNext()); + System.out.println("Next ByThrees value is " + ob.getNext()); } From 8f9ca42b86e8f055ae39d9374749891ef5f29f02 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Mon, 2 Oct 2023 23:44:26 +0800 Subject: [PATCH 10/38] Committed on or around 2023/10/02 --- Chapter7/IncompatibleRef.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/Chapter7/IncompatibleRef.java b/Chapter7/IncompatibleRef.java index aa45591..84ceb56 100644 --- a/Chapter7/IncompatibleRef.java +++ b/Chapter7/IncompatibleRef.java @@ -1,5 +1,3 @@ -import java.awt.PrintGraphics; - // This will not compile class X { int a; From 43dfb3d9831483d03feeec23b994c2f4bb60d308 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Mon, 2 Oct 2023 23:52:14 +0800 Subject: [PATCH 11/38] Committed on or around 2023/10/02 --- Chapter7/IncompatibleRef.java | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Chapter7/IncompatibleRef.java b/Chapter7/IncompatibleRef.java index 84ceb56..c161811 100644 --- a/Chapter7/IncompatibleRef.java +++ b/Chapter7/IncompatibleRef.java @@ -1,36 +1,37 @@ // This will not compile class X { + int a; - + X(int i) { a = i; } } class Y { - int a ; - - Y(int i){ + + int a; + + Y(int i) { a = i; } - -} +} public class IncompatibleRef { public static void main(String[] args) { // TODO Auto-generated method stub - + X x = new X(10); - + X x2; - + Y y = new Y(5); - - x2 = x; // Ok, both of same type. - - x2 = y; // Error, not of same type. + + x2 = x; // Ok, both of same type. + + x2 = y; // Error, not of same type. } From 90575cb09b02bd7a4d1a267feec1ee51c495e978 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 00:57:12 +0800 Subject: [PATCH 12/38] Committed on or around 2023/10/03 --- Chapter7/theoretical/SupSubRef.java | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Chapter7/theoretical/SupSubRef.java diff --git a/Chapter7/theoretical/SupSubRef.java b/Chapter7/theoretical/SupSubRef.java new file mode 100644 index 0000000..bcb9efb --- /dev/null +++ b/Chapter7/theoretical/SupSubRef.java @@ -0,0 +1,47 @@ +package theoretical; + +//A superclass reference can refer to a subclass object. + +class X2 { + int a; + + public X2(int i) { + // TODO Auto-generated constructor stub + + a = i; + } +} + +class Y2 extends X2 { + int b; + + public Y2(int i, int j) { + // TODO Auto-generated constructor stub + super(j); + b = i; + } +} + +public class SupSubRef { + + public static void main(String[] args) { + + X2 x = new X2(10); + X2 x2; + Y2 y = new Y2(5, 6); + + x2 = x; // OK, both of same type. + System.out.println("X2.a: " + x2.a); + System.out.println("x.a: " + x.a); + + x2 = y; // still ok because Y2 is derived from X2. Both of somewhat same type. + System.out.println("X2.a: " + x2.a); + + // X references know only about X members + x2.a = 19; // OK +// x2.b = 27; // Error, X does not have a b member. + + + } + +} From 2b760344ef5b00663a84c3ce631b599d7d773aca Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 00:59:26 +0800 Subject: [PATCH 13/38] Committed on or around 2023/10/03 --- Chapter7/theoretical/SupSubRef.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Chapter7/theoretical/SupSubRef.java b/Chapter7/theoretical/SupSubRef.java index bcb9efb..ba3032f 100644 --- a/Chapter7/theoretical/SupSubRef.java +++ b/Chapter7/theoretical/SupSubRef.java @@ -2,20 +2,20 @@ //A superclass reference can refer to a subclass object. -class X2 { +class X { int a; - public X2(int i) { + public X(int i) { // TODO Auto-generated constructor stub a = i; } } -class Y2 extends X2 { +class Y extends X { int b; - public Y2(int i, int j) { + public Y(int i, int j) { // TODO Auto-generated constructor stub super(j); b = i; @@ -26,9 +26,9 @@ public class SupSubRef { public static void main(String[] args) { - X2 x = new X2(10); - X2 x2; - Y2 y = new Y2(5, 6); + X x = new X(10); + X x2; + Y y = new Y(5, 6); x2 = x; // OK, both of same type. System.out.println("X2.a: " + x2.a); From c47d68e82a243eebebf4962fd266fde0bb8082f9 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 01:05:13 +0800 Subject: [PATCH 14/38] Committed on or around 2023/10/03 --- Chapter7/theoretical/SupSubRef.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter7/theoretical/SupSubRef.java b/Chapter7/theoretical/SupSubRef.java index ba3032f..b672a1a 100644 --- a/Chapter7/theoretical/SupSubRef.java +++ b/Chapter7/theoretical/SupSubRef.java @@ -34,7 +34,7 @@ public static void main(String[] args) { System.out.println("X2.a: " + x2.a); System.out.println("x.a: " + x.a); - x2 = y; // still ok because Y2 is derived from X2. Both of somewhat same type. + x2 = y; // still ok because Y is derived from X. System.out.println("X2.a: " + x2.a); // X references know only about X members From bc3f05dfc4e0fd06999bd6355d1a9d28cbf66457 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 01:08:03 +0800 Subject: [PATCH 15/38] Committed on or around 2023/10/03 --- Chapter7/theoretical/SupSubRef.java | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Chapter7/theoretical/SupSubRef.java b/Chapter7/theoretical/SupSubRef.java index b672a1a..e735611 100644 --- a/Chapter7/theoretical/SupSubRef.java +++ b/Chapter7/theoretical/SupSubRef.java @@ -3,45 +3,46 @@ //A superclass reference can refer to a subclass object. class X { + int a; - + public X(int i) { - // TODO Auto-generated constructor stub - a = i; } } + class Y extends X { + int b; - + public Y(int i, int j) { - // TODO Auto-generated constructor stub + super(j); b = i; } } + public class SupSubRef { public static void main(String[] args) { - + X x = new X(10); X x2; Y y = new Y(5, 6); - + x2 = x; // OK, both of same type. System.out.println("X2.a: " + x2.a); System.out.println("x.a: " + x.a); - - x2 = y; // still ok because Y is derived from X. + + x2 = y; // still ok because Y is derived from X. System.out.println("X2.a: " + x2.a); - + // X references know only about X members - x2.a = 19; // OK + x2.a = 19; // OK // x2.b = 27; // Error, X does not have a b member. - } } From 9983e266ba3beb85c7d7aae5eed231ad913aad7d Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:08:53 +0800 Subject: [PATCH 16/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Chapter8/Try_This_8_1.java diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java new file mode 100644 index 0000000..a331471 --- /dev/null +++ b/Chapter8/Try_This_8_1.java @@ -0,0 +1,19 @@ +// A character queue interface +interface ICharQ{ + + // Put a character into the queue. + void put(char ch); + + // Get a character from the queue. + char get(); +} + + +public class Try_This_8_1 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} From d972d3051068a94e30da086eb97b48d97920bed2 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:17:09 +0800 Subject: [PATCH 17/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index a331471..ed4e8dd 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -9,6 +9,27 @@ interface ICharQ{ } +// A fixed-size queue class for characters. +class FixedQueue implements ICharQ{ + + private char q[]; // This array holds the queue. + private int putloc, getloc; // The put and get indices. + + @Override + public void put(char ch) { + // TODO Auto-generated method stub + + } + + @Override + public char get() { + // TODO Auto-generated method stub + return 0; + } + +} + + public class Try_This_8_1 { public static void main(String[] args) { From 3944846104635066af3e6702f2ed8f34527cef16 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:19:44 +0800 Subject: [PATCH 18/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index ed4e8dd..e227098 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -14,14 +14,16 @@ class FixedQueue implements ICharQ{ private char q[]; // This array holds the queue. private int putloc, getloc; // The put and get indices. + + public FixedQueue(int size) { + q = new char[size]; // allocate memory for queue. + putloc = getloc = 0; + } - @Override public void put(char ch) { - // TODO Auto-generated method stub } - @Override public char get() { // TODO Auto-generated method stub return 0; From 77dd325e75ad5f8184623abdc9ffc6f42822484f Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:24:25 +0800 Subject: [PATCH 19/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index e227098..24d0937 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -21,7 +21,12 @@ public FixedQueue(int size) { } public void put(char ch) { + if(putloc == q.length) { + System.out.println(" - Queue is full."); + return; + } + q[putloc++] = ch; } public char get() { From 62ee7e47c5c803109379fe192c12048ec1cf9cda Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:26:25 +0800 Subject: [PATCH 20/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 24d0937..d35cea1 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -30,8 +30,13 @@ public void put(char ch) { } public char get() { - // TODO Auto-generated method stub - return 0; + + if (getloc == putloc) { + System.out.println(" - Queue is empty."); + + return (char) 0; + } + return q[getloc++]; } } From fdb7b1ee837ea9e329862309e5575ffbcf624d27 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:30:12 +0800 Subject: [PATCH 21/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index d35cea1..09c1c42 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -41,6 +41,21 @@ public char get() { } +class CircularQueue implements ICharQ{ + private char q[]; // this array holds the queue. + private int putloc, getloc; // the put and get indices. + + public void put(char ch) { + // TODO Auto-generated method stub + + } + + public char get() { + // TODO Auto-generated method stub + return 0; + } +} + public class Try_This_8_1 { From c6118f9e52c7b5064a0077be61cd36ba0d7644a5 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:46:52 +0800 Subject: [PATCH 22/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 41 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 09c1c42..4f11bee 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -44,19 +44,56 @@ public char get() { class CircularQueue implements ICharQ{ private char q[]; // this array holds the queue. private int putloc, getloc; // the put and get indices. + + // Construct an empty queue given its size. + public CircularQueue(int size) { + q = new char[size + 1]; // allocate memory for queue + putloc = getloc = 0; + } public void put(char ch) { - // TODO Auto-generated method stub + /* + * Queue is full if either putloc is one less than getloc, + * or if putloc is at the end of the array and getloc is at + * the beginning. + * */ + + if (putloc + 1 == getloc | ((putloc == q.length - 1) & (getloc == 0))) { + System.out.println(" - Queue is full. "); + return; + } + + q[putloc++] = ch; + + if (putloc == q.length) { + putloc = 0; + } } public char get() { // TODO Auto-generated method stub - return 0; + if (getloc == putloc) { + System.out.println(" - Queue is empty."); + return (char) 0; + } + + char ch = q[getloc++]; + + if (getloc == q.length) { + getloc = 0; + } + + return ch; } } +class DynQueue implements ICharQ{ + +} + + public class Try_This_8_1 { public static void main(String[] args) { From 4b36e2baae369bc31a3930fc5e8f53c592d14ba7 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:47:06 +0800 Subject: [PATCH 23/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 4f11bee..45afa8b 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -90,6 +90,18 @@ public char get() { class DynQueue implements ICharQ{ + + @Override + public void put(char ch) { + // TODO Auto-generated method stub + + } + + @Override + public char get() { + // TODO Auto-generated method stub + return 0; + } } From b1f34621768464fed08fd6fc929ee306d6ecdb51 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:53:53 +0800 Subject: [PATCH 24/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 39 ++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 45afa8b..892c1de 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -1,4 +1,7 @@ // A character queue interface + +import javax.print.attribute.Size2DSyntax; + interface ICharQ{ // Put a character into the queue. @@ -90,17 +93,45 @@ public char get() { class DynQueue implements ICharQ{ + + private char q[]; + private int putloc, getloc; + + public DynQueue(int size) { + q = new char[size]; + putloc = getloc = 0; + } + - @Override public void put(char ch) { // TODO Auto-generated method stub + if (putloc == q.length) { + // increase queue size + char t[] = new char[q.length * 2]; + + // copy elements into new queue + for (int i = 0; i < q.length; i++) { + + t[i] = q[i]; + + q = t; + } + q[putloc++] = ch; + } + } - @Override + public char get() { - // TODO Auto-generated method stub - return 0; + + if (getloc == putloc) { + System.out.println(" - Queue is empty."); + return (char) 0; + } + + return q[getloc++]; + } } From f1396e2068ed929249321fdd1a013e5c204de83b Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:56:52 +0800 Subject: [PATCH 25/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 892c1de..0678a7f 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -1,7 +1,5 @@ // A character queue interface -import javax.print.attribute.Size2DSyntax; - interface ICharQ{ // Put a character into the queue. @@ -141,6 +139,12 @@ public class Try_This_8_1 { public static void main(String[] args) { // TODO Auto-generated method stub + + FixedQueue q1 = new FixedQueue(10); + DynQueue q2 = new DynQueue(5); + CircularQueue q3 = new CircularQueue(10); + + ICharQ iQ; } From 5210b57c104bc2af28009e9f1303211c41bd0683 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:57:24 +0800 Subject: [PATCH 26/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 0678a7f..25c054c 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -145,6 +145,9 @@ public static void main(String[] args) { CircularQueue q3 = new CircularQueue(10); ICharQ iQ; + + char ch; + int i; } From 85615cdb43221ed9b56553873ef8bec415b44fa5 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 21:22:06 +0800 Subject: [PATCH 27/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 25c054c..de4b1dc 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -148,6 +148,15 @@ public static void main(String[] args) { char ch; int i; + + iQ = q1; + + // Put some characters into fixed queue. + for (i = 0; i < 10; i++) { + iQ.put((char) ('A' + i)); + } + + // Show the queue. } From 935042f1502e4cdd298995956f2c8af015bcd59a Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 23:58:01 +0800 Subject: [PATCH 28/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index de4b1dc..07350db 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -157,6 +157,7 @@ public static void main(String[] args) { } // Show the queue. + System.out.print("Contents of fixed queue: "); } From 421badb760c9bdd0d8b62429277d7835283b1d6e Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 00:00:02 +0800 Subject: [PATCH 29/38] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 07350db..7fc33f2 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -158,6 +158,11 @@ public static void main(String[] args) { // Show the queue. System.out.print("Contents of fixed queue: "); + + for (i = 0; i < 10; i++) { + ch = iQ.get(); + System.out.print(ch); + } } From d4331dc3f18d9bcc1c4197c8d5ffc71c0dadf175 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 00:02:31 +0800 Subject: [PATCH 30/38] Committed on or around 2023/10/04 --- Chapter8/Try_This_8_1.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 7fc33f2..f5af1c9 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -163,6 +163,13 @@ public static void main(String[] args) { ch = iQ.get(); System.out.print(ch); } + + System.out.println(); + + iQ = q3; + + // Put some characters into circular queue. + } From 547a382f210161b79c4bea716e77916f5b697ed2 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 00:10:56 +0800 Subject: [PATCH 31/38] Committed on or around 2023/10/04 --- Chapter8/Try_This_8_1.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index f5af1c9..b5864d5 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -164,12 +164,33 @@ public static void main(String[] args) { System.out.print(ch); } + System.out.println(); + + iQ = q2; + + // Put some characters into dynamic queue. + for (i = 0; i < 10; i++) { + iQ.put((char) ('Z' - i)); + } + + // Show the queue. + System.out.print("Contents of dynamic queue: "); + + System.out.println(); iQ = q3; // Put some characters into circular queue. + for (i = 0; i < 20; i++) { + iQ.put((char) ('A' + i)); + } + // Show the queue. + System.out.print("Contents of circular queue: "); + for (i = 0; i < 10; i++) { + + } } From a80ed15e9e362320b3d506c7e40960a43441aa7e Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 00:13:44 +0800 Subject: [PATCH 32/38] Committed on or around 2023/10/04 --- Chapter8/Try_This_8_1.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index b5864d5..3528fe2 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -175,6 +175,10 @@ public static void main(String[] args) { // Show the queue. System.out.print("Contents of dynamic queue: "); + for (i = 0; i < 10; i++) { + ch = iQ.get(); + System.out.print(ch); + } System.out.println(); From e3e15dc2973ba99e7865057fad34fb56201d6bdf Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 12:08:31 +0800 Subject: [PATCH 33/38] Committed on or around 2023/10/04 --- Chapter8/Try_This_8_1.java | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 3528fe2..735ce48 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -18,7 +18,8 @@ class FixedQueue implements ICharQ{ public FixedQueue(int size) { q = new char[size]; // allocate memory for queue. - putloc = getloc = 0; + +putloc = getloc = 0; } public void put(char ch) { @@ -180,7 +181,6 @@ public static void main(String[] args) { System.out.print(ch); } - System.out.println(); iQ = q3; @@ -193,7 +193,31 @@ public static void main(String[] args) { // Show the queue. System.out.print("Contents of circular queue: "); for (i = 0; i < 10; i++) { - + ch = iQ.get(); + System.out.print(ch); + } + + System.out.println(); + + // Put more characters into circular queue. + for (i = 0; i < 20; i++) { + iQ.put((char) ('A' + i)); + } + + // Show the queue. + System.out.print("Contents of circular queue: "); + for (i = 0; i < 10; i++) { + ch = iQ.get(); + System.out.print(ch); + } + + System.out.println("\nStore and consume from" + " circular queue."); + + // Store in and consume from circular queue. + for (i = 0; i < 20; i++) { + iQ.put((char) ('A' + i)); + ch = iQ.get(); + System.out.print(ch); } } From 30e0870af28223fa624de4e8e7b6aa1953c744b9 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 12:30:24 +0800 Subject: [PATCH 34/38] Committed on or around 2023/10/04 --- Chapter8/Try_This_8_1.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 735ce48..b3194e5 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -113,12 +113,12 @@ public void put(char ch) { for (int i = 0; i < q.length; i++) { t[i] = q[i]; - - q = t; } - q[putloc++] = ch; + + q = t; } + q[putloc++] = ch; } From 14325f9bcfaa3f113bb40e5647b215a4c281ef66 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 13:02:19 +0800 Subject: [PATCH 35/38] Committed on or around 2023/10/04 --- Chapter8/Try_This_8_1.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index b3194e5..a892e59 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -19,7 +19,7 @@ class FixedQueue implements ICharQ{ public FixedQueue(int size) { q = new char[size]; // allocate memory for queue. -putloc = getloc = 0; + putloc = getloc = 0; } public void put(char ch) { From 1165d4bdd74eb652a72ae037fee612c4e79b1ae1 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 13:15:01 +0800 Subject: [PATCH 36/38] Committed on or around 2023/10/04 --- Chapter8/Try_This_8_1.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index a892e59..542eb47 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -186,7 +186,7 @@ public static void main(String[] args) { iQ = q3; // Put some characters into circular queue. - for (i = 0; i < 20; i++) { + for (i = 0; i < 10; i++) { iQ.put((char) ('A' + i)); } @@ -200,7 +200,7 @@ public static void main(String[] args) { System.out.println(); // Put more characters into circular queue. - for (i = 0; i < 20; i++) { + for (i = 10; i < 20; i++) { iQ.put((char) ('A' + i)); } From cc0f63f4c15dbe5ed2a8bb7410c7623a017bfa7c Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 14:02:22 +0800 Subject: [PATCH 37/38] Committed on or around 2023/10/04 --- .../Interface/Interfaces_Can_Be_Extended.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Chapter8/Interface/Interfaces_Can_Be_Extended.java diff --git a/Chapter8/Interface/Interfaces_Can_Be_Extended.java b/Chapter8/Interface/Interfaces_Can_Be_Extended.java new file mode 100644 index 0000000..c101761 --- /dev/null +++ b/Chapter8/Interface/Interfaces_Can_Be_Extended.java @@ -0,0 +1,18 @@ +package Interface; + +interface A{ + + void meth1(); + void meth2(); +} + + + +public class Interfaces_Can_Be_Extended { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} From fdd7c3de3831b4b30a87b1ef9f3ef5dcbbde3c05 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 14:12:36 +0800 Subject: [PATCH 38/38] Committed on or around 2023/10/04 --- .../Interface/Interfaces_Can_Be_Extended.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Chapter8/Interface/Interfaces_Can_Be_Extended.java b/Chapter8/Interface/Interfaces_Can_Be_Extended.java index c101761..69c49e7 100644 --- a/Chapter8/Interface/Interfaces_Can_Be_Extended.java +++ b/Chapter8/Interface/Interfaces_Can_Be_Extended.java @@ -1,17 +1,50 @@ package Interface; +// One interface can extend another. interface A{ void meth1(); void meth2(); } +// B now includes meth1() and meth2() - it adds meth3(). +interface B extends A{ // B inherits A. + void meth3(); +} + +// This class must implement all of A and B +class MyClass implements B{ + + @Override + public void meth1() { + // TODO Auto-generated method stub + System.out.println("Implement meth1(). "); + } + + @Override + public void meth2() { + // TODO Auto-generated method stub + System.out.println("Implement meth2(). "); + } + + @Override + public void meth3() { + // TODO Auto-generated method stub + System.out.println("Implement meth3(). "); + } +} public class Interfaces_Can_Be_Extended { public static void main(String[] args) { // TODO Auto-generated method stub + + MyClass obMyClass = new MyClass(); + + obMyClass.meth1(); + obMyClass.meth2(); + obMyClass.meth3(); }