Difference between revisions of "CSC352 Homework 2 2013"

From dftwiki3
Jump to: navigation, search
(Time to Play!)
(Time to Play!)
Line 37: Line 37:
 
</center>
 
</center>
 
<br />
 
<br />
 +
If you keep on going, you should see that as you get closer to the right side of the applet the rectangles are getting smaller and smaller.  That's because the collection of rectangles being packed is random, and sorted from largest area to smallest area, and the packing algorithm you are given always packs the largest rectangles first.
 +
 +
<br />
 +
==Time to Read the Code==
 +
<br />
 +
The only two java programs you need to read carefully are '''PackingHw2''' and '''Packer'''.  The header of '''PackingHw2''' indicates how to modify the program to make it pack on its own.  Try this modification.
 +
 +
Keep on reading and playing with the code until you feel you understand how both files work.  You do not need to study the other java files to do this homework assignment.
 +
<br />
 +
 +
=Your Assignment=
 +
<br />
 +
 +
Your assignment is to do something very similar to what you did in the [[CSC352_Java_Threads:_Producer-Consumer_Lab| Producer-Consumer]] lab.
 +
 +
You need to add a new thread to the  main application (create it in a new class, in a new java file called '''PackThread''') that will be given the list  of rectangles to pack (sorted or not) by the '''setup()''' function, and then will pack the the rectangles as fast as it can, and pass them back to '''draw()''' in a thread-safe data structure of your choice.  Draw() will display the rectangles it gets back, as it gets them back, as fast as it can.
 +
 +
<br />
 +
==Questions==
 +
*

Revision as of 17:36, 20 September 2013

--D. Thiebaut (talk) 17:10, 20 September 2013 (EDT)


Page under construction!
UnderConstruction.jpg

Multithreaded Packing

The idea for this homework is simple. You get a Processing application that is highly serial and that packs rectangles into a rectangular applet, and you make it parallel by adding a thread that generates packed rectangles, i.e. rectangles that are positioned in the area of the applet in such a way that they touch each other but do not overlap, and at the same time minimize the wasted area.


First, Get the Code


Two different versions of the initial code is available to you. One to run in the native Processing IDE, the other to run with Eclipse (which you are highly encourage to use--Eclipse is a bit tough to setup to run with Processing apps, but one you are done, you benefit from the great features of Eclipse).

The two different versions are available below (but only accessible from Smith campus computers. If you are off-campus, please email me).


This section is only visible to computers located at Smith College

If you go with the native Processing app, just download the *IDE.tgz file and unpack it in a Terminal window on your Mac:

  tar xzvf nameOfTheTgzFile

You will get a new directory called PackingHw2. That's your sketch. Open it with the Processing IDE, and run PackingHw2. You should get a blank applet opened in front of you.

If you go with the Eclipse version (good on you!), then download it and unpack it similarly. You can then import it as File System into your workspace. The main application is PackingHw2. Just run it and you should have a blank applet in front of you.


Time to Play!


Once you have the appet opened in front of you, press the ENTER key a few times. You should see some pink rectangles appear on the screen.

CSC352Homework2 2013 Packing1.png


If you keep on going, you should see that as you get closer to the right side of the applet the rectangles are getting smaller and smaller. That's because the collection of rectangles being packed is random, and sorted from largest area to smallest area, and the packing algorithm you are given always packs the largest rectangles first.


Time to Read the Code


The only two java programs you need to read carefully are PackingHw2 and Packer. The header of PackingHw2 indicates how to modify the program to make it pack on its own. Try this modification.

Keep on reading and playing with the code until you feel you understand how both files work. You do not need to study the other java files to do this homework assignment.

Your Assignment


Your assignment is to do something very similar to what you did in the Producer-Consumer lab.

You need to add a new thread to the main application (create it in a new class, in a new java file called PackThread) that will be given the list of rectangles to pack (sorted or not) by the setup() function, and then will pack the the rectangles as fast as it can, and pass them back to draw() in a thread-safe data structure of your choice. Draw() will display the rectangles it gets back, as it gets them back, as fast as it can.


Questions