0% found this document useful (2 votes)
228 views

Java 5 Programs

The document describes 4 projects involving different design patterns: Project 1 involves implementing a toolbox and tools using the prototype pattern. It provides an interface for tools and toolbox and asks to implement it in Java. Project 2 involves implementing a generic queue using an adapter pattern to wrap a LinkedList. It asks to provide the class diagram and Java implementation. Project 3 involves implementing a singleton pattern for a Stdout class to print to terminal. It asks to provide the Java code and a test. Project 4 involves implementing parallel sum calculation using threads. It asks to implement the sum method using locks or wait/notify.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (2 votes)
228 views

Java 5 Programs

The document describes 4 projects involving different design patterns: Project 1 involves implementing a toolbox and tools using the prototype pattern. It provides an interface for tools and toolbox and asks to implement it in Java. Project 2 involves implementing a generic queue using an adapter pattern to wrap a LinkedList. It asks to provide the class diagram and Java implementation. Project 3 involves implementing a singleton pattern for a Stdout class to print to terminal. It asks to provide the Java code and a test. Project 4 involves implementing parallel sum calculation using threads. It asks to implement the sum method using locks or wait/notify.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Project 1 A toolbox contains various named tools that can be used in a way that depends on the specific tool.

The tool interface is: interface Tool extends Cloneable { String getName(); void use(); } There are several classes of tools for our problem domain: Eraser, Pencil, Protractor, Ruler, Paper. A toolbox can return (get()) to the client a copy of a stored tool object based on its name, or throw NoSuchElementException if no tool exists in the toolbox with the given name. The toolbox can be populated with new tools (the add() method). The toolbox interface is: interface Toolbox { void add(Tool tool); Tool get(String toolName); } a. Use the Prototype pattern to design the toolbox and tools. Write the UML class diagram. b. Implement the design in Java. Write contracts for all methods. To keep things simple, the Tool.use() method should just println("Using " + name()). c. Write a ToolTest class with a main() method that exemplifies how the toolbox is used: - create the toolbox - populate it with one tool of each kind - call get for several named tools and 'use' them

--------------------------------------------------------------------------------------------------------------------

Project 2 Generic queue interface: interface MyQueue <E> { // return the top of the queue element or throw NoSuchElementException if empty E head(); // remove and return the top of the queue element or throw NoSuchElementException if empty E dequeue(); // add an element to the queue void enqueue(E e); // returns the size of the queue int size(); // returns true if the queue is empty boolean isEmpty(); } a) Use the Adapter pattern to design a generic queue class called LQueue<E> that implements interface MyQueue and that "wraps" a LinkedList<E> object that stores the elements. Write the class diagram in a pdf file. Make sure you identify the pattern roles (e.g. "adapter", "adaptee") with UML notes. b) Implement class LQueue<E> in Java. Write correct contracts for each method. (Notice that NoSuchElementException is not a checked exception, but you could put it in the function signature.) c) Write a class QueueTest with a main(...) method that tests all 5 methods in LQueue<E>. -----------------------------------------------------------------------------------------------------------------------------------------

Project 3 Use the Singleton pattern for a Java class Stdout with just one instance that a programmer can use to print text lines (String objects) to the terminal: println("some string"). Make sure the pattern is implemented correctly (e.g. just one instance can be created, and when it is first needed). Write all Java code in the Stdout.java file. Also write a main(....) method to test your code. ----------------------------------------------------------------------------------------------------------------------------------Project 4 Write a class Summer that computes the sum of numbers from 1 to n in parallel using multiple concurrent threads. The class interface has a public method: /** *@param n the upper limit *@param k the number of threads *@return the sum 1 + 2 + ... + n computed with k threads */ int sum(int n, int k); The sum 1+2+...+n is broken down in k intervals: [0, n/k], [n/k+1, 2*n/k], [2*n/k+1, 3*n/k],...,[(k-1)*n/k, n]. A "worker"thread with index j (from 0 to k-1) computes the partial sum of the interval [j*n/k, (j+1)*n/k]. After all worker threads finish summing their interval, the main thread adds all partial sums and returns the total sum. Make sure it works. Test against the formula sum=n*(n+1)/2. Use either ReentrantLock with Condition or use the Java object lock with wait/notify and and "synchronized" methods/blocks (preferred solution). Points will be deducted if race conditions are possible.

----------------------------------------------------------------------------------------Project 5 Use the Command pattern to implement a simple 'pocket calculator'. The user types on the terminal (System.in) lines of text. Use Scanner to read lines. Each line can be one of: - an arithmetic operator: +,-,*,/ - a double number, like 12.34 - the letter u ('undo' function) - the letter c ('clear' function)

The calculator maintains an accumulator with the current result. The initial value of the accumulator is 0. The accumulator can be assigned a value by entering a double number as the first line or after a 'clear' operation. Number lines must alternate with operator lines. A clear or undo operation can be entered at any time. When the user enters 'u' (with no quotes, obviously) on a line, the calculator reverts the accumulator to its prior value BEFORE the most recent arithmetic operation (or 0, if none). When the user enters 'c', the accumulator is set to 0. After a number line is entered, the calculator must display the new value of the accumulator with a tab space in front ('\t') and computed as follows: the new accumulator value is set to the value resulting from applying the previously entered operation on the current accumulator and the number just entered. I.e. newacc = oldacc op number. If the user enters invalid input, the input is ignored and the current accumulator is displayed. If the user types an empty line, the calculator displays the current value of the accumulator. Here are some example runs: (output is displayed with a TAB '\t' in front) Ex1. ac=0 10 ac=10 3 ac=7 * 2 ac=14 + 6 ac=20 u ac=14 u ac=7 / 2 ac=3.5 bad input ac=3.5 c 0 2 ac=2 u ac=0

u ac=0 Ex 2. ac=0 + 5 ac=5 10 ac=-5 + + 7 ac=2 u ac=-5

// this is an invalid line. Ignore

A 'Command' object from the pattern should encapsulate an operation with the operand. Call the class Operation. Operation could also store the current accumulator value. The Calculator object could use a (special type of) collection object that gives LIFO access for Operation objects. Requirements: a) Write the class diagram and the state diagram for Calculator in a pdf file. Identify the pattern roles in the class diagram. b) Write the Java code and supply a test class (with main()) for testing your code. Use programming by contract. c) Include a screenshot of your application running in the pdf file from a)

You might also like