Lab Exercises 2013

advertisement
COMP285 Lab Assignments
Fall 2013
Professor Michael Werner
Version 1
Lab 0 Installing JDK and eclipse
1. Obtain the Java SE Development Kit 7 from Oracle
2. Obtain the eclipse IDE www.eclipse.org/downloads/
3. Submit a Hello World program. Submit the Java file plus a sample run. The program should
print “Hello world from …”. (insert your name)
Lab 1 Using Console I/O
These problems are interactive with a user in a console window. Messages to the user are printed
using System.out.print commands and user inputs are scanned using java.util.Scanner.The code
can be run within eclipse or from a command session.
1. The user inputs a money amount by specifying numbers of quarters, dimes, nickels and pennies.
The program gives the amount in dollars and cents, i.e. 6 quarters, 0 dimes, 4 nickels and 2 pennies
is returned as $1.72.
2. The user inputs a dollar and cents amount to be returned in coins, i.e. 1.72. The program computes
the least number of coins needed, in this case 6 quarters, 2 dimes and 2 pennies.
3.
Create a Date Format Converter: US date: mm/dd/yy (user’s input) -> Europe date: dd.mm.yy
(output). Use methods of the class String.
Lab 2 JOptionPane & Applets
1. Redo the Lab 1 problems, this time using JOptionPane for I/O.
2. Write an applet that displays your initials in large block letters using the canvas class.
3. Rewrite the applet in Part 2 as a Java Application using JFrame.
Lab 3 Strings
1. Develop a Caesar’s Cipher program. The input is a string which may contains letters, numbers and
other ASCII characters. Only the letters are encrypted. The user enters the plain text source string
and a negative or positive integer (the shift). The shift is added to the ASCII value of each letter.
When letters are shifted beyond ‘z’ of ‘Z’ they are wrapped around. i.e. “Crazy” becomes “Etcba”
when shifted by 2. The same program is used to decipher when run with the negative of the shift.
2. Revise Problem 1 to allow decryption even when the shift is unknown. You may assume a Caesar’s
Cipher algorithm was used with a shift in the range 1 .. 25. Simply try all possibilities and show a
table of results. The user picks out the one which looks like English.
3. (Optional for nerds) Instead of the user picking the best result your program could look up the
words in a dictionary. Charge a penalty for each unfound word. The result with the lowest penalty
score wins. (Not guaranteed to succeed)
Lab 4 GUI
1.
Revise Problem 1 in Lab 3 to use a GUI interface. Your JFrame should have labeled text
fields for the user to enter the plain text and to display the ciphered message. There should
also be a text field to enter the shift. Have two buttons, one to encrypt and one to decrypt,
and add an action listener to each. When the decrypt button is pressed a 25 row text area
should be filled with a numbered list of the various shifted values of the ciphered
message. The user should be able to enter a row number and the selected row appears in the
plain text box.
2. (Optional - 5 points extra credit) Follow up on Part 1 by having your program read the plain
text from a file and print the cipher text to another file. You can add a File menu to your
frame with menu items to choose an existing text file for input (plain text) and choose a path
and filename for output (cipher text). The JFileChooser class can help.
Lab 5 Creating Classes
1. You will create a Customer class. This class could be useful to a plumber or a dentist, or for that
matter any company that needs to track its customers.
Data Fields:
 id (assigned automatically)
 name
 address
 phone
 email
 beginDate
 endDate
Constructors:
The id is an int that is assigned automatically starting from 100. There should be a static variable named
lastId which is incremented each time a constructor is called. A constructor increments lastId and uses
that value for id. Create at least two constructors, the default constructor (no arguments) asks the user
to enter values for the fields except for id. There should also be a constructor with parameters for every
field except id.
Destructor:
Fills in the endDate.
Methods:
 Getters and setters for each field. (No setter for id)
 sendMsg(String message) – sends an email message to the customer
2. Create a Company class.
Data Fields:
 name
 customers – A collection of Customer objects
Constructors:
 Company()
 Company(String name)
Methods:
 newCustomer()
 endCustomer() //Fills in the endDate for the customer. It does not destroy the record.
 sendBroadcast() //sends a message to all customers
Testing the Customer and Company classes
Create a class named TestCompany that is used to construct a sample company and populate it with
customers. The TestCompany class will use its main() method to do the tests.
Lab 6 Create a Customer GUI
1. Create a JFrame GUI named CustomerFrame to show customer records
2. Add methods to the Company class to open the Customer GUI in either view or edit mode. (edit mode
will be used in Lab 7).
3. The GUI has a field for finding a customer by id.
4. It also has a field for finding a customer by name (or fragment of name). In the case of multiple
matches a screen (JOptionPane) pops up and lets the user make a choice.
Lab 7 Enabling the CustomerFrame to edit customers
In Lab 5 a TestCompany class was used to drive the Company-Customer system, i.e. to test it using a
script of predefined tests. In Lab 7 the goal is to shift the system to be event driven.
1.
2.
3.
4.
A user will be able to open a customer record in edit mode and change field values.
There will be a way to add a new customer and to end an existing one.
A JFrame will also be developed for the Company class named CompanyFrame.
CompanyFrame will have a button or menu item to launch CustomerFrame.
5. The Company class will include a main() method to launch CompanyFrame. Once launched all
system actions will be event driven.
6. The TestCompany will no longer be used.
Lab 8 Adding persistence to the Company system
1. Add menus to the JFrames, including a File menu with options to save and load.
2. Existing data can be saved to a text file. This file could be in XML form. An alternative is to use
java object streams (http://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html) or
serialization.
3. The system can be loaded from a file
4. A simple log file should be kept recording changes to the Customer data.
Download