Practice pieces


As you go through the course, you may find you want some example projects to cut your teeth on, but for which you can find some hints and/or answers. This page supplies some of these.


 

Data Storage

The practical code will expand over the next few practicals so that it holds a 2D raster surface as values in a 2D array. However, we could instead store the x/y coordinates and attributes of vector points in our array. Try taking a copy of the second practical and adapting it so that rather than storing an array of doubles, it stores an array of Point objects, each containing an x and y value. Try setting and getting back a couple of x values from within the array. (Make sure you take a copy of your practical code, as we'll work with the double-array version). You'll need to make a third 'Point' class, as we saw in the lecture.

[hints]
[answers and more ideas]


 

Loops and Branches

1) There are two rabbits. Each pair of rabbits has four young, then dies. How many rabbits are there in the 20th generation? Hint: write the code then test it with a calculator for the first three generations.

[hints]
[answers and more ideas]

2) Build a program that produces the following output efficiently:

1: This is odd
2: This is even
3: This is odd
4: This is even
5: This is odd
6: This is even

[hints]
[answers and more ideas]


 

Methods

1) Make a Calculator main class which adds two numbers together using a method inside itself.

2) Then make the Calculator main class have an associated IntCalc class that contains methods that take in two ints and return the result of various mathematical operations, for example, adding the two numbers, or multiplying the two numbers. Pass in the two ints from the Calculator class.

[hints]
[answers and more ideas]


 

Inheritance

Make "land" and "sea" classes that extend the Point class you made above, but also have their own methods and variables. Prove to your own satisfaction you can access the Superclass's methods and variables inside objects made from the new classes.

[hints]
[answers and more ideas]


 

Packages

ArrayList is a popular replacement for arrays which can be imported from the java.util package. ArrayLists expand as you add things, but are easily converted to arrays. Make an Alice class that stores an ArrayList of Strings, each String randomly set to either "Tweedledum", or "Tweedledee" with an equal chance. Add 100 Strings to the ArrayList. Finally, run through and delete any that contain "Tweedledum".

[hints]
[answers and more ideas]


 

Others' Code

Using the above code, make your Point class' getX() method throw an exception of your own making if the x value is out of range - say over 100. Make a class that uses Point's getX() method to prove to yourself that it won't compile without a try-catch block.

[hints]
[answers and more ideas]


 

Efficiency

Now you've got the hang of the core language, can you make this code more efficient: PoorCode.java?

[hints]
[answers and more ideas]


 

Input/Output

Write a simple application that takes text from the command line, and writes it to a text file using a text file writer until the user enters an empty line.

[hints]
[answers and more ideas]


 

GUIs

Make a Frame with a Button on it in the SOUTH position. When the button is pressed, make the text on the button change from "Press me" to "Pressed", using the ActionEvent's getSource() method.

[hints]
[answers and more ideas]


 

Drawing

Make a GUI that reads in this file containing vector graphics data and draws it on the screen: coordinates.txt (contains a column of x and a column of y locations representing points that, when joined together, make a shape). Start from these files (should just need a paint method in the Frame class): Analyst.java; IO.java; Storage.java.

[hints]
[answers and more ideas]


 

Networks

Use getImage and graphics.drawImage to get an image from your personal webspace and draw it on an Applet. Here's an image, if you can't think of one.

[hints]
[answers and more ideas]


 

Coding for Real

Relax++, geek god/dess.