CS340 Programming Homework 2 (P2)

advertisement
CS340 Programming Homework 2 (P2)
Stacks, Queues, Recursion
Due Nov. 8th, 2011
Deliverables: code, README file that describes how to run your code
1. Implement an Operating System Stack (30 pts)
This homework can be found in your textbook, Chapter 3, Programming Project
10 (p. 193)
Operating Systems sometimes use a fixed array storage to accommodate a pair
of stacks such that one grows from the bottom (with the first item stored at
index 0) and the other grows from the top (with its first item stored at the
highest array index). As the stacks grow, the top of the stacks will move closer
together.
The stacks are full when the top elements are stored in adjacent array elements
(top2 = top1+1). Design, code and test a class DoubleStack that implements this
data structure. Double stack should support the normal stack operations (push, pop,
peek, empty etc.). Each stack method should have an additional int parameter that
indicates which of the stacks (1 or 2) is being processed. For example, push(1, item)
will push item into stack 1.
2. Use a queue to simulate a data center(30 pts)
You are working for Google and need to estimate how many servers you need for the
new data center that will be built in NC near the Appalachian mountains.
HTTP requests enter the data center and there are one or more servers to serve these. If
a server is free, the request is served by the free server. Otherwise the request enters a
queue and waits until there is a free server.
Your program should take as input:
a. The average arrival rate of a request to the server (measure this in requests per
second)
b. The average processing time of a request on a server (measure this in seconds)
c. The number of servers
Page 1 of 3
Use your program to find how many servers are required for a given arrival rate and
average processing time. Another question you should be able to answer with your
program is how long on average is a request going to wait in the queue, if the arrival
rate is for example 1000 requests per second, each server processes 400 requests per
second, and we have 3 servers in the data center. For this you need to be able to mark
the time one request remains in the queue and calculate the average of all these waiting
times.
Alternative with reduced requirements (less points):
You can reproduce the case study in chapter 4.5 of your book (p. 221233). This will be mostly copying the code from the book, therefore you
will only get 15 points credit.
3. Recursion (40 pts):
a. Paper Folds (25 pts)
Description: Take a piece of paper and fold it in half. Unfold it and there is one fold,
which I'll signify with a "v". If you fold the paper twice (always keeping the folds in
the same direction and the creases parallel), it will have three folds when you unfold
it, and they will be in a "^ v v" pattern. Fold a piece of paper three times, and you
will see "^ ^ v v ^ v v" as the sequence of folds.
Folding a piece of already folded paper sounds a lot like recurison, and indeed it is.
In case you can't see the algorithm, here it is:


If you fold a paper once, the fold pattern is "v".
For additional folds, it is the pattern you have only reversed and flipped over,
then a "v" fold, and then the pattern you already have tacked on the end.
This actually makes sense, if you think about it, or you can just take it on faith as you
commonly do when writing recursive functions.
Assignment: Write a class, PaperFold, that has a recursive routine that returns a
string representing the fold pattern for a paper folded n times. Here is code to test
your class:
class TestPaperFolds
{
public static void main(String[] args)
{
PaperFold paper = new PaperFold();
System.out.println( paper.fold(4) );
}
}
Page 2 of 3
You may want to write a private helper function to generate a string that takes a fold
sequence and returns it reversed and flipped over such that a "v ^ ^ ^" becomes a "v
v v ^". As in many recursive solutions, expect the fold method to be extremely
simple (and of course recursive).
b. Lists and recursion (15 pts)
Write an iterative Reverse() method that reverses a list by rearranging all the
.next pointers and the head pointer. Ideally, Reverse() should only need to make
one pass of the list.
You only need to write the Reverse() method and use the LinkedList package
from the Java API.
Page 3 of 3
Download