University of North Carolina at Asheville

CSCI 273.002: Mathematical Programming


Lab 11

Using Data Types

[Introduction] [Instructions] [What To Submit]


Introduction

In this lab you will write client programs that work with the standard Java library class java.awt.Color. You will also test a Booksite client application based on a textbook class named Charge, which models pointlike electric charges. Some of your tasks are essentially just tests of these client demos. However, a few problems may require a bit of insight and creativity on your part.

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 Lab11. 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. Most of the programs you will be downloading or creating for this lab are clients of library classes that are archived in the JAR file Booksite.jar. So your next task is to make all those library classes available to your Lab11 project. First check whether you still have a previously downloaded copy of Booksite.jar 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 version of the textbook demo AlbersSquares.java (Program 3.1.2) from the Booksite into the default directory for your Lab11 project. Open AlberSquares.java in the NetBeans editor and familiarize yourself with its contents, including the preliminary comments. When you think you can anticipate what this program will do, Build the project and run AlbersSquares from the terminal window. For starters, you might try the color component values suggested in the program header comments. But you should also experiment with at least a few other color combinations before showing your first checkpoint to your lab instructor.
  5. Now create a new Java Main Class named ColorStudy in the default directory. Borrowing as much code from AlbersSquares as you like, implement ColorStudy so that it creates an image like the one shown below:

    You should definitely exercise your own design skills here, but it might help you to define more than one method in ColorStudy (not just the main() method). At the very least, that approach should reduce the amount of code that you have to write in main() itself.

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

  6. Bonus, up to 20 points extra credit:: Create another application named ColorStudy2, which is similar to ColorStudy but produces an image like the following:

  7. To start your last task in this lab, return to the Booksite and download the additional demos Potential.java (Program 3.1.7) and Charge.java (Program 3.2.1) into the default directory for Lab11. Then open both of these files in the NetBeans editor.

    Now take a quick look at Charge.java. In spite of the fact that this class contains a main() method, it does not represent an application, at least not in the same sense as most of the demos you have encountered up to this point. Instead, the class Charge serves as a blueprint for creating software objects that model real-world entities (in this case, electric charges). In this respect, Charge plays a role similar to the role that java.awt.Color plays in color-oriented graphics applications.

    In contrast, Potential represents a client application that creates Charge objects and displays them using color plots. To keep things simple, it requires that all the charges lie in a plane (no 3D graphics here). You should now take a moment to look over the source code in Potential. Note in particular that it expects the standard input stream to provide the number of charges it must display, followed by the x-y coordinates and magnitude of each charge.

    In this lab you will not be asked to define or even modify object-oriented classes like Charge, but you will need to understand how a client application like Potential can create and use Charge objects. This amounts to learning the so-called API (Applications Programming Interface) for the Charge class.

    To start things off, build Lab11 again. Then create a new subfolder of Lab11 named data. Create a new, empty (text) file within data named "charges.txt", and initially copy the following lines into it:

    
    2
    0.3 0.5 10.0
    0.7 0.5 10.0
    
    When Potential reads this file it will create two charges, whose x-y coordinates are (0.3, 0.5) and (0.7, 0.5) respectively, where each charge has magnitude 10.0. In case you care, these numbers are all in standard MKS units. But of course you don't need to care, at least not for the purposes of this lab.

    Now run Potential from the terminal window, redirecting its standard input stream to charges.txt. Once you see what the display looks like for the values shown above, try changing the sign of the first charge (replace 10.0 by -10.0). Incidentally, this configuration of two opposite charges is known as an electric dipole. Then of course, please feel free to add more charges at various locations in the unit square, with varying charge magnitudes.

    Your experimentation should not be confined just to the input data. If you go back to the source code for Potential, you should easily find that the author has actually included more than one possible way of defining the color at each (x,y) point. Only one choice is used, while the others have been commented out. Using a fixed data file, you should now try each of the other color plotting choices in turn (but remember that you will have to rebuild Lab11 after each change).

    To complete the formalities, show your favorite 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 Lab11 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 Lab11 project folder, using the command

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