-
Extension 1 for this module (8 points):
Conway's Game of Life
Conway's Game of Life
is a biology simulation that was developed by British mathematician
John Horton Conway in 1970. It is designed to simulate cellular
automation by creating an initial configuration of living and dead cells and
observing how they evolve. Many interesting patterns have developed from the origins
of the original simulation--producing patterns that pulsate, exist into infinity, and even glide
like spaceships.
Directions
In this lab, you will be responsible for building the simulator portion of
Conway's Game of Life (henceforth known as Conway, or Life).
You can then run the game on your own patterns on on patterns that we provide.
The code for this work can be found
conway package of the extensions source folder.
Here is a tour of what you should find there:
Notes
- A Conway game has-a rectangular array of Cell
objects. The constructor for Conway specifies the number of
rows and columns in that array.
To complete the constructor, you must use what you already know about
arrays and objects. Get help as needed,
but be sure to understand what you are doing before you proceed to the next
steps.
- As given to you, the unit test will already pass testcellFunctions, because Cell is already implemented.
- You should proceed with implementation according to the unit tests.
-
This means your first task is to get the constructor to work, so that testinit passes.
What other methods must you complete so that testinit passes?
- From there, proceed with testConwayAccessors.
- From there, proceed with testStep.
- The testPrint test should work once your other methods
are complete. This test prints the board on the console.
Once you have completed all the methods, you can run the Main method to play Conway's Game of Life. There
are many patterns that can be used to test your simulation, some of which can be
found here.
To further debug your code, the visual interface allows you to take one step
at a time.
If the game is not working, use the debugger or print information helpful to
diagnosing the problems you see.
Sample Patterns
Still Lifes
Block
Beehive
Loaf
Boat
Oscillators
Blinker
Toad
Beacon
Pulsar
Spaceships
Glider
Lightweight Spaceship
Perpetual Patterns
Gosper Glider Gun
Block-Laying Switch Engine
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
End of extension 1
-
Extension 2 for this module (3 points):
Automatic generation of Conway initializations
For this extension, you will be working with automatic code generation, which will save you time and energy if
you want to save any of the cell mappings you create in Conway. In order to get credit for this extension, you must implement
the functionality of logAndCapture() in Conway
to capture the current living cells you have on the board and generate the appropriate Java code for creating those cells
in the console.
For example, the Four Blinkers code is captured already,
but if you were
to generate code for it using logAndCapture() the result would look something like this:
Beginning of Log and Capture
setLife(true, 1, 1);
setLife(true, 1, 2);
setLife(true, 1, 3);
setLife(true, 1, 5);
setLife(true, 1, 6);
setLife(true, 1, 7);
setLife(true, 5, 1);
setLife(true, 5, 2);
setLife(true, 5, 3);
setLife(true, 5, 5);
setLife(true, 5, 6);
setLife(true, 5, 7);
End of Log and Capture
The idea is that the code can be copied from the console, pasted into your
Conway class, and when you choose the right menu item from the
interface, the board will be initialized to replicate what you captured.
Once you have logAndCapture() working, use this new tool to automatically generate your own Conway
patterns in myDesignOne(), myDesignTwo(), and myDesignThree(). For credit for this
extension, these patterns should be both intriguing and potentially time-consuming to generate by hand.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
End of extension 2
-
Extension 3 for this module (5 points):
In the matrix package of the extensions source folder,
implement the Matrix class with the following features.
- A constructor that takes a two-dimensional array of type double
and saves it (as an instance variable) as the values of the matrix.
To be safe, your instance variable must be a copy of the
parameter, so that the contents of your Matrix's array cannot be changed beyond
your control.
To copy the two-dimensional array, you must instantiate a new two-dimensional array
and copy the original array's contents into your new array.
- An arraysAreEqual(double[][] one, double[][] two) method that compares the
two arrays to see if they are the same. The two arrays are the same if:
- They agree in the size of both of their dimensions.
- The contents of the two arrays are the same.
The .equals(Object) method included with this lab calls your
arraysAreEqual method, so that Matrix equality of
two matrices depends on the contents of those matrices.
Until this method is working, the rest of the JUnit tests will not work properly.
- A toString method that neatly shows the contents of the matrix.
In your string use "\n" to insert a line break, and use "\t" where you want a tab between elements.
Include JUnit tests that print these strings to the console (using System.out.println) for visual inspection,
in addition to any automated tests you perform.
Your toString method will come in handy for debugging the other methods.
- A scaleRow method that takes a row number (an integer) and a scale factor (a double),
and multiplies that row by the given scale factor. This method does not return anything. It just modifies the matrix.
In this lab, rows are numbered as arrays are indexed. Thus,
the top row in the matrix is row 0, and the bottom row is numbered one less than
the number of rows in the matrix.
- An addRows method that takes two row numbers as parameters, and adds the two rows together, storing the
result in place of the second of the two rows.
- A plus method that takes another Matrix as its parameter and returns the
matrix that results from adding this matrix by the given one.
Matrix addition is only valid when the two matrices are the same size in both dimensions,
so your plus method should throw an
IllegalArgumentException when this is not the case.
- A transpose method that takes no parameters and returns a new matrix that is the transpose of
this one. Recall that the columns of the transposed matrix have the same values as the rows of the original matrix.
- A times method that takes another Matrix as its parameter and returns the
matrix that results from multiplying this matrix by the given one.
Recall that when you multiply two matrices A and B, the value in the
cell at row r and column c of the resulting matrix
is the dot product of A's row r with B's column c.
Also recall that matrix multiplication is only valid when the number
of columns of the first matrix is equal to the number of rows of the
second, so your times method should throw an
IllegalArgumentException when this is not the case.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
End of extension 3
-
Extension 4 for this module (6 points):
Gaussian Elimination
Add a method to your Matrix class to perform Gaussian elimination, as described in this
Wikipedia article.
Write JUnit tests that use your method to solve systems of equations.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
End of extension 4
-
Extension 5 for this module (3 points):
Net Present Value
Calculating NPV:
The present value is a calculation used by investors as a baseline for comparison when making new investments. The present value is seen as the value of an amount of cash were it to be saved in a bank account (or other low risk venture) as opposed to being invested. It can be used to evaluate a cash flow in two different ways: the projection of a current cash flow into the future (capitalization) or the evaluation of expected future earnings (discounting).
The discount evaluation is especially useful when determining the viability of starting a new investment. For example, say that $30,000 has been invested into an opportunity that is expected to return $10,000 each year thereafter for three years. The present value of each year can be calculated by:
PV = C / (1 + r)^t
where C is the expected yearly return, r is the discount rate (typically the risk-free interest rate) and t is the time in years that have elapsed since the initial investment. Using a discount rate of 10% (.10), we can see that the present values for each of the three years are:
Year 1: $9090.90
Year 2: $8264.26
Year 3: $7513.15
It can be seen that the present value for each year is less than the actual return (it has been discounted), since the same amount of money invested at the outset (time = 0) would have accrued interest. The sum of the present values over a given time is called the Net Present Value. The NPV of the above example would be $24868.31. Since the initial investment can be considered a negative cash flow at time = 0, the actual NPV would be -$5131.69 making this a bad investment. More money could have been made by simply placing the original investment into a bank account for the same time period. Putting everything together, the formula for NPV is the sum of all present values from 1 to T minus the original investment.
To assist you with this work, a CashFlows class has been provided
(along with some other useful clases) in the business package in
the coursesupport source folder.
The CashFlows object represents the expected cash flows for a certain number of years. The CashFlows class contains the following methods:
- public CashFlows(Money ... f): takes in expected cash flows for each year and stores them. The amount of flows given can vary, and are stored in the order that they are given. The number of cash flows provided determines the maximum number of years for the investment.
- public int getNumberOfFlows(): returns the number of cash flows currently being stored. This number also represents the maxmum number of years for the investment.
- public Money getFlow(int year): returns the flow for the desired year for 0 < year <= getNumberOfFlows() (since the value at year = 0 is -investment).
Using the information provided above, your task is to complete the following methods within the NpvCalculator class found in the
netpresentvalue package of the extensions source
folder in your repository:
- public NpvCalculator(CashFlows flows, double investment, double discount): The constructor takes in an object of type CashFlows containing all of the estimated cash flows for an investment, the original investment amount expressed as a positive value, and the expected discount rate.
- public Money calculatePV(int year): returns the present value of a cash flow for a given year using the formula outlined above.
- public Money calculateNPV(int year): calculates the net present value from time = 1 to the given year. In other words, the year parameter = T from the above NPV formula. Don't forget to account for the initial investment when returning the NPV.
- public Money calulateTotalNPV(): returns the NPV for the entire set of cash flows provided.
- public Money findMaxNPV(): returns the largest NPV for the given cash flows. This NPV is not necessarily the same as the total NPV described above, and may even be a negative value.
To demo this work, run the NpvCalculator class as a Java Application
and look for the printed results to be similar to what is shown at the top
of this description.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
End of extension 5
-
Extension 6 for this module (8 points):
Julia sets: fractal images
Overview:
Fractals are mathematical models that
can create seemingly complex drawings from very simple specifications.
Because they are not easily bored, computers are well-suited to
iterative tasks.
You will use iteration in this lab to compute
a fractal drawing.
This lab emphasizes how computation can be applied
with relative ease and speed to create what
would take
a human much longer to accomplish.
Before Starting:
- Update your workspace and look for the julia package in
the extensions source folder. To run
the program, run JuliaController as a Java application.
- Please read over the entire document before you start. Really.
It will save you time in the long run.
- If you are interested, see the
wallpaper a student made from
this lab. You can change constants and colors to get different patterns.
- Take a look at the
documentation supplied for this work.
Background Information:
Fractals are an amazing mechanism for describing complex systems in terms
of simple and recursive components.
The idea of fractals in the complex plane (where the x coordinate is real and the y coordinate is imaginary)
dates back to 1918, when
Gaston Julia began research that characterized
Julia Set
fractals. In 1979,
Benoit Mandelbrot defined a map over Julia sets that reflected
the expense of computing a specific point in the set. Such maps
generate interesting patterns, and we will in a sense reproduce
Mandelbrot's work in this lab.
Since Mandelbrot's original work, computers have become faster and Java
has made programming them easier. Thus,
our renditions will be more aesthetically pleasing
(and won't take overnight to render either).
Search
here
(using
Google) to find some pages with more background information.
Problem statement: Drawing a Julia Set
This lab involves computation over some points of a complex plane.
Recall that a complex number has two components:
a real and an imaginary part.
For each complex point c
that we want to display, we compute
the function rigor(c)
as follows.
Pseudocode for rigor(c):
z = -0.7795 + 0.134 i
iters = 0
while abs(c) < 2 and iters < maxIters
c = c*c + z
iters = iters + 1
end while
return iters
In other words, rigor(c)
is the number of iterations
that it takes to compute the value at c
.
The value of maxIters
is arbitrary, but let's assume
for now that
100 iterations are allowed for the computation. The function
abs(z)
computes the distance of z
from
the complex point (0,0).
To show complex numbers on an x-y axis system,
let us assume that the real component of a complex number is registered on
the x-axis; the imaginary component is thus registered on the y axis.
If we apply the above computation over the complex plane as x
ranges from -2 to 2 and y ranges from -2i to 2i,
we obtain the following picture:
(-2, 2i) | (0, 2i) | (2, 2i) |
(-2, 0i) |
 | (2, 0i) |
(-2, -2i) | (0, -2i) | (2, -2i) |
Figure 1: Initial display |
-
Note the complex coordinates of the lower-left and upper-right corners.
We use those two corners to describe the currently viewed area of the
display.
Zooming in, zooming out, and the selection of a box all affect those two
corners, which in turn frame the display that is shown.
- The
black areas represent points whose computation exceeded the limit set above (
maxIters
).
-
In the figures shown in this write-up, the color of a pixel p is
computed as follows, based on the value of iters that was
computed for the Complex coordinate associated with p:
Color color = Color.black;
if (iters < maxIters)
color = Color.getHSBColor((iters % 256)/255.0f, 1.0f, 1.0f);
image.setPixel(i, j, color);
-
The above code uses the HSB color model. For more information,
consult the Color.getHSBColor documentation. The basic idea is to pick a hue based on the number of iterations,
but leave the brightness and saturation at full.
Zooming around
What is really interesting about a fractal drawing is that one
can dive into the drawing and discover ever increasing detail.
Below you see the results of zooming into the picture.
(-1.6, -0.5i) |
|
(-0.6, 0.4i) |
|  | |
(-1.6, 0.5i) |
|
(-0.6, 0.5i) |
|
(-1.51755, 0.063544i) |
|
(-1.51210, 0.063544i) |
|  | |
(-1.51755, -0.0690i) |
|
(-1.51210, -0.0690i) |
|
Zoom in on left part of picture |
Another zoom in on left part of picture |
- Zooming into the picture changes the fractal plane coordinate of
the pixels.
Sedgewick's StdDraw allows you to set the coordinates of the
display.
-
The Julia object offers several ways to zoom, but the most
important method to consider is
setCoordinates.
The other zooming methods can accomplish their tasks by calling
setCoordinates with the appropriate parameters.
- The zoom methods are called by selecting entries on the JuliaController panel:
- ZoomIn and ZoomOut are available.
- Reset sets things back to the way they started.
- The Mouse panel button allows you to click and drag an area of
the display into which to zoom.
Approach
-
Since this may be your first experience implementing code
from a design, you should first peruse the
documentation supplied for
this extension.
- Work on the classes in the following order, testing as you go:
- Complex
- Test Complex using the provided unit tests.
- Julia
Your code for Julia must use the Complex class. You will not get
credit for this extension if your code is sloppy!
- You are provided with JUnit tests for Complex.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
End of extension 6