Sunteți pe pagina 1din 3

Introduction

The first 6 parts covers most of Project Coin. If you are unfamiliar with this aspect of Java 7, then it is likely that researching and solving those tasks will take up all available time for this Hands on Lab. If you already know Coin, those exercises should take 5 to 10 minutes, leaving you a lot of time for the harder exercise 7. The information needed to solve each exercise can be found at http://download.oracle.com/javase/tutorial/index.html. Good luck!

Setup
Unzip the JDK7 lab to a suitable location. Start netbeans and import the project.

Part 1 Try With Resource


Run the program. Note the printouts from Testing try with resources. Use the new Java 7 features to make sure that the assert does not fail. Hint/limitation You are not allowed to use more than one catch-clause or to use finally.

Part 2 New annotation


Rebuild the project (Shift-F11). You should get a Note: JDK7 Lab\src\jdk7\lab\ArrayBuilder.java uses unchecked or unsafe operations. Use a new annotation from Java 7 that makes this message go away. Hint /limitation You are not allowed to turn off all warnings. You need to edit the ArrayBuilder class.

Part 3 Strings in switch


Modify the checker method using the new Strings in Switch in such a way that it still works. Run the project to ensure that none of the asserts fail.

Part 4 Diamond
Scroll back up to the method safeVarargs. Use diamond notation to make the code shorter and easier to read.

Part 5 Data types


Change the initiation of lots so it is easier to read and to see how many thousands and millions are in the number.

Change the initiation of maxbyte to use binary instead of hexadecimal format. Run your code to ensure that the asserts still hold.

Part 6 Multi catch


Refactor the method to only have one catch clause. Hint/limitation You are still only allowed to catch the exact exception types in the current code, no super classes.

Part 7a Gloves are off


The parts above were a nice and simple introduction to Coin. If you made it this far in the allotted time, its time to remove the training wheels. Create a new project that follows the requirements below:

Definition
Fibonacci(0) = 1 Fibonacci(1) = 1 Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2) // For n >= 2

Disclaimer
Using fork/join to solve Fibonacci is a very bad idea from a computational standpoint. (For those of you that are into Big-O notation, it runs in O(exp(n)) time). We are doing this because its a clear example, not because its computationally effective.

Requirements
Create a project that calculates Fibonacci(6) that: - - - Uses the Java 7 fork/join framework Uses the InvokeAll method to do the actual forking and joining Has debug print-outs that shows what any specific thread is currently calculating, e.g Thread 17 is calculating Fib(3)

Can you see any work-stealing happening? Can you, from your debug print-outs, see how the algorithm works and what is run when?

Part 7b Do as Doug says


When you are using InvokeAll as you did above, your algorithm (extremely simplified) creates 2 new threads to do computation while the original one rests and waits for their results. If would be more effective to just create one new thread, and let the original thread do the other half of the calculation instead of just waiting.

Refactor your code from 7a, but you are not allowed to use InvokeAll, instead you should use fork and join. Look at your debug prints what are the differences?

S-ar putea să vă placă și