diff --git a/Chapter7/IncompatibleRef.java b/Chapter7/IncompatibleRef.java index aa45591..c161811 100644 --- a/Chapter7/IncompatibleRef.java +++ b/Chapter7/IncompatibleRef.java @@ -1,38 +1,37 @@ -import java.awt.PrintGraphics; - // 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. } diff --git a/Chapter7/theoretical/SupSubRef.java b/Chapter7/theoretical/SupSubRef.java new file mode 100644 index 0000000..e735611 --- /dev/null +++ b/Chapter7/theoretical/SupSubRef.java @@ -0,0 +1,48 @@ +package theoretical; + +//A superclass reference can refer to a subclass object. + +class X { + + int a; + + public X(int i) { + a = i; + } +} + + +class Y extends X { + + int b; + + public Y(int i, int j) { + + 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. + 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. + + } + +} diff --git a/Chapter8/Interface/Interfaces_Can_Be_Extended.java b/Chapter8/Interface/Interfaces_Can_Be_Extended.java new file mode 100644 index 0000000..69c49e7 --- /dev/null +++ b/Chapter8/Interface/Interfaces_Can_Be_Extended.java @@ -0,0 +1,51 @@ +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(); + + } + +} diff --git a/Chapter8/Interface/Using_Interface_References.java b/Chapter8/Interface/Using_Interface_References.java new file mode 100644 index 0000000..fd89725 --- /dev/null +++ b/Chapter8/Interface/Using_Interface_References.java @@ -0,0 +1,98 @@ +package Interface; + +// An interface specifies what to do, not how to do. +interface Seriess{ + int getNext(); + void reset(); + void setStart(int x); +} + +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 + start = x; + value = x; + } + +} + + +class ByThreess implements Seriess{ + + int start; + int value; + + public ByThreess() { + start = 0; + value = 0; + } + + @Override + public int getNext() { + + value += 3; + + return value; + } + + @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; + } + +} + + +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 ByTwos value is " + ob.getNext()); + + ob = threeOb; + + System.out.println("Next ByThrees value is " + ob.getNext()); + + } + + } + +} diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java new file mode 100644 index 0000000..542eb47 --- /dev/null +++ b/Chapter8/Try_This_8_1.java @@ -0,0 +1,225 @@ +// A character queue interface + +interface ICharQ{ + + // Put a character into the queue. + void put(char ch); + + // Get a character from the queue. + char get(); +} + + +// 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. + + public FixedQueue(int size) { + q = new char[size]; // allocate memory for queue. + + putloc = getloc = 0; + } + + public void put(char ch) { + if(putloc == q.length) { + System.out.println(" - Queue is full."); + return; + } + + q[putloc++] = ch; + } + + public char get() { + + if (getloc == putloc) { + System.out.println(" - Queue is empty."); + + return (char) 0; + } + return q[getloc++]; + } + +} + +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) { + /* + * 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 + 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{ + + private char q[]; + private int putloc, getloc; + + public DynQueue(int size) { + q = new char[size]; + putloc = getloc = 0; + } + + + 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; + } + + + public char get() { + + if (getloc == putloc) { + System.out.println(" - Queue is empty."); + return (char) 0; + } + + return q[getloc++]; + + } + +} + + +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; + + 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. + System.out.print("Contents of fixed queue: "); + + for (i = 0; i < 10; i++) { + ch = iQ.get(); + 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: "); + for (i = 0; i < 10; i++) { + ch = iQ.get(); + System.out.print(ch); + } + + System.out.println(); + + iQ = q3; + + // Put some characters into circular queue. + for (i = 0; i < 10; 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(); + + // Put more characters into circular queue. + for (i = 10; 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); + } + + } + +}