ISE 582

Spring 2010
Assigned: March 26
Due date: March 29, 11:59pm
Textbook chapters 142
Read carefully and strictly follow all directions! (worth 10 points)
This midterm is a take home, open book (notes, web, etc.) programming assignment. You are expected to complete the midterm on your own without external help. Please review the Academic Integrity Policy described on the course syllabus in case of doubt.
To complete the exam, you will need the template, code and data files stored at: wwwclasses.usc.edu/engr/ise/582/2010/Midterm
You must answer the questions without any modification to the code provided outside of the methods specified. In particular, you cannot change method signatures or class definitions and interfaces. In most cases, however, you will have to replace the code provided in the specific methods addressed in the questions with your own code.
The assignment consists of 5 questions that can be completed independently and in any order. It is advised (but not required) to solve the questions in the order they are given, as debugging will be easier. The total number of points achievable is 150; the exam will be graded out of 100. Excess points will be counted as extra credit. A score of 100 should be achievable in about 3 hours of work.
10 points will be assigned to compliance with submission and presentation instructions. The grading of each coding assignment will account for correctness, style, clarity and presentation. As usual, copious comments and documentation are key.
The deadline for submission is Monday March 29, 11:59pm.
Submit your answers in one single text file named
<first name>_<last name>midterm.txt
For your convenience, a template is provided at: wwwclasses.usc.edu/engr/ise/582/2010/Midterm/submit.txt
You can copy this file to write/paste your answers in the designated sections and submit the properly renamed resulting file.
You are going to contribute to the development of a graphical application that draws a fractal snow flake. Existing code for the application can be found in: wwwclasses.usc.edu/engr/ise/582/2010/Midterm/SnowFlake
The SnowFlake application follows the modelview approach, as seen
in class and as described in the textbook (ch. 4142).
The SnowFlake
application class
extends JFrame
. The FractalFlake
class
(model) extends Observable
and implements
FractalFlakeInterface
. The Graph
class
(view) extends JComponent
and
implements GraphInterface
.
The code provided already implements an application that compiles and runs, but does not meet all the requirements. Your task is to implement a few key methods for which only partial (donothing or doverylittle) implementation is provided.
→ Compile and run the SnowFlake
application
The requirements for the final application are as follows:
The questions below refine these requirements and guide you through producing code that satisfy them.
hint: the questions below give rough algorithms for the code to implement. You can look for coding inspiration and hints in the partial implementation provided.
As a warmup, implement the code in method main
to set
the fractal subdivision level (first argument) and base polygon file
name (second argument) if provided.
public static void main(String arg[]) { int level=2; String fileName=null; // read level from command line if supplied (first argument) // read base polygon file name from command line if supplied (second argument) new SnowFlake("SnowFlake",level,fileName); }
→ Submit the code for method main
(20 points)
The Graph.paint
method makes use of
the drawPolygon
method to draw the fractal snow flake.
The model stores vertices as instances
of FractalVertex
, whose coordinates are floating point
values between 0.0 and 1.0.
Graph
stores vertex coordinates of type integer,
expressed in window coordinates, in two arrays xVertices
and yVertices
.
computePolygon
computes the window coordinates of the
fractal vertices (parameter of type Vector
) based on
current component size. The drawing area is centered in the window,
with a margin of 10% of the width in the x direction, and 10% of
window height in the y direction (hint: look at the example developed
in class on 03/05). Before computing the vertices, the method updates
the number of vertices and instantiates new coordinate arrays if
necessary.
a fractal vertex of coordinates x,y (between 0.0 and 1.0) maps to window coordinates u,v, where:
The computePolygon
method signature and algorithm are
as follows:
public void computePolygon(Vector fvertices){ // compute window offsets and scaling factors // if necessary, instantiate new coord arrays // compute and store window coordinates of each fractal vertex // force redraw }
→ Submit the code for method computePolygon
(20 points)
A fractal is a shape that is recursively constructed or selfsimilar, that is, a shape that appears similar at all scales of magnification. Fractals typically have a simple and recursive definition which produces fine structure at each scale that cannot easily be described using traditional Euclidean geometry. A simple example of a fractal recursively breaks a straight line into four straight lines as illustrated below.
At each level of the recursion, a line segment with endpoints (x0, y0) and (x1, y1) generates four new line segments with the following pairs of endpoints:
FractalFlake
stores the vertices of the fractal shape
as instances of FractalVertex
, in instance
variable vertices
of type Vector
.
The vertices coordinates are floating point values contrained by construction between 0.0 and 1.0.
FractalFlakeInterface
defines a required method
named computeVertices
that computes new vertices for the
fractal snow flake at a given subdivision level.
Implement a utility recursive method fracLine
that
computes and the vertices of a fractal line subdivided to a maximum
level into a polygon as in the figure above (use the formulas above
for intermediate vertices).
Recall that a recursive function (textbook ch. 25) is a function that calls itself. A recursive method must include one or several base cases (simple cases) and one or several recursive cases, i.e. that call the method to solve subproblems.
The fracLine
method signature and algorithm are as
follows:
private void fracLine(FractalVertex v0, FractalVertex v1, int l){ if(l==0){ // base case // store vertex v0 } else{ // recursive case // compute vertex vA // compute vertex vB // compute vertex vC // call fracLine for segment v0,vA at level l1 // call fracLine for segment vA,vB at level l1 // call fracLine for segment vB,vC at level l1 // call fracLine for segment vC,v1 at level l1 } }
→ Submit the code for method fracLine
(30 points)
computeVertices
calls on method fracLine
to compute the vertices of a fractal polygon whose base shape is a
polygon (by default an equilateral triangle), whose vertices are
stored in the vector baseVertices
.
Implement the computeVertices
method, whose signature
and algorithm are as follows:
public void computeVertices(int level){ // clear current vertices // call fracLines to compute the vertices of fractal lines // defined by the vertices stored in baseVertices, // subdivided to provided level // don't forget to close the polygon // set changed indicator and notify observers }
→ Submit the code for method computeVertices
(30 points)
loadBasePolygon
reads base polygon vertices from a
text file containing x and y coordinates (floating point values
between 0.0 and 1.0).
Implement the code for method loadBasePolygon
. Use
the tokens method to read the coordinates. You can assume the polygon
files do not contain incomplete data.
// read vertices of base polygon from file // must have at least 3 vertices // otherwise, revert to default equilateral triangle private void loadBasePolygon(String fileName){ // instantiate vector if necessary // clear existing base vertices // read vertices from file (use token method) try{ // create input stream, reader, tokenizer // while not at end of file, // read x, read y, add new FractalVertex of coord (x,y) to baseVertices // assume the input file has no incomplete data // close stream // must have at least 3 vertices // otherwise, revert to default equilateral triangle } // catch exceptions }
→ Submit the code for method loadBasePolygon
(40 points)
Note: a few base polygon sample files are provided (.poly files), feel free to experiment!