University of North Carolina at Asheville

CSCI 273.002: Mathematical Programming


Lab 12

Creating Data Types

[Introduction] [Instructions] [What To Submit]


Introduction

In this lab you will work with two kinds of class definitions: some that implement client programs or applications, and some that provide specifications for creating and manipulating objects of various types. So far in this course, you have seen many examples of client programs, but from now on you will be working more and more with object-oriented classes. In general software development situations, such classes may be intended to represent real-world entities like cars or alarm clocks, or they may represent abstractions like complex numbers.

As usual, some of your tasks are essentially just tests of textbook examples of these two kinds of classes. However, you will also be asked to create your own object-oriented class definitions.

Instructions:

To complete this project, follow the steps listed below:

  1. Login to your home directory and launch a terminal window. Use the cd command to change your default directory to csci/273.002/labs. Keep this window open for future reference.
  2. Launch the NetBeans IDE and raise the New Project dialog. Define the project to be a Java Application named Lab12. Make sure that the project folder will be created within your csci/273.002/labs folder. As in your previous labs, you should also uncheck the Create Main Class checkbox before you click Finish.
  3. Some of the classes you will be downloading or creating for this lab are clients of library classes in the JAR file Booksite.jar. So your next task is to make all those library classes available to your Lab12 project. First make sure that your version of Booksite.jar is still in your csci/273.002/labs folder. If not, download Booksite.jar into csci/273.002/labs now. Then repeat the same procedure in the NetBeans IDE that you first performed as Step 6 in Lab 08.

  4. Download the plain-text versions of the textbook demos Charge.java (Program 3.2.1) and Turtle.java (Program 3.2.4) from the Booksite into the default directory for your Lab12 project. In case you are wondering, Charge is indeed one of the classes that you downloaded last week in Lab 11. One of your tasks in this lab will require you to create a new version of this class for an animated-display application.

    While you are at it, you might as well go ahead and download these additional files: Spiral.java (Program 3.2.5), Complex.java (Program 3.2.6), and Mandelbrot.java (Program 3.2.7). You will be working with all of these demos in this lab.

  5. Open Turtle.java in the NetBeans editor and familiarize yourself with its contents. Hopefully you will soon recognize that Turtle is in the category of object-oriented classes, in spite of the fact that its definition includes a main() method. In particular, you can find examples of instance variables, namely x, y, and angle. You should also recognize the definition of a constructor. And of course you should note that most of its methods do not include the static specifier in their headers, which means that they are instance methods. In fact the only static method in this class is its main(), which is included here only as a facility for testing and debugging the other methods.

    The function of a Turtle object is to draw figures using a restricted set of operations collectively referred to as turtle graphics. Application programs can create one or more Turtle objects to make such drawings.

    Now open Spiral.java in the NetBeans editor. You should see immediately that Spiral is a typical client application, which in this case includes only a main() method. And as you might guess, the purpose of this application is to draw a spiral of some sort using turtle graphics. But you want to study this code and understand precisely how its main() method creates and manipulates a Turtle object (or instance) to perform this task.

    At this point, Build the project and run Spiral from the terminal window. For starters, you might try the command-line arguments suggested in the program header comments.

  6. Now create a new Empty Java File named MySpirals in the default directory. Borrowing as much code from Spiral as you like, implement MySpirals so that it can create an image containing multiple spirals, as in the example shown below:

    You should definitely exercise your own design skills here, but it might really help you to consider creating multiple instances of type Turtle. If each instance is created at a suitable starting location, each one can draw its own spiral.

    When you have finished implementing MySpirals.java, Build the project again and run Myspirals from the terminal window. When everything is working, show your results to your lab instructor.

  7. Your next task in this lab is to add a new Java Main Class to the default directory for Lab12, and use it to create a new client application named Dragon. This class will be somewhat like Spiral in that it will use a Turtle object to draw a figure. In this case the required figure will show dragon curves of order n, for several values of n. Dragon curves are described at various points in your text (see Exercise 1.2.35 on p. 44, Exercise 1.5.9 on pp. 154-155, and Exercise 3.2.26 on p. 412).

    To make things interesting, have your Dragon application display the first six dragon curves (orders 0 through 5) in a single plot. Your results should look something like the image shown below:

    The real key to solving this problem is to use recursive methods to do the drawing. You may be able to get useful hints from your text and the Booksite to get you started. But this is a nontrivial task, and you should not hesitate to wheedle lots of help out of your kindly lab instructor.

  8. Your next task in this lab is a variation of Exercise 3.2.34 in your text. Unfortunately, the authors have actually provided a complete Booksite solution to the problem as stated in your text. But your task here is to make a colorized version of Mandelbrot named (you guessed it!) MyMandelbrot, in which the colors are chosen within the program by direct computation. This is in contrast to the Booksite solution, which reads its colors from an input textfile. Of course the way in which you compute your colors is entirely up to you, but all you really need to do here is produce something other than a grayscale plot.

    When you get MyMandelbrot up and running, take the time to experiment a bit with your color selection formulas. Then show your favorite results to your lab instructor.

  9. To start your last task in this lab, create a new Empty Java File named MyCharge to the default package for Lab12, and start to fill it by copying the contents of Charge.java. Then modify the definition of MyCharge as specified by Exercise 3.2.23 of your text (see p. 411), so that it represents mutable electric charges.

    Next, remove all the current statements in the main() method in MyCharge. Then rewrite main() completely, so that it implements the test client application specified by Exercise 3.2.23.

    Build and run MyCharge. Hopefully you will be gratified with an animation of the sort described on p. 411 of your text.

    To complete the formalities, show your final results to your lab instructor.


What To Submit

When your project is complete and working correctly, demonstrate it to your lab instructor. Then, before you exit NetBeans, clean your Lab12 project. Finally, before you logout, switch back to your terminal and set your default directory back to csci/273.002/labs. Then create a JAR file of your Lab12 project folder, using the command

jar cf Lab12Project.jar Lab12
Please leave both your Lab12 project folder and Lab12Project.jar in your csci/273.002/labs directory for the remainder of the semester.