Objectives

Java Development Environments

One advantage of moving to the full Java platform is the availability of more powerful development environments. You will use one of the following for the remainder of your labs and homeworks:

Exercise 8a.1

If you haven't already done so, use your Java IDE to do the following:

  1. Create a project for your Java-based lab and homework exercises for this course.
  2. Create a new package for this lab exercise named edu.calvin.yourLoginID.lab08.

Use this package for all the exercises in this lab. Take time now to make sure that you can find the package/directory you just created using Linux; you will submit this package/directory in same way that submitted your work for Processing.

Console-Based Java Applications

All the Processing-based applications in this course have been graphically oriented, but Java also supports command-line or console-based applications that read input that the user types on the keyboard and print their output on the textual console. We will not build many console-based applications, but it’s useful to see how they work.

You build a Java program by creating a new Java class with a main() method; you can then run this class as a Java application. To create the class in eclipse, right-click on the package where you want to place the class (in this case, edu.calvin.yourLoginID.lab08) and choose "New"-"Class".

Exercise 8a.2

Use your Java IDE to build a console-based Java application that does currency conversion from US dollars to Canadian dollars. Call it CurrencyConverter and build it in a manner similar to the console-based examples from the text or the lecture. Include a constant representing the conversion factor and decide whether this constant should be public or private, static or instance.

Optional: When you have the US-Canadian conversion working, your instructor may ask you to upgrade your program to do the conversion either from US to Canadian or from Canadian to US. Implement this by allowing the user to enter a currency value and a currency type and doing the conversion based on the given currency type. Use the following algorithm:

Here are three (separate) example runs:

Please enter the currency type and value (e.g., US$ 10.00 or CN$ 20.00): US$ 10.00
	US$ 10.0 = CN$ 10.6755
Please enter the currency type and value (e.g., US$ 10.00 or CN$ 20.00): CN$ 20.00
	CN$ 20.0 = US$ 18.73448550419184
Please enter the currency type and value (e.g., US$ 10.00 or CN$ 20.00): AUSTRALIAN$ 30.00
	Unknown currency type: AUSTRALIAN$

Save this program to turn in later.

You can consult the Scanner API specification for detailed information on how to use the scanner.

Encapsulating Processing Sketches in Java

You can use a Java class to encapsulate a Processing application. The process for doing basically mimics the work done automatically by the Processing IDE when it pre-processes a sketch (see the text and lecture examples). When your PApplet and JFrame controller classes are complete, you can run the controller as a Java application.

Exercise 8a.3 Encapsulate your scribbler application from lab 4a.2 as follows:
  1. Get your scribbler application running as a stand-alone sketch in Processing.
  2. Encapsulate the scribbler sketch in a new Java class called ScribblerPanel. Include a default constructor that sets the initial x-y coordinates to default values and rename your variables using our instance variable naming convention (i.e., myX, myY, etc.).
  3. Create a scribbler-controller class called ScribblerController that declares, initializes and presents a scribbler panel.

When you have this Java application running (see the image to the right), modify it as follows:

  • Add an explicit-value constructor that receives x-y coordinate values and the stroke weight from the controller program and uses them appropriately.

Save this version of the program to turn in.

scribbler GUI

Documenting your Classes

When you build a class, you generally would like to reuse the methods at a later time, and perhaps to advertise them so other programmers can use them as well. To support this, it is a good idea to clearly document the Application Programmer’s Interface (API) to the reusable elements of the class (e.g., the public methods you’ve written). Most development environments provide tools for doing this automatically. Go here to do this in your development environment:

Compare the format of the documentation you produce with the standard Java reference documentation; as you can see that Java produces its documentation using Javadoc as well.

Exercise 8a.4

Add appropriately formatted Javadoc comments to your two new classes and generate a Javadoc API reference for your new application.

Checking In

Submit all the code and supporting files for the exercises in this lab.