Lab 1

Java Programming Fundamentals

The main purpose of this lab is to refresh your memory of programming in Java. We will learn a few new things also so as to be prepared for the new course. Most of the exercises are organized around a smartphone store app as described in the Application Context section, in which you can apply knowledge, or acquire new knowledge along the way, in the following aspects:

-  Functions commonly provided with almost all software systems

o  Data input (either from user directly or from a file) and result presentation;

o  Data processing, such as

§  Parsing textual data input for information (with data type conversion when necessary);

§  Organizing a number of values with a data structure;

§  Using repetition and decision constructs as appropriate; and

§  Formatting results into information useful to targeted users.

-  Use (more accurately, reuse) Java APIs for features readily available, such as

o  “value” objects, e.g., String and Number objects;

o  Utility classes, e.g., Random, Scanner, and Arrays/Collections; and

o  IO/GUI objects, e.g., File and JOptionPane.

Application Context

All the specific scenarios are inspired by a simplified store that sells smartphones where a customer can select a phone, an area code, and optionally, a contract. Phone models with prices, as well as area code options are store in phoneinfo.txt. Simple GUIs will be created using the javax.swing.JOptionPane class (see http://docs.oracle.com/javase/6/docs/api/ for examples).

OO design is not the main concern in this lab. You may use a main method to start with, and refactor your code from time to time by using helper methods to fulfill the requirements so as to keep the main method clean.

Programming Exercises

The first two scenarios are used here to introduce the JOptionPane class, which can be used to replace System.out and Scanner for better user interactions.

1.  Prompt the customer for name and say hello with only the first name.

Try to use the showInputDialog method of the JOptionPane class to seek for name input and the showMessageDialog method to present the greeting.

2.  Ask the customer if a contract is needed.

Since a yes-no question is appropriate in this case, the showOptionDialog method with a YES_NO_OPTION will be used.

To integrate with the code for the previous step, drop the message dialog and use this option dialog instead: adjust the wording on this step accordingly.

Phone pricing and area code data will be needed in the following scenarios. We will practice how to read text from a file using the Scanner and File APIs.

3.  Read phone data from a text file and parse useful information for later usage.

First take a look at the phoneinfo.txt file for the way data is stored.

Instantiate a Scanner object that takes a File object as argument, and use the file name to instantiate an anonymous file object for the scanner. Notice that the file needs to be located in your project root folder and the checked IO exception needs to handled properly. Separate lists (or arrays) can be used to hold phone model name, pricing (2 values per model), and area code data.

(The StringTokenizer class can be used to help parse phone model name and prices.)

4.  Present phone models with appropriate prices to the customer.

Only the prices reflecting the customer’s contract selection will be in display, and a dropdown list will be shown using the showInputDialog method with another arrangement. The phone models should be displayed in ascending order on model names.

[See the sample class InputDialogWithDropdownListbox on the last page to learn how to include a dropdown list in an input-dialog box.]

5.  Present area code options to the customer.

Only the 3-digit area code will be in display, though the next 3 digits are fix for a given area code. The options will be shown as buttons using the showOptionDialog which takes an array of options.

6.  Show invoice to the customer with all information from the previous steps.

Format the information well. Use the showMessageDialog method to show final results to the customer. (The showConfirmationDialog can be used here for the user to confirm or cancel the purchase, which can make the application more realistic.)

7.  Beautify your GUIs by using a store logo image (optional).

An icon can be specified when the showXXXDialog method is used. A simple way to create an icon is to instantiate an ImageIcon with a file name. You may download/create an image file and use it. Or you may also paint the image with a custom Icon class.

Due by Tuesday 1/23 when class begins. Please create a Lab 1 folder for your code and other resources with your IDE. To submit your work, please copy your code and data file(s) used into a Word file, each starting on a new page. Also to include typical screenshots for the steps mentioned in the lab description. Submit your report on Canvas.

import javax.swing.JOptionPane;

public class InputDialogWithDropdownListbox {

public static void main(String[] a) {

String[] choices = { "A", "B", "C", "D", "E", "F" };

String input = (String) JOptionPane.showInputDialog(

null, // No main window, stand-alone dialog

"Choose now...", // Message

"The Choice of a Lifetime", // Title

JOptionPane.QUESTION_MESSAGE, // Use a default icon: with a ?

null, // No custom icon

choices, // Array of choices

choices[1]); // Initial choice

System.out.println(input);

}

}