COP4331/5339 Homework 5Chapters 8 and 9
*** no homeworks will be accepted after the due date ***
Preparation/Delivery Instructions:
1. Write all your answers, in the order given in the homework file, in ONE PDF file.
(Convert from Word doc.)
Upload also the Java source files in a **single zip** file.
Organize the files in the
zip file in directories corresponding to the problems.
Follow this format for the PDF file:
Write your name followed by the section number (e.g. COP4331 001 – or COP5339).
For each problem write as a heading the problem number (e.g. “Problem 4.1”).
The problem number must be clearly readable before the problem solution.
Make sure Java code and UML diagrams are readable. Make it easy for the grader.
Nice color syntax highlighting is not required, but appreciated.
Check out online services for highlighting in Word with conversion to HTML:
https://tohtml.com/java/ and http://hilite.me/ .
Proper indentation and code formatting is required.
2. Upload on Canvas the pdf file and the zip file with all Java source files for the solutions.
Put UML diagrams in the PDF file.
3. For full credit, your designs and code must follow the course guidelines and must compile without
warnings and work correctly, as required in the problem description.
Remider: it is academic misconduct to submit work that is not yours.
Do not paste code taken from the web.
You can (and should) use any helpful code from the textbook for your answers.
—————-
Other general advice that will help you do well in this class. And build better code, too.
* !! Ask your instructor if you have any questions about the homework
(and anything else related to the class) !!
* Consult the solutions for selected textbook problems, available at
http://www.horstmann.com/oodp2/solutions/solutions.html
* Do exactly what the problem asks you to do. There is no extra credit for unnecessary work.
Points are deducted if design or implemenation requirements are not met.
* Do not rename classes and methods if they are given.
* Do not change method signature, where specified.
* Design/code your classes for general use. Assume there are other programmers who will use your code.
* Avoid unnecessary side effects. Do not use static fields/methods, unless warranted (e.g. main()).
* check for errors and exceptions.
* Enclose methods that may throw exceptions within a try-catch block.
* Check parameters and variables before you do something in a method. E.g. average = sum/list.size()
may throw an ArithmeticException if the list is empty.
* Use class (static) variables only when necessary (e.g. to share a variable between instances,
or for constants)
* Do not define instance variables when local variables could do the job.
* Use nouns for class names and verbs for methods.
* Follow coding conventions; class names start with capitals, methods and variables start with lowercase, etc.
Scoring: non-optional problems total = 100 points
======================================================================================================
Homework problems (Chapter 8):
8.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();
Object clone();
}
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 ” + this.getName()).
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
—————————-
9.1
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 “synchronized” methods/blocks (preferred solution).
Points will be deducted if race conditions are possible.