xvi Preface
Development Exercises
give students an opportunity
to practice incremental
development.
Level-by-level Organization for
Programming Exercises
Hallmark Features of the Text
Problem Solving
Printing the Initials
Now that we have acquired a basic understanding of Java application programs, let’s
write a new application. We will go through the design, coding,and testing phases of the
software life cycle to illustrate the development process. Since the program we develop
here is very simple,we can write it without really going through the phases. However,it is
extremely important for you to get into a habit of developing a program by following the
software life cycle stages. Small programs can be developed in a haphazard manner, but
not large programs. We will teach you the development process with small programs first,
so you will be ready to use it to create large programs later.
We will develop this program by using an incremental development technique,
which will develop the program in small incremental steps. We start out with a bare-
bones program and gradually build up the program by adding more and more code to
it. At each incremental step, we design, code, and test the program before moving on
to the next step. This methodical development of a program allows us to focus our at-
tention on a single task at each step, and this reduces the chance of introducing errors
into the program.
Problem Statement
We start our development with a problem statement. The problem statement for our
sample programs will be short, ranging from a sentence to a paragraph,but the problem
statement for complex and advanced applications may contain many pages. Here’s the
problem statement for this sample development exercise:
Write an application that asks for the user’s first, middle, and last names and
replies with the user’s initials.
Overall Plan
Our first task is to map out the overall plan for development. We will identify classes nec-
essary for the program and the steps we will follow to implement the program. We begin
with the outline of program logic. For a simple program such as this one, it is kind of
obvious; but to practice the incremental development, let’s put down the outline of pro-
gram flow explicitly. We can express the program flow as having three tasks:
1. Get the user’s first, middle,and last names.
2. Extract the initials to formulate the monogram.
3. Output the monogram.
Having identified the three major tasks of the program, we will now identify the
classes we can use to implement the three tasks. First, we need an object to handle
the input. At this point, we have learned about only the Scanner class, so we will use it
here. Second, we need an object to display the result. Again, we will use System.out,as
it is the only one we know at this point for displaying a string value. For the string
Sample Development
2.5 Sample Development
program
tasks
Sample Development Programs
Most chapters include a sample development
section that describes the process of
incremental development.
Level 1 Programming Exercises ★
5. In the RollDice program, we created three Die objects and rolled them once.
Rewrite the program so you will create only one
Die object and roll it three
times.
6. Write a program that computes the total ticket sales of a concert. There are
three types of seatings: A, B, and C. The program accepts the number of
tickets sold and the price of a ticket for each of the three types of seats. The
total sales are computed as follows:
totalSales = numberOfA_Seats * pricePerA_Seat +
numberOfB_Seats * pricePerB_Seat +
numberOfC_Seats * pricePerC_Seat;
Write this program, using only one class, the main class of the program.
7. Define a new class named Temperature. The class has two accessors—to-
Fahrenheit and toCelsius—that return the temperature in the specified unit
and two mutators—
setFahrenheit and setCelsius—that assign the temperature
in the specified unit. Maintain the temperature internally in degrees Fahrenheit.
Using this class, write a program that inputs temperature in degrees
Fahrenheit and outputs the temperature in equivalent degrees Celsius.
Development Exercises
For the following exercises, use the incremental development methodology to
implement the program. For each exercise, identify the program tasks, create
a design document with class descriptions, and draw the program diagram.
Map out the development steps at the start. Present any design alternatives and
justify your selection. Be sure to perform adequate testing at the end of each
development step.
11. In the sample development, we developed the user module of the keyless
entry system. For this exercise, implement the administrative module that
allows the system administrator to add and delete
Resident objects and
modify information on existing
Resident objects. The module will also allow
the user to open a list from a file and save the list to a file. Is it proper to
implement the administrative module by using one class? Wouldn’t it be
a better design if we used multiple classes with each class doing a single,
well-defined task?
12. Write an application that maintains the membership lists of five social clubs
in a dormitory. The five social clubs are the Computer Science Club, Biology
Club, Billiard Club, No Sleep Club, and Wine Tasting Club. Use the
Dorm
class to manage the membership lists. Members of the social clubs are
Resident objects of the dorm. Use a separate file to store the membership
list for each club. Allow the user to add, delete, and modify members of
each club.
wu23305_fm.qxd 2/17/09 10:38 AM Page xvi