Supplement: Case Study: AddressBook. For Introduction to Java Programming By Y. Daniel Liang

Similar documents
NETWORKING CHAPTER. Objectives

Advanced Network Programming Lab using Java. Angelos Stavrou

Graphical User Interfaces

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

DHBW Karlsruhe, Vorlesung Programmieren, Remote Musterlösungen

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

CS 335 Lecture 06 Java Programming GUI and Swing

Homework/Program #5 Solutions

Programming with Java GUI components

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

An Overview of Java. overview-1

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

Informatik II. // ActionListener hinzufügen btnconvert.addactionlistener(this); super.setdefaultcloseoperation(jframe.

public class demo1swing extends JFrame implements ActionListener{

Essentials of the Java(TM) Programming Language, Part 1

Using A Frame for Output

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

CS506 Web Design and Development Solved Online Quiz No. 01

Creating a Simple, Multithreaded Chat System with Java

WRITING DATA TO A BINARY FILE

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

During the process of creating ColorSwitch, you will learn how to do these tasks:

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

AVRO - SERIALIZATION

13 File Output and Input

D06 PROGRAMMING with JAVA

INPUT AND OUTPUT STREAMS

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1


JAVA - FILES AND I/O

How to Convert an Application into an Applet.

How To Write A Program For The Web In Java (Java)

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Extending Desktop Applications to the Web

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Course/Year W080/807 Expected Solution Subject: Software Development to Question No: 1

Analysis Of Source Lines Of Code(SLOC) Metric

Java Appletek II. Applet GUI

public static void main(string[] args) { System.out.println("hello, world"); } }

Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

B.Sc (Honours) - Software Development

Assignment # 2: Design Patterns and GUIs

Principles of Software Construction: Objects, Design and Concurrency. GUIs with Swing. toad Spring 2013

Essentials of the Java Programming Language

Swing. A Quick Tutorial on Programming Swing Applications

Tutorial Reference Manual. Java WireFusion 4.1

Introduction to the Java Programming Language

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

What is an I/O Stream?

CS108, Stanford Handout #33. Sockets

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

Konzepte objektorientierter Programmierung

Introduction to Java

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Object-Oriented Programming in Java

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

How Scala Improved Our Java

Summer Internship 2013

Nexawebホワイトペーパー. Developing with Nexaweb ~ Nexaweb to Improve Development Productivity and Maintainability

Chapter 2 Introduction to Java programming

Event-Driven Programming

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Building a Multi-Threaded Web Server

Java Interview Questions and Answers

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Network/Socket Programming in Java. Rajkumar Buyya

Syllabus for CS 134 Java Programming

How to Install Java onto your system

Java Programming Fundamentals

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

Using Files as Input/Output in Java 5.0 Applications

Dev Articles 05/25/07 11:07:33

Illustration 1: Diagram of program function and data flow

Using NetBeans IDE for Desktop Development. Geertjan Wielenga

Specialized Programme on Web Application Development using Open Source Tools

AP Computer Science Java Mr. Clausen Program 9A, 9B

CS 121 Intro to Programming:Java - Lecture 11 Announcements

FILE I/O IN JAVA. Prof. Chris Jermaine Prof. Scott Rixner

Fachbereich Informatik und Elektrotechnik Java Swing. Advanced Java. Java Swing Programming. Programming in Java, Helmut Dispert

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

MVC Table Model / View / Controller

Fundamentals of Java Programming

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Lösningsförslag till tentamen

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Stream Classes and File I/O

16.1 DataFlavor DataFlavor Methods. Variables

Mouse Event Handling (cont.)

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

1 Hour, Closed Notes, Browser open to Java API docs is OK

Communicating with a Barco projector over network. Technical note

Implementação. Interfaces Pessoa Máquina 2010/ Salvador Abreu baseado em material Alan Dix. Thursday, June 2, 2011

Chapter 2: Elements of Java

Transcription:

Supplement: Case Study: AddressBook For Introduction to Java Programming By Y. Daniel Liang This case study can be presented after Chapter 19, Binary I/O. Now let us use RandomAccessFile to create a useful project for storing and viewing an address book. The user interface of the program is shown in Figure 1. The Add button stores a new address at the end of the file. The First, Next, Previous, and Last buttons retrieve the first, next, previous, and last addresses from the file, respectively. Figure 1 AddressBook stores and retrieves addresses from a file. <Side Remark: fixed-length record> Random access files are often used to process files of records. For convenience, fixed-length records are used in random access files so that a record can be located easily, as shown in Figure 2. A record consists of a fixed number of fields. A field can be a string or a primitive data type. A string in a fixed-length record has a maximum size. If a string is smaller than the maximum size, the rest of the string is padded with blanks. file Record 1 Record 2 Record n e.g., Address 1 Address 2 Address n Figure 2 Field1 Field 2 Field k name street city state zip Random access files are often used to process files of fixed-length records.

Let address.dat be the file to store addresses. A RandomAccessFile for both read and write can be created using RandomAccessFile raf = new RandomAccessFile("address.dat", "rw"); Let each address consist of a name (32 characters), street (32 characters), city (20 characters), state (2 characters), and zip (5 characters). If the actual size of a field (e.g., name) is less than the fixed maximum size, fill it with blank characters. If the actual size of a field is greater than the fixed maximum size, truncate the string. Thus the total size of an address is 32 + 32 + 20 + 2 + 5 = 91 characters. Since each character occupies two bytes, one address takes 2 * 91 = 182 bytes. After an address record is read, the file pointer is 182 bytes ahead of the previous file pointer. For convenience, Listing 1 contains two methods for reading and writing a fixed-length string. Listing 1 FixedLengthStringIO.java ***PD: Please add line numbers in the following code*** <Side Remark line 12: read characters> <Side Remark line 23: fill string> <Side Remark line 27: fill blank> <Side Remark line 30: write string> import java.io.*; public class FixedLengthStringIO { /** Read fixed number of characters from a DataInput stream */ public static String readfixedlengthstring(int size, DataInput in) throws IOException { // Declare an array of characters char[] chars = new char[size]; // Read fixed number of characters to the array for (int i = 0; i < size; i++) chars[i] = in.readchar(); return new String(chars); /** Write fixed number of characters to a DataOutput stream */ public static void writefixedlengthstring(string s, int size, DataOutput out) throws IOException { char[] chars = new char[size]; // Fill an array of characters from the string s.getchars(0, Math.min(s.length(), size), chars, 0); // Fill in blank characters in the rest of the array for (int i = Math.min(s.length(), size); i < chars.length; i++) chars[i] = ' '; // Create and write a new string padded with blank characters out.writechars(new String(chars));

The writefixedlengthstring(string s, int size, DataOutput out) method writes a string in a fixed size to a DataOutput stream. If the string is longer than the specified size, it is truncated (line 23); if it is shorter than the specified size, blanks are padded into it (lines 26 27). In any case, a new fixed-length string is written to a specified output stream. Since RandomAccessFile implements DataOutput, this method can be used to write a string to a RandomAccessFile. For example, Invoking writefixedlengthstring("john", 2, raf) actually writes "Jo" to the RandomAccessFile raf, since the size is 2. Invoking writefixedlengthstring("john", 6, raf) actually writes "John " to the RandomAccessFile raf, since the size is 6. The readfixedlengthstring(int size, InputOutput in) method reads a fixed number of characters from an InputStream and returns as a string. Since RandomAccessFile implements InputOutput, this method can be used to read a string from a writefixedlengthstring(string s, int size, DataOutput out). The rest of the work can be summarized in the following steps: 1. Create the user interface. 2. Add a record to the file. 3. Read a record from the file. 4. Write the code to implement the button actions. The program is shown in Listing 2. Listing 2 AddressBook.java ***PD: Please add line numbers in the following code*** <Side Remark line 9: constant> <Side Remark line 18: raf> <Side Remark line 21: GUI component> <Side Remark line 37: open file> <Side Remark line 45: create UI> <Side Remark line 102: register listener> <Side Remark line 104: add address> <Side Remark line 107: register listener> <Side Remark line 110: first record> <Side Remark line 117: register listener> <Side Remark line 122: next address> <Side Remark line 144: register listener> <Side Remark line 150: last address>

<Side Remark line 160: first address> import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class AddressBook extends JFrame { // Specify the size of five string fields in the record final static int NAME_SIZE = 32; final static int STREET_SIZE = 32; final static int CITY_SIZE = 20; final static int STATE_SIZE = 2; final static int ZIP_SIZE = 5; final static int RECORD_SIZE = (NAME_SIZE + STREET_SIZE + CITY_SIZE + STATE_SIZE + ZIP_SIZE); // Access address.dat using RandomAccessFile private RandomAccessFile raf; // Text fields private JTextField jtfname = new JTextField(NAME_SIZE); private JTextField jtfstreet = new JTextField(STREET_SIZE); private JTextField jtfcity = new JTextField(CITY_SIZE); private JTextField jtfstate = new JTextField(STATE_SIZE); private JTextField jtfzip = new JTextField(ZIP_SIZE); // Buttons private JButton jbtadd = new JButton("Add"); private JButton jbtfirst = new JButton("First"); private JButton jbtnext = new JButton("Next"); private JButton jbtprevious = new JButton("Previous"); private JButton jbtlast = new JButton("Last"); public AddressBook() { // Open or create a random access file raf = new RandomAccessFile("address.dat", "rw"); catch(ioexception ex) { System.out.print("Error: " + ex); System.exit(0); // Panel p1 for holding labels Name, Street, and City JPanel p1 = new JPanel(); p1.setlayout(new GridLayout(3, 1)); p1.add(new JLabel("Name")); p1.add(new JLabel("Street")); p1.add(new JLabel("City")); // Panel jpstate for holding state JPanel jpstate = new JPanel();

jpstate.setlayout(new BorderLayout()); jpstate.add(new JLabel("State"), BorderLayout.WEST); jpstate.add(jtfstate, BorderLayout.CENTER); // Panel jpzip for holding zip JPanel jpzip = new JPanel(); jpzip.setlayout(new BorderLayout()); jpzip.add(new JLabel("Zip"), BorderLayout.WEST); jpzip.add(jtfzip, BorderLayout.CENTER); // Panel p2 for holding jpstate and jpzip JPanel p2 = new JPanel(); p2.setlayout(new BorderLayout()); p2.add(jpstate, BorderLayout.WEST); p2.add(jpzip, BorderLayout.CENTER); // Panel p3 for holding jtfcity and p2 JPanel p3 = new JPanel(); p3.setlayout(new BorderLayout()); p3.add(jtfcity, BorderLayout.CENTER); p3.add(p2, BorderLayout.EAST); // Panel p4 for holding jtfname, jtfstreet, and p3 JPanel p4 = new JPanel(); p4.setlayout(new GridLayout(3, 1)); p4.add(jtfname); p4.add(jtfstreet); p4.add(p3); // Place p1 and p4 into jpaddress JPanel jpaddress = new JPanel(new BorderLayout()); jpaddress.add(p1, BorderLayout.WEST); jpaddress.add(p4, BorderLayout.CENTER); // Set the panel with line border jpaddress.setborder(new BevelBorder(BevelBorder.RAISED)); // Add buttons to a panel JPanel jpbutton = new JPanel(); jpbutton.add(jbtadd); jpbutton.add(jbtfirst); jpbutton.add(jbtnext); jpbutton.add(jbtprevious); jpbutton.add(jbtlast); // Add jpaddress and jpbutton to the frame add(jpaddress, BorderLayout.CENTER); add(jpbutton, BorderLayout.SOUTH); jbtadd.addactionlistener(new ActionListener() { writeaddress(); );

jbtfirst.addactionlistener(new ActionListener() { if (raf.length() > 0) readaddress(0); ); jbtnext.addactionlistener(new ActionListener() { long currentposition = raf.getfilepointer(); if (currentposition < raf.length()) readaddress(currentposition); ); jbtprevious.addactionlistener(new ActionListener() { long currentposition = raf.getfilepointer(); if (currentposition - 2 * RECORD_SIZE > 0) // Why 2 * 2 * RECORD_SIZE? See the follow-up remarks readaddress(currentposition - 2 * 2 * RECORD_SIZE); else readaddress(0); ); jbtlast.addactionlistener(new ActionListener() { long lastposition = raf.length(); if (lastposition > 0) // Why 2 * RECORD_SIZE? See the follow-up remarks readaddress(lastposition - 2 * RECORD_SIZE); ); // Display the first record if exists if (raf.length() > 0) readaddress(0);

/** Write a record at the end of the file */ public void writeaddress() { raf.seek(raf.length()); jtfname.gettext(), NAME_SIZE, raf); jtfstreet.gettext(), STREET_SIZE, raf); jtfcity.gettext(), CITY_SIZE, raf); jtfstate.gettext(), STATE_SIZE, raf); jtfzip.gettext(), ZIP_SIZE, raf); /** Read a record at the specified position */ public void readaddress(long position) throws IOException { raf.seek(position); String name = FixedLengthStringIO.readFixedLengthString( NAME_SIZE, raf); String street = FixedLengthStringIO.readFixedLengthString( STREET_SIZE, raf); String city = FixedLengthStringIO.readFixedLengthString( CITY_SIZE, raf); String state = FixedLengthStringIO.readFixedLengthString( STATE_SIZE, raf); String zip = FixedLengthStringIO.readFixedLengthString( ZIP_SIZE, raf); jtfname.settext(name); jtfstreet.settext(street); jtfcity.settext(city); jtfstate.settext(state); jtfzip.settext(zip); public static void main(string[] args) { AddressBook frame = new AddressBook(); frame.pack(); frame.settitle("addressbook"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true);

A random access file, address.dat, is created to store address information if the file does not yet exist (line 37). If it already exists, the file is opened. A random file object, raf, is used for both write and read operations. The size of each field in the record is fixed and therefore defined as a constant in lines 9 15. The user interface is created in lines 44 100. The listeners are registered in lines 102 156. When the program starts, it displays the first record, if it exists, in lines 159 164. The writeaddress() method sets the file pointer to the end of the file (line 170) and writes a new record to the file (lines 171 180). The readaddress() method sets the file pointer at the specified position (line 189) and reads a record from the file (lines 190 199). The record is displayed in lines 201 205. To add a record, you need to collect the address information from the user interface and write the address into the file (line 104). The code to process button events is implemented in lines 102 156. For the First button, read the record from position 0 (line 110). For the Next button, read the record from the current file pointer (line 122). When a record is read, the file pointer is moved 2 * RECORD_SIZE number of bytes ahead of the previous file pointer. For the Previous button, you need to display the record prior to the one being displayed now. You have to move the file pointer two records before the current file pointer (line 135). For the Last button, read the record from the position at raf.length() - 2 * RECORD_SIZE.