From 39ea9759535b35c7df187e9d11e1372ee078420c Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Wed, 13 Nov 2019 10:43:15 -0600 Subject: Do lab 9, rearrange lab 6 stuff --- csci1913/Java/lab6/Polygon.java | 32 ------- csci1913/Java/lab6/lab6/Polygon.java | 32 +++++++ csci1913/Java/lab6/lab6/lab6_strap012.java | 19 ++++ csci1913/Java/lab6/lab6/tests6.java | 28 ++++++ csci1913/Java/lab6/lab6_strap012.java | 19 ---- csci1913/Java/lab6/tests6.java | 28 ------ csci1913/Java/lab9_strap012.java | 139 +++++++++++++++++++++++++++++ 7 files changed, 218 insertions(+), 79 deletions(-) delete mode 100644 csci1913/Java/lab6/Polygon.java create mode 100644 csci1913/Java/lab6/lab6/Polygon.java create mode 100644 csci1913/Java/lab6/lab6/lab6_strap012.java create mode 100644 csci1913/Java/lab6/lab6/tests6.java delete mode 100644 csci1913/Java/lab6/lab6_strap012.java delete mode 100644 csci1913/Java/lab6/tests6.java create mode 100644 csci1913/Java/lab9_strap012.java (limited to 'csci1913') diff --git a/csci1913/Java/lab6/Polygon.java b/csci1913/Java/lab6/Polygon.java deleted file mode 100644 index 7f025fe..0000000 --- a/csci1913/Java/lab6/Polygon.java +++ /dev/null @@ -1,32 +0,0 @@ -package lab6; - -class Polygon -{ - private int[] sideLengths; - - public Polygon(int sides, int ... lengths) - { - int index = 0; - sideLengths = new int[sides]; - for (int length: lengths) - { - sideLengths[index] = length; - index += 1; - } - } - - public int side(int number) - { - return sideLengths[number]; - } - - public int perimeter() - { - int total = 0; - for (int index = 0; index < sideLengths.length; index += 1) - { - total += side(index); - } - return total; - } -} diff --git a/csci1913/Java/lab6/lab6/Polygon.java b/csci1913/Java/lab6/lab6/Polygon.java new file mode 100644 index 0000000..7f025fe --- /dev/null +++ b/csci1913/Java/lab6/lab6/Polygon.java @@ -0,0 +1,32 @@ +package lab6; + +class Polygon +{ + private int[] sideLengths; + + public Polygon(int sides, int ... lengths) + { + int index = 0; + sideLengths = new int[sides]; + for (int length: lengths) + { + sideLengths[index] = length; + index += 1; + } + } + + public int side(int number) + { + return sideLengths[number]; + } + + public int perimeter() + { + int total = 0; + for (int index = 0; index < sideLengths.length; index += 1) + { + total += side(index); + } + return total; + } +} diff --git a/csci1913/Java/lab6/lab6/lab6_strap012.java b/csci1913/Java/lab6/lab6/lab6_strap012.java new file mode 100644 index 0000000..feecbe1 --- /dev/null +++ b/csci1913/Java/lab6/lab6/lab6_strap012.java @@ -0,0 +1,19 @@ +package lab6; + +class Rectangle extends Polygon { + public Rectangle(int l, int w) { + super(4, l, w, l, w); + } + public int area() { + return this.side(0)*this.side(1); + } +} + +class Square extends Rectangle { + public Square(int l) { + super( l, l); + } + // public int area() { + // return this.side(0)*this.side(1); + // } +} \ No newline at end of file diff --git a/csci1913/Java/lab6/lab6/tests6.java b/csci1913/Java/lab6/lab6/tests6.java new file mode 100644 index 0000000..fc98648 --- /dev/null +++ b/csci1913/Java/lab6/lab6/tests6.java @@ -0,0 +1,28 @@ +package lab6; + +// SHAPES. Public tests for the classes RECTANGLE and SQUARE. Comments show +// what each test must print, and how many points it is worth. + +class Shapes +{ + public static void main(String[] args) + { + Rectangle wreck = new Rectangle(3, 5); + + System.out.println(wreck.side(0)); // 3 1 point. + System.out.println(wreck.side(1)); // 5 1 point. + System.out.println(wreck.side(2)); // 3 1 point. + System.out.println(wreck.side(3)); // 5 1 point. + System.out.println(wreck.area()); // 15 1 point. + System.out.println(wreck.perimeter()); // 16 1 point. + + Square nerd = new Square(7); + + System.out.println(nerd.side(0)); // 7 1 point. + System.out.println(nerd.side(1)); // 7 1 point. + System.out.println(nerd.side(2)); // 7 1 point. + System.out.println(nerd.side(3)); // 7 1 point. + System.out.println(nerd.area()); // 49 1 point. + System.out.println(nerd.perimeter()); // 28 1 point. + } +} diff --git a/csci1913/Java/lab6/lab6_strap012.java b/csci1913/Java/lab6/lab6_strap012.java deleted file mode 100644 index feecbe1..0000000 --- a/csci1913/Java/lab6/lab6_strap012.java +++ /dev/null @@ -1,19 +0,0 @@ -package lab6; - -class Rectangle extends Polygon { - public Rectangle(int l, int w) { - super(4, l, w, l, w); - } - public int area() { - return this.side(0)*this.side(1); - } -} - -class Square extends Rectangle { - public Square(int l) { - super( l, l); - } - // public int area() { - // return this.side(0)*this.side(1); - // } -} \ No newline at end of file diff --git a/csci1913/Java/lab6/tests6.java b/csci1913/Java/lab6/tests6.java deleted file mode 100644 index fc98648..0000000 --- a/csci1913/Java/lab6/tests6.java +++ /dev/null @@ -1,28 +0,0 @@ -package lab6; - -// SHAPES. Public tests for the classes RECTANGLE and SQUARE. Comments show -// what each test must print, and how many points it is worth. - -class Shapes -{ - public static void main(String[] args) - { - Rectangle wreck = new Rectangle(3, 5); - - System.out.println(wreck.side(0)); // 3 1 point. - System.out.println(wreck.side(1)); // 5 1 point. - System.out.println(wreck.side(2)); // 3 1 point. - System.out.println(wreck.side(3)); // 5 1 point. - System.out.println(wreck.area()); // 15 1 point. - System.out.println(wreck.perimeter()); // 16 1 point. - - Square nerd = new Square(7); - - System.out.println(nerd.side(0)); // 7 1 point. - System.out.println(nerd.side(1)); // 7 1 point. - System.out.println(nerd.side(2)); // 7 1 point. - System.out.println(nerd.side(3)); // 7 1 point. - System.out.println(nerd.area()); // 49 1 point. - System.out.println(nerd.perimeter()); // 28 1 point. - } -} diff --git a/csci1913/Java/lab9_strap012.java b/csci1913/Java/lab9_strap012.java new file mode 100644 index 0000000..94a003c --- /dev/null +++ b/csci1913/Java/lab9_strap012.java @@ -0,0 +1,139 @@ +import java.util.Iterator; + +// ARRAY QUEUE. A fixed length queue implemented as a circular array. + +class ArrayQueue { + private int front; // Index of front object in BASES. + private int rear; // Index of rear object in BASES. + private Base[] bases; // The BASEs in the queue. + + // Constructor. Make a new empty queue that can hold SIZE - 1 elements. + + public ArrayQueue(int size) { + if (size >= 1) { + front = 0; + rear = 0; + bases = (Base[]) new Object[size]; + } else { + throw new IllegalArgumentException("Size must be at least 1."); + } + } + + // DEQUEUE. Remove a BASE from the front of the queue and return it. + + public Base dequeue() { + if (isEmpty()) { + throw new IllegalStateException("Queue is empty."); + } else { + front = (front + 1) % bases.length; + Base temp = bases[front]; + bases[front] = null; + return temp; + } + } + + // ENQUEUE. Add BASE to the rear of the queue. + + public void enqueue(Base base) { + int nextRear = (rear + 1) % bases.length; + if (front == nextRear) { + throw new IllegalStateException("Queue is full."); + } else { + rear = nextRear; + bases[rear] = base; + } + } + + // IS EMPTY. Test if the queue is empty. + + public boolean isEmpty() { + return front == rear; + } + + // IS FULL. Test if the queue can hold no more elements. + + public boolean isFull() { + return front == (rear + 1) % bases.length; + } + + private class ArrayQueueIterator implements Iterator { + private int where; + private ArrayQueueIterator() { + where = (front + 1) % bases.length; + } + public boolean hasNext() { + return where != (rear + 1 ) % bases.length; + } + public Base next() { + if (hasNext()){ + Base temp = bases[where]; + where = (where + 1) % bases.length; + return temp; + } else { + throw new IllegalStateException("Queues at its end."); + } + } + public void remove() { + //Do nothing + } + } + public Iterator iterator() { + return new ArrayQueueIterator(); + } +} + + +// QUEUETERATOR. Test ARRAY QUEUE's iterator. It's worth 20 points. + +class Queueterator { + + // MAIN. Start execution here. + + public static void main(String[] args) { + + // Make an ARRAY QUEUE and enqueue some STRINGs. It can hold at most three. + + ArrayQueue queue = new ArrayQueue(4); + + queue.enqueue("A"); + queue.enqueue("B"); + queue.enqueue("C"); + + // Make a FIRST iterator for QUEUE and use it to visit QUEUE's elements. + + Iterator first = queue.iterator(); + while (first.hasNext()) { + System.out.println(first.next()); // A B C one per line 5 pts. + } + + // Make sure FIRST hasn't changed QUEUE. + + System.out.println(queue.isEmpty()); // false 1 pt. + System.out.println(queue.dequeue()); // A 1 pt. + System.out.println(queue.dequeue()); // B 1 pt. + System.out.println(queue.dequeue()); // C 1 pt. + System.out.println(queue.isEmpty()); // true 1 pt. + + // Let's enqueue more three more things to QUEUE. + + queue.enqueue("X"); + queue.enqueue("Y"); + queue.enqueue("Z"); + + // Now make a SECOND iterator for QUEUE. The FIRST one does not work any more, + // because QUEUE has changed. Use SECOND to visit QUEUE's new elements. + + Iterator second = queue.iterator(); + while (second.hasNext()) { + System.out.println(second.next()); // X Y Z one per line 5 pts. + } + + // The new iterator hasn't changed QUEUE either. + + System.out.println(queue.isEmpty()); // false 1 pt. + System.out.println(queue.dequeue()); // X 1 pt. + System.out.println(queue.dequeue()); // Y 1 pt. + System.out.println(queue.dequeue()); // Z 1 pt. + System.out.println(queue.isEmpty()); // true 1 pt. + } +} \ No newline at end of file -- cgit v1.2.3