During this lab session:
This activity helps students develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.
IMPORTANT NOTE: The lab activities can be developed individually or in pairs. The lab report must be developed individually.
Create a folder called adapter. Inside this folder, create three files called: simple_queue.rb, queue_adapter.rb, and tc_adapter.rb.
All three Ruby source files must start with a comment containing the lab's title, date, and the authors' personal information. For example:
# Lab 6: Adapter Pattern # Date: 6-Mar-2013 # Authors: # 456654 Thursday Rubinstein # 1160611 Anthony Stark
The content of the file simple_queue.rb is as follows:
# File: simple_queue.rb # IMPORTANT: Do not modify the following class in any way! class SimpleQueue def initialize @info =[] end def insert(x) @info.push(x) self end def remove if empty? raise "Can't remove if queue is empty" else @info.shift end end def empty? @info.empty? end def size @info.size end def inspect @info.inspect end end
The SimpleQueue class contains a First-In First-Out (FIFO) data structure that implements the following methods:
| Method Signature | Description |
|---|---|
insert(x)
|
Inserts x at the back of this queue. Returns this queue. |
remove
|
Removes and returns the element at the front of this queue. Raises an exception if this queue happens to be empty. |
empty?
|
Returns true if this queue is empty, otherwise returns false.
|
size
|
Returns the number of elements currently stored in this queue. |
Write an adapter class called QueueAdapter (place it in the file called queue_adapter.rb) that makes a SimpleQueue instance (as implemented above) behave like a Last-In First-Out (LIFO) stack with the following interface:
| Method Signature | Description |
|---|---|
initialize(q)
|
Initializes a new stack, using q as the adaptee. |
push(x)
|
Inserts x at the top of this stack. Returns this stack. |
pop
|
Returns nil if this stack is empty, otherwise removes and returns its top element.
|
peek
|
Returns nil if this stack is empty, otherwise returns its top element without removing it. |
empty?
|
Returns true if this stack is empty, otherwise returns false.
|
size
|
Returns the number of elements currently stored in this stack. |
QueueAdapter class. Place the test class in the tc_adapter.rb source file.
# File: tc_adapter.rb require 'test/unit' require 'simple_queue' require 'queue_adapter' class QueueAdapterTest < Test::Unit::TestCase def test_queue_adapter q = SimpleQueue.new qa = QueueAdapter.new(q) assert q.empty? assert qa.empty? assert_nil qa.pop qa.push("Foo") assert_equal "Foo", qa.peek assert !q.empty? assert !qa.empty? qa.push("Bar") assert_equal "Bar", qa.peek qa.push("Baz").push("Quux") assert_equal 4, q.size assert_equal 4, qa.size assert_equal "Quux", qa.peek assert_equal "Quux", qa.pop assert_equal "Baz", qa.peek assert_equal "Baz", qa.pop assert_equal "Bar", qa.peek assert_equal "Bar", qa.pop assert_equal "Foo", qa.peek qa.push("Goo") assert_equal "Goo", qa.peek assert_equal "Goo", qa.pop assert_equal "Foo", qa.peek assert_equal 1, qa.size assert_equal "Foo", qa.pop assert_nil qa.peek assert_nil qa.pop assert q.empty? assert qa.empty? assert_equal 0, q.size assert_equal 0, qa.size end end
To hand in your individual lab work, follow these instructions.
lab6_report_A0MMMMMMM.txt, where A0MMMMMMM is your student ID. From your AsciiDoc source, generate the corresponding HTML file. That file should be called lab6_report_A0MMMMMMM.html. Place these two files in the adapter directory.
adapter directory. Call this file adapter.tgz. From a terminal, you can use the following command to create this file (make sure to run it at the same level where the adapter folder resides):
tar czf adapter.tgz adapter
Due date is Tuesday, March 12.
This activity will be evaluated using the following criteria:
| 50% | Implementation of functional requirements. |
|---|---|
| 50% | Lab report. |
| DA | The program and/or report was plagiarized. |