Computer Science Homework Solutions
Problem
#161538

Object-Oriented Design

Generate an object-oriented design for a system that keeps tracks of your CD and DVD
collection.
• Identify each of the classes, associated data, and operations for the classes.
• Generate the pseudocode for each of the classes as demonstrated on p. 251.
• Draw a GUI that will create the objects and provide access to each object’s processing
methods. Note: Use the drawing tool in Microsoft® Word or in any other applicable
drawing tool to complete this part of the assignment.

Attached file(s):
Attachments
it210_week7_reading1[1].pdf  View File

Attachment Content Summary (Note: view attachment at the above link before purchasing. Actual attachment content may vary slightly from that shown below.)

it210_week7_reading1[1].pdf
Objects in the Everyday World

The chapter you'reall aroundreadAnythingobjects, and you're is an object, this book is an object,isthat
about to
means. The answer is simple:
object. Objects are
is about probably wondering what on earth
that has properties and a function (or functions) an
us--the chair you're sitting on and
so is the washing machine you use to clean your clothes. Even you are an object.
Consider the washing machine. It certainly has properties--it's made of metal and has a
tub, motor, and gearbox, has certain dimensions, and weighs a few hundred pounds. After writing
a long list of its properties, we may know what a washing machine looks like (which is fine), but
we don't yet know enough to define it. We also have to talk about its functions, the processes it
carries out: The machine turns on, fills with water, agitates, empties, rinses, spins, and turns off.
Finally, we need to know what our object "works on"; in this case, clothes. Put together all these
pieces--properties, functions, and something to work on--and we can completely describe a
useful object.
Here are a couple of other important aspects of a washing machine or, for that matter, any
useful object:
N You don't have to know how it works internally to be able to use it.
N If someone has already built a suitable one, and it's available for purchase (or, better yet, for
free), you don't have to build it yourself.
As another example, consider the chair mentioned above. Here's how we could describe one
of these objects:
N Properties: Made of wood, has four legs, has a seat, has a back, is light brown
N Functions: Holds up a weight applied to the seat and back
N Works on: A person
In programming, objects containing properties (data) and functions (processes) provide
"packaged solutions" to help us solve our programming problems. Defining and creating objects
may initially seem unnecessarily complicated, but their use leads to elegant and efficient ways to
handle complex problems. Moreover, by their very nature, objects ultimately simplify the pro-
gramming process and ensure that we don't have to re-invent the wheel (or washing machine or
chair). ISBN: 0-536-12326-8




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
1 CHAPTER EIGHT 2
More on OOP and GUIs

Throughout this book, we have used a single approach in developing our more
complicated programs: top-down, modular design. In this chapter, we will discuss two
other approaches to program design--object-oriented and event-driven programming.
In Section 2.5, we provided a brief introduction to these topics; here, we will explore
them in more depth. We will first discuss the basic concepts that underlie object-ori-
ented programming (OOP) and then apply this material to the objects that make up
a typical graphical user interface (GUI). To be more specific, you will learn
1. The basic terminology used in object-oriented programming [Section 8.1].
2. How to define classes and create objects [Section 8.1].
3. About the inheritance and polymorphism features of OOP [Section 8.2].
4. About object-oriented program design [Section 8.2].
5. About the objects used in creating a graphical user interface [Section 8.3].
6. How to handle events in programming for a GUI [Section 8.4].
7. About event-driven program design [Sections 8.4, 8.5].
ISBN: 0-536-12326-8




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
8.1 3 Classes you may recall from Section 2.5, an object is a structure comprised of
As
and Objects

data (or attributes) and processes (or methods) that perform operations
on that data. Object-oriented programming (or OOP, for short) refers
to an approach to program design and coding that places an emphasis
on the objects needed to solve a given problem and the relationships
among them. In this section, we will discuss some of the basic concepts
underlying object-oriented programming.

Some Terminology
When learning a new subject, coming to grips with its special terminolo-
gy is not always easy. In the case of object-oriented programming, we
have the additional complication that there are often several terms that
describe the same concept. On the other hand, as you will see, many of

8
the new terms and concepts are analogous to ones with which you are
already familiar.
Classes of objects The fundamental entity in object-oriented programming is the class.
A class is a data type that allows us to create objects; it provides the def-
inition for a collection of objects by describing its attributes (data) and
specifying the methods (operations) that may be applied to that data.
For example, consider the following definition of the class "televison":
1 Its attributes include brand name, model number, dimensions, screen
size, and cabinet color.
1 Its methods include turn it on, change channels, change volume, and
turn it off.
This "television class" just describes what a television is and what can be
done with it; a "television object," on the other hand, is a particular exam-
ple of a televison, such as a Sony XBR32S.
So, to put it simply, the purpose of defining a class is to allow us to cre-
ate objects; an object is just a particular instance of its class. The rela-
tionship between a class and its objects is analogous to the relationship
between a data type and variables of that type. For example, when we write
Declare Number As Integer
the type Integer states what kind of data we are dealing with and what
operations (+, -, etc.) can be performed on it. The variable Number is a par-
ticular instance of the type Integer. It can be assigned a specific integer
value to be used within the program.
Other-object- Let us now take a closer look at the objects themselves. As you know,
related objects are made up of two components: data and operations on that data.
terminology
(We say that an object encapsulates--packages together--data and oper-
ations.) The operations are specified in the class definition; the data are
specific to the particular object under consideration (although the type
ISBN: 0-536-12326-8




244 3 CHAPTER 8 More on OOP and GUIs




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
of data is also specified in the class definition). Be aware that there are sev-
eral alternate names for the two components that make up an object:
1 An object's data are known as its attributes, properties, or state.
1 An objects operations are called methods, behaviors, services, pro-
cedures, or functions.
In this book, we will usually use the terms attributes (for data) and meth-
ods (for operations).

Defining Classes and Creating Objects
If you want to use objects in a program, the first step is to define a class
for each kind of object. The class definition provides the structure of the
objects in it-- the attributes they possess and the methods that may be
applied to them. The following example illustrates the kind of pseudocode
we will use to define a class.

EXAMPLE 1 Recall that a cube is a box-shaped solid in which all sides are of equal 8
length and whose volume is obtained by taking the third power of the
length of a side, V = s3. Suppose that we want to define a class called Cube
that will have
1 Attributes: the length of a side (Side) and the volume (Volume) of the
cube
1 Methods that:
assign a value to the side (SetSide)
compute the volume of the cube (ComputeVolume)
return the value of the side to the program (GetSide)
return the volume of the cube to the program (GetVolume)
(We will discuss the specifics of these methods later in this section.)
To define the class Cube, we use the following pseudocode:
Class Cube
Side As Real
Volume As Real
Subprogram SetSide(NewSide)
Set Side = NewSide
End Subprogram
Subprogram ComputeVolume()
Set Volume = Side ^ 3
End Subprogram
Function GetVolume() As Real
Set GetVolume = Volume
End Function
Function GetSide() As Real
Set GetSide = Side
End Function
End Class
ISBN: 0-536-12326-8




8.1 Classes and Objects 4 245




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
The Function and Subprogram notation we have used in this pseudocode
was introduced in Chapter 7. Recall that the variables within parentheses
in the subprogram header (for example, NewSide in the subprogram Set-
Side) are called parameters. If a particular subprogram has no parame-
ters, notice that we still write the parentheses, ()--see, for example, the
subprogram ComputeVolume.

Data hiding In Example 1, the methods SetSide, GetSide, and GetVolume, are
called access methods. They provide the rest of the program with access
to the object's attributes. SetSide imports a value of the attribute Side
from the main program. GetSide and GetVolume allow the main program
to make use of the values of Side and Volume. This raises the question:
"Why not just pass the values of the variables Side and Volume back and
forth to the program as parameters? The answer to this question is one of
the keys to understanding OOP: In object-oriented programming, we nor-

8 mally want to keep the class variables completely "hidden" from the rest
of the program. This practice of data hiding has a two-fold purpose:
1. It enhances the security of the object's data--the data cannot be
altered except by the means intended, namely, by using one of the
object's methods.
2. It helps shield the inner workings of the object from the programmer.
In OOP, objects work like "black boxes." Although their interface with
the outside programming world (their methods) is made public, the
way in which a method gets its job done and the variables with which
it works is kept private.

To explicitly state which members (attributes and/or methods) of a class are
PROGRAMMING public (available to code outside an object of that class) and which are pri-
POINTER vate to the class (not available outside the class), programming languages use
the keywords Public and Private. The relevant keyword, placed in front of the
variable or method name, specifies the status of that class member. For
instance, in Example 1, to declare all variables as private and all methods as
public, we would rewrite the pseudocode as:
Class Cube
Private Side As Real
Private Volume As Real
Public Subprogram SetSide(NewSide)
Set Side = NewSide
End Subprogram
Public Subprogram ComputeVolume()
Set Volume = Side ^ 3
End Subprogram
Public Function GetVolume() As Real
Set GetVolume = Volume
End Function
ISBN: 0-536-12326-8




246 3 CHAPTER 8 More on OOP and GUIs




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
Public Function GetSide() As Real
Set GetSide = Side
End Function
End Class
Attributes are normally declared to be Private (to protect their integrity).
Methods are declared as Public if they are part of the interface between the
object and program, and as Private if they are only used internally--within the
class itself.

Creating objects Remember: Defining a class is analogous to creating a data type, and
just as a data type (like Integer) cannot be referenced within the pro-
gram, neither can the name of the class. So, once we have defined a class,
we need to create one or more objects of that class, which can be refer-
enced within the program. (In OOP language, we need to create an

8
instance of the class; that is, we must perform an instantiation operation.)
This is typically done by means of a declaration statement placed in the
main program. For example, in this book, we use the statement
Declare Cube1, Cube2 As Cube
to create two objects, named Cube1 and Cube2, of the class Cube.
Once they are created, we can make use of the objects Cube1 and
Cube2 in our program, but we need a notation for doing so. In this book,
we will use a "dot notation" that allows us to refer, using a single expres-
sion, to both the object and method or attribute under consideration. For
example, to assign a value of 10 to the Side attribute of Cube1 (see Exam-
ple 1), we will use the statement:
Call Cube1.SetSide(10)
This statement calls the subprogram (method) SetSide, assigning the val-
ue 10 to its argument, NewSide, in the process. To ensure that this method
is setting the Side of the object Cube1 (not that of Cube2) equal to 10,
we place Cube1 in front of the subprogram name, separated from it by a
dot (period). As another example, to display the Volume attribute of
Cube2, we use the statement:
Write Cube2.GetVolume
In general, to refer to a public member (attribute or method) called
MemberName, of an object called ObjectName, we use the notation:
ObjectName.MemberName
The next example further illustrates this notation.

EXAMPLE 2 The following program makes use of the Cube1 object in the class Cube
(defined in Example 1). This program inputs a number from the user that rep-
resents the length of the side of a cube and displays the volume of that cube.
ISBN: 0-536-12326-8




8.1 Classes and Objects 4 247




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
Main
Declare Cube1 As Cube
Write "Enter the length of the side of a cube:"
Input Side1
Call Cube1.SetSide(Side1)
Call Cube1.ComputeVolume()
Write "The volume of a cube of side ", Cube1.GetSide
Write "is ", Cube1.GetVolume
End Program
Notice that there are four calls in this pseudocode to the methods of
the object Cube1; two subprogram calls--to SetSide and Compute-
Volume--and two function calls--to GetSide and GetVolume. (In OOP
language, each of these calls is referred to as a message to the appro-
priate method.)

8 1 The first subprogram call, Call Cube1.SetSide(Side1) , assigns the num-
ber input to the subprogram variable Side.
1 The next subprogram call, Call Cube1.ComputeVolume() , sets Volume =
Side^3.
1 The statement Write "The volume of a cube of side ", Cube1.GetSide dis-
plays the value of Side.
1 The statement Write "is ", Cube1.GetVolume displays the value of Volume.

In Example 2, a program error will result if the main program calls the func-
PROGRAMMING tion ComputeVolume before its Side attribute is given a value. To prevent
POINTER this (and for other reasons, as well), object-oriented programming languages
supply an easy way, through the use of a constructor, to initialize an object's
attributes. A constructor is a special method included in the class definition
that automatically performs specified setup tasks (such as initializing the
object's attributes) when an object is created.


Reading Check 8.1
1. What are the two major components of an object?
2. What is the relationship between a class and objects in that class?
3. What is the difference between public and private class members?
4. Define a class called InAndOut that has one attribute named Value
(of type Real) and two methods:
N SetValue imports a value of the attribute Value from the main
program.
N GetValue returns the value of the attribute Value to the main
program.
5. What is a constructor?
ISBN: 0-536-12326-8




248 3 CHAPTER 8 More on OOP and GUIs




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
8.2 3 MoreInon Object-oriented Programming
this section, we will continue our discussion of object-oriented pro-
gramming (OOP). We will describe additional features that provide OOP
with versatility and power, and also discuss the process of object-ori-
ented program design.

Inheritance and Polymorphism
To contrast it to OOP, the approach to programming that makes use of
top-down modular design is known as procedural programming. Most
early programming languages, such as FORTRAN and BASIC, did not
support the use of classes and objects, and are known as procedural lan-
guages. Most modern languages, on the other hand, allow the program-
mer to make use of objects, in addition to providing the tools found in a
procedural language. However, to take full advantage of the power of

8
OOP--to truly support object-oriented programming--a language must
include the following features:
Characteristics of 1 Encapsulation--The incorporation of data and operations on that
an object-oriented data into a single unit in such a way that the data can only be accessed
language
through these operations. This is the fundamental idea behind class-
es and objects.
1 Inheritance--The ability to create new classes that are based on
existing ones. The methods (operations) and attributes (data) of the
original class are incorporated into the new class, together with meth-
ods and attributes specific to the latter.
1 Polymorphism--The ability to create methods that perform a general
function, which automatically adapts itself to work with objects of
different classes.

Some Benefits of Object-oriented Languages Object-oriented programming tools
TIP have been in use for several decades, but only started to gain great popularity
in the late 1980s. There are two basic reasons for this development:
N During the 1980s, programs became more complex as demand grew
for sophisticated applications such as word processors, graphics pro-
grams, and computer games. Programs of this sort had so many options
and possible outcomes that keeping track of their subprograms became
a nightmare. Due to the self-contained nature of objects and the properties
of inheritance and polymorphism, OOP is better equipped than procedural
programming to deal with these extremely complex programs.
N The graphical user interface (GUI), popularized by the Apple Macintosh in
the mid-1980s, slowly became almost universal for computers. A GUI is
comprised of objects (windows, boxes, buttons, etc.), so OOP became
the natural way of programming for these interfaces.
ISBN: 0-536-12326-8




8.2 More on Object-oriented Programming 4 249




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
The concept of encapsulation was discussed in Section 8.1; inheri-
tance and polymorphism will be described below.

Inheritance In everyday life, we often classify things. For example,
trucks and cars are kinds of vehicles, and station wagons, convertibles,
and sedans are all types of cars. In a more mathematical way, we can say
that the set of cars is a subset of the set of vehicles and the set of con-
vertibles is a subset of the set of cars. To picture the relationships among
these sets, we can use a kind of hierarchy chart, as shown in Figure 1.
Classifying objects can help explain what they are and how they oper-
ate. For example, if we know what a car is, then in describing a convert-
ible, we don't have to explain the attributes and functions that it has in
common with (that it has inherited from) a car. We just state that a con-
vertible is a car, and present the special features of a convertible that dis-
tinguish it from a car.

8 This concept of classification and inheritance also works in object-ori-
ented programming. Object-oriented languages allow us to create a sub-
class of an existing class. In this case, the existing class is called the base
class (or parent class) and the subclass is called the derived class (or
child class). In creating a subclass, the attributes and methods of the base
class automatically become members of the derived class, together with any
additional attributes and methods defined specifically for the latter.
In OOP, the derived class may or may not be a special type (a subset)
of the base class. Rather, a derived class is created to take advantage of
the methods that have already been defined in an existing class. As an
example, recall the "Cube" class of Section 8.1. Its members are:
1 Attributes--Side, Volume
1 Methods--SetSide, ComputeVolume, GetSide, GetVolume


FIGURE 1
A Hierarchy of Vehicles
Vehicles




Trucks Cars




Station
Sedans Convertibles
Wagons
ISBN: 0-536-12326-8




250 3 CHAPTER 8 More on OOP and GUIs




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
We may consider a cube to be a special type of box--one in which all
sides are equal. If we want to define a class that models another kind of
box, one with a square base but whose height is not the same as the sides
of its base, we need not start from scratch; we can define this new class
to be a subclass of Cube. The next example demonstrates how to do this.


EXAMPLE 3 The following pseudocode gives the definitions of the class Cube (from
Section 8.1) and its subclass, SquareBox. The SquareBox class makes use
of all attributes and methods of the Cube class (although it changes the
definition of the ComputeVolume method) and adds an attribute and two
methods of its own.
Class Cube
Side As Real
Volume As Real
Subprogram SetSide(NewSide)
Set Side = NewSide
8
End Subprogram
Subprogram ComputeVolume()
Set Volume = Side ^ 3
End Subprogram
Function GetVolume() As Real
Set GetVolume = Volume
End Function
Function GetSide() As Real
Set GetSide = Side
End Function
End Class
Class SquareBox As Cube
Height As Real
Subprogram SetHeight(NewHeight)
Set Height = NewHeight
End Subprogram
Function GetHeight() As Real
Set GetHeight = Height
End Function
Subprogram ComputeVolume()
Set Volume = Side ^ 2 * Height
End Subprogram
End Class
In this pseudocode, notice that we specify SquareBox to be a sub-
class of Cube with the statement:
Class SquareBox As Cube
ISBN: 0-536-12326-8




8.2 More on Object-oriented Programming 4 251




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
(Of course, each programming language has its own way of defining sub-
classes.) The derived class SquareBox has the following members:
1 Its attributes are Side and Volume, both inherited from Cube, and
Height.
1 Its methods are SetSide and GetSide, inherited from Cube, and
SetHeight, GetHeight, and ComputeVolume.
A statement (elsewhere in the program) like
Declare Box As SquareBox
creates an object called Box of the class SquareBox that can take advan-
tage of all these attributes and methods. For example, the following state-
ments assign the values 10 and 20 to the Side and Height attributes,
respectively:
Call Box.SetSide(10)

8 Call Box.SetHeight(20)

Overriding a In Example 3, notice that a ComputeVolume method appears in both
method the base class Cube and the derived class SquareBox. In such a case, for
an object in the derived class, the definition in the derived class over-
rides (is used instead of) the one in the base class. The next example
clarifies this notion.

EXAMPLE 4 With the definition of the SquareBox class as given in Example 3, the fol-
lowing pseudocode inputs values for the side and height of a box from the
user and computes and displays its volume.
Main
Declare Box As SquareBox
Write "For a box with a square base and arbitrary height,"
Write "enter the length of the sides of its base:"
Input BoxSide
Call Box.SetSide(BoxSide)
Write "Enter the height of the box:"
Input BoxHeight
Call Box.SetHeight(BoxHeight)
Call Box.ComputeVolume()
Write "The volume of the box is ", Box.GetVolume
End Program
When the message
Call Box.SetSide(BoxSide)
is sent, the computer "looks in" the class definition SquareBox (the class
of the object Box) for the method SetSide. Not finding it there, the com-
puter looks in the parent class Cube for this method and applies it to the
ISBN: 0-536-12326-8




252 3 CHAPTER 8 More on OOP and GUIs




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
object Box, setting Box.Side equal to the value input. On the other hand,
when the subprogram SetHeight is called, this method is found in the
definition of SquareBox, and invoked. Similarly, when the message
Call Box.ComputeVolume
is sent, it is received by the method ComputeVolume defined in Square-
Box. Thus, the correct formula, Volume = Side ^ 2 * Height, is applied at
this point.

Polymorphism In a hierarchy of classes, it is often the case that some
methods are common to several classes, but that their definitions may dif-
fer from class to class. For example, the ComputeVolume method of Exam-
ple 3 calculates the volume of a cube in the base class Cube and the volume
of a box with a square base in the derived class SquareBox. More gener-
ally, a module might contain definitions of many three-dimensional
objects--cubes, boxes, spheres, cylinders, etc.--each of which has a vol-
ume given by a different formula. It would be convenient if a programmer
were able to use a single method, ComputeVolume, to calculate the volume
8
of any of these objects. Polymorphism allows for this kind of flexibility.
The word polymorphism means "many shapes." In programming, it
allows a method to take on many definitions when applied to objects in
a hierarchy of classes. Although this OOP feature is implemented differ-
ently in various object-oriented languages, the next example shows the
general way in which it works.

EXAMPLE 5 The following pseudocode shows how a method can take on different
forms when applied to different objects. It makes use of the class definitions
given in Example 3 (earlier in this section).
Main
Declare Box1 As Cube
Declare Box2 As SquareBox
Write "Enter the length of the side of a cube:"
Input Side1
Call Box1.SetSide(Side1)
Call Box1.ComputeVolume()
Write "The volume of this cube is ", Box1.GetVolume
Write "For a box with a square base and arbitrary height,"
Write "enter the length of the sides of its base:"
Input Side2
Call Box2.SetSide(Side2)
Write "Enter the height of the box:"
Input Height2
Call Box2.SetHeight(Height2)
Call Box2.ComputeVolume()
Write "The volume of this box is ", Box2.GetVolume
End Program
ISBN: 0-536-12326-8




8.2 More on Object-oriented Programming 4 253




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
The Declare statements create the objects Box1 and Box2 as instances
of the classes Cube and SquareBox, respectively. Thus, for all statements
that refer to Box1, the Cube class definition is referenced. In particular,
when the first message is sent to the method ComputeVolume, the formula
used is Volume = Side^3. On the other hand, for statements that refer to
Box2, the SquareBox class definition is referenced; the parent Cube def-
inition is used only when an attribute or method definition is not found
in SquareBox. Thus, when the second message is sent to ComputeVolume
(this time, preceded by "Box2."), the definition in SquareBox is used, pro-
viding the formula Volume = Side^2 * Height, as desired.

Object-oriented Program Design
As you know, the top-down modular approach to program design (pro-
cedural programming) places an emphasis on determining the subpro-
grams (procedures) that are needed in a program.
8 Object-oriented program design, on the other hand, places an empha-
sis on determining the objects needed to solve the given problem. In fact,
in most cases in which OOP is better-suited to solve a given problem
(such as in programming for a graphical user interface), there is no strong
hierarchy of program modules.
Viewed in terms of the program development cycle--problem analy-
sis, program design, program coding, and program testing--it is the analy-
sis phase that differs the most between the two approaches. In developing
an OOP program, the analysis phase entails:
1 Identifying the classes to be used in the program.
2 Determining the attributes needed for these classes.
3 Determining the methods needed for these classes.
4 Determining the relationships among the classes.
The manner in which these steps are carried out is not so different from
that of the top-down approach. The program designer works with the
program specifications, imagining what kinds of situations the program
will encounter--what types of specific cases it must handle--and deter-
mines the needed objects, attributes, and methods from these scenarios.
In carrying out these steps, there is a natural transition into the design
phase of the program development cycle. Here, as in the top-down tech-
nique, the methods (subprograms) must be defined. Although this is easy
to say, in real-life programs, there may be hundreds or even thousands of
methods. Encapsulating them in objects generally makes it easier to man-
age them. Finally, although the coding and testing phases proceed as in
procedural programming, here too, a benefit of OOP emerges. It is like-
ly that some pre-existing objects can be recycled in this new program;
making use of this reusable code speeds up both coding and testing.
The next example illustrates the analysis phase of an object-oriented
design.
ISBN: 0-536-12326-8




254 3 CHAPTER 8 More on OOP and GUIs




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
EXAMPLE 6 Suppose we want to create a grade management program (like the one
developed in Section 7.5) to input and store student names and test scores,
and compute test averages for each student. An object-oriented solution to
this problem would begin as follows:
1. In analyzing the problem, we decide to use two classes of objects:
N A Student class, whose objects are the students enrolled in the course.
N A Test class, whose objects are the test scores and averages for
each student.
2. The attributes of the Student class are Name and ID number. (There
could be others, such as Major and Rank--Freshman, Sophomore, etc.).
The attributes of the Test class are an array of Scores and AverageScore.
3. The methods for the Student class allow access to its attributes; they
are SetName, GetName, SetID, and GetID. The methods for the Test
class are those that allow access to its attributes together with a sub-
program, ComputeAverage, that calculates the average test score.
4. The classes Student and Test are related; each test score and aver- 8
age refers to a particular student. Thus, we take Test to be a class
derived from the parent class Student, so that the former inherits the
attributes and methods of the latter.
Now that we have determined the classes, attributes, and methods
needed to solve this problem, we must design, code, and test the corre-
sponding subprograms, as well as the program itself. (Section 8.5 presents
a GUI solution to the grade management problem.)

Reading Check 8.2
1. Fill in the blank: In contrast to object-oriented programming, the
top-down modular approach to programming is referred to as
____________ programming.
2. Identify each of the following OOP features using a single word:
a. The incorporation of data and operations on that data into a
single unit in such a way that the data can only be accessed by
making use of these operations.
b. The ability to create new classes that are based on existing
ones and to make use of their data and attributes.
c. The ability to create methods that perform a general function
which automatically adapts to work with certain related classes.
3. Fill in the blanks: When we create a subclass of an existing class,
the new class is called the ____________ class; the original is the
____________ class.
4. List the fundamental steps taken in analyzing a problem to be
solved using object-oriented program design.
5. What is one advantage of OOP over procedural programming?
ISBN: 0-536-12326-8




8.2 More on Object-oriented Programming 4 255




Extended Prelude to Programming: Concepts and Design, Second Edition by Stewart Venit. Copyright © 2004 by Scott/Jones, Inc. Published by Scott/Jones, Inc., in conjunc-
tion with Addison Wesley, a division of Pearson Education.
8.3 3 GraphicalofUserimportant applications of object-oriented programming (OOP)
One the
Interfaces Revisited

is to create programs that employ a graphical user interface. A graphical
user interface (or GUI) is comprised of windows that contain compo-
nents such as menus, buttons, boxes, etc., which allow users to make
choices and issue commands in a simple, intuitive way. In Section 2.5,
we introduced some basic GUI concepts. In this section and the next, we
will explore this topic in greater detail.

Window Components
Figure 2 displays a typical dialog box window (the Print dialog box for the
Microso
Solution
What is this?
By OTA - Overall OTA Rating
Purchase Cost Now
$2.19 CAD (was ~$27.93)
Included in Download
  • Plain text response
  • Attached file(s):
    • CD_DVD_Collection.doc
$2.19 Instant Download
Add to Cart
Why you can trust BrainMass.com
  • Your Information is Secure
  • Best Online Academic Help Service
  • Students find real academic Success
Related Solutions
Browse