SlideShare a Scribd company logo
1 of 55
Introduction to Software Design
Chapter 1: Introduction to Software Design 2
Outline
• The software challenge and the software life cycle
• Activities of each phase of the software life cycle
• Using top-down design and object-oriented design
• Managing complexity:
• Data abstraction
• Procedural abstraction
• Information hiding
• Class diagrams document interactions between classes
Chapter 1: Introduction to Software Design 3
Outline (2)
• Abstract data types:
• Role in modeling
• Implementing them with classes and interfaces
• Use cases: tool to document interaction with a user
• Software design process example:
• Design and implementation of an array-based
telephone directory
• Sequence diagrams: tool for documenting the interaction
between multiple classes used in a program
Chapter 1: Introduction to Software Design 4
The Software Challenge
• Software is ...
• Used for a long time
• Updated and maintained
• By people who did not write it
• Initial specification may be incomplete
• Specification clarified through extensive interaction
between user(s) and system analyst(s)
• Requirements specification needed at the beginning of
any software project
• Designers and users should both approve it!
Chapter 1: Introduction to Software Design 5
Things Change!
• Users’ needs and expectations change
• Use reveals limitations and flaws
• Desire for increased convenience, functionality
• Desire for increased performance
• Environment changes
• Hardware, OS, software packages (“software rot”)
• Need to interact with clients, parent org., etc.
• Law and regulations change
• Ways of doing business
• Style, “cool” factor
Chapter 1: Introduction to Software Design 6
The Software Life Cycle
• Software goes through stages as it moves from initial
concept to finished product
• The sequence of stages is called a life cycle
• Must design and document software:
• In an organized way for:
• Understanding and ...
• Maintenance (change) after the initial release
• The maintainer is not necessarily the author!
• ... and even authors forget
• ... and no one can keep all details in mind at once
Chapter 1: Introduction to Software Design 7
Software Life Cycle Models:
The Waterfall Model
• Simplest way to organizing activities in stages
• Activities are:
• Performed in sequence
• Result of one flows (falls) into the next
• The Waterfall Model is simple ... but unworkable
• Fundamental flaw: Assumption that each stage can
and must be completed before the next one occurs
• Example: User may need to see finished product to
express true requirements!
Chapter 1: Introduction to Software Design 8
Waterfall Model
Chapter 1: Introduction to Software Design 9
Waterfall Model (2)
Chapter 1: Introduction to Software Design 10
Other Software Life Cycle Models
• Common theme among models: stages or cycles
• Unified Model:
• Cycles are called phases and iterations
• Activities are called workflows
• The four phases of the Unified Model:
• Inception
• Elaboration
• Construction
• Transition
Chapter 1: Introduction to Software Design 11
Other Software Life Cycle Models (2)
Chapter 1: Introduction to Software Design 12
Software Life Cycle Activities
Activities essential for successful development:
• Requirements specification
• Architectural, component, & detailed designs
• Implementation
• Unit, integration, and acceptance testing
• Installation and maintenance
Chapter 1: Introduction to Software Design 13
Software Life Cycle Activities Defined
Chapter 1: Introduction to Software Design 14
Software Life Cycle Activities (more)
• Requirements Specification
• System analyst works with users to clarify the detailed
system requirements
• Questions include format of input data, desired form
of any output screens, and data validation
• Analysis
• Make sure you completely understand the problem
before starting the design or program a solution
• Evaluate different approaches to the design
Chapter 1: Introduction to Software Design 15
Software Life Cycle Activities (continued)
• Design
• Top-down: break system into smaller subsystems
• Object-oriented: identify objects and their interactions
• UML diagrams: tool to show interactions between:
• Classes (inside the system)
• Classes and external entities
Chapter 1: Introduction to Software Design 16
Example of Top-Down: Stepwise Refinement
Chapter 1: Introduction to Software Design 17
Example of Object-Oriented: Class Diagram
Chapter 1: Introduction to Software Design 18
Using Abstraction to Manage Complexity
• An abstraction is a model of a physical entity or activity
• Models include relevant facts and details
• Models exclude matters irrelevant to system/task
• Abstraction helps programmers:
• Complex issues handled in manageable pieces
• Procedural abstraction: distinguishes ...
• What to achieve (by a procedure) ...
• From how to achieve it (implementation)
• Data abstraction: distinguishes ...
• Data objects for a problem and their operations ...
• From their representation in memory
Chapter 1: Introduction to Software Design 19
Using Abstraction to Manage Complexity (2)
• If another class uses an object only through its methods,
the other class will not be affected if the data
representation changes
• Information hiding: Concealing the details of a class
implementation from users of the class
• Enforces the discipline of data abstraction
Chapter 1: Introduction to Software Design 20
Abstract Data Types, Interfaces, and
Pre- and Post-conditions
• A major goal of software engineering: write reusable code
• Abstract data type (ADT): data + methods
• A Java interface is a way to specify an ADT
• Names, parameters, return types of methods
• No indication of how achieved (procedural abstraction)
• No representation (data abstraction)
• A class may implement an interface
• Must provide bodies for all methods of the interface
Chapter 1: Introduction to Software Design 21
Abstract Data Types, Interfaces, and
Pre- and Postconditions (2)
Chapter 1: Introduction to Software Design 22
Abstract Data Types, Interfaces, and Pre-
and Postconditions (continued)
• You cannot instantiate (new) an interface
• But you can:
• Declare a variable that has an interface type
• Use it to reference an actual object, whose class
implements the interface
• A Java interface is a contract between
• The interface designer and ...
• The coder of a class that implements the interface
• Precondition: any assumption/constraint on the method
data before the method begins execution
• Postcondition: describes result of executing the method
Chapter 1: Introduction to Software Design 23
Requirements Analysis:
Use Cases, and Sequence Diagrams
• Analysis first step: study input and output requirements:
• Make sure they are understood and make sense
• Use case:
• User actions and system responses for a sub-problem
• In the order that they are likely to occur
• Sequence diagram:
• Shows objects involved across the horizontal axis
• Shows time along the vertical axis
• See page 26 for an example; shows:
• User, PDApplication, PhoneDirectory, BufferedReader,
PDUserInterface object + a number of method calls
Chapter 1: Introduction to Software Design 24
Design of an Array-Based Phone Directory
• Case study shows:
• Design
• Implementation
• Testing of a software-based phone directory
• In UML class diagrams:
+ sign next to a method/attribute means it is public
- sign next to a method/attribute means it is private
Chapter 1: Introduction to Software Design 25
Design of Array-Based Phone Directory
Classes/interfaces to design include:
• PDUserInterface: interface; later we consider:
• Console (command line) UI class
• Graphical (JOptionPane) UI class
• PDApplication: main / driving class
• PhoneDirectory: interface
• ArrayBasedPD: class implementing PhoneDirectory
• DirectoryEntry: class, for one item in the directory
Chapter 1: Introduction to Software Design 26
Design of Array-Based Phone Directory (2)
Chapter 1: Introduction to Software Design 27
Design of Array-Based Phone Directory (3)
Chapter 1: Introduction to Software Design 28
Design of DirectoryEntry
• Simple class, similar to Person in Java review:
• Two private fields, for name and number
• Two-argument constructor
• Get methods for both fields
• Set method for number (only)
Chapter 1: Introduction to Software Design 29
Design of Array-Based Phone Directory (4)
Chapter 1: Introduction to Software Design 30
The PhoneDirectory Interface
/**
* The interface for the telephone directory.
* @author Koffman & Wolfgang
*/
public interface PhoneDirectory {
...
}
• Shows syntax of an interface
• Shows a javadoc comment and the @author tag
Chapter 1: Introduction to Software Design 31
PhoneDirectory.loadData
/** Load the data file containing the
* directory, or establish a connection with
* the data source.
* @param sourceName The name of the file
* (data source) with the phone directory
* entries
*/
void loadData (String sourceName);
• Shows syntax of method in an interface (note ;)
• Shows a javadoc comment with the @param tag
• Since returns void, no @return tag
Chapter 1: Introduction to Software Design 32
PhoneDirectory.lookupEntry
/** Look up an entry.
* @param name The name of the person
* to look up
* @return The number, or null if name
* is not in the directory
*/
String lookupEntry (String name);
• Shows a javadoc comment with the @return tag
• I prefer a space before the ( in a declaration (not a call)
Chapter 1: Introduction to Software Design 33
PhoneDirectory.addOrChangeEntry
/** Add an entry or change an existing entry.
* @param name The name of the person being
* added or changed
* @param number The new number to be assigned
* @return The old number or, if a new entry,
* null
*/
String addOrChangeEntry (String name,
String number);
• Shows a javadoc comment with two @param tags
Chapter 1: Introduction to Software Design 34
PhoneDirectory.removeEntry
/** Remove an entry from the directory.
* @param name The name of the person to be
* removed
* @return The current number. If not in
* directory, return null
*/
String removeEntry (String name);
Chapter 1: Introduction to Software Design 35
PhoneDirectory.save
/** Method to save the directory.
* pre: The directory is loaded with data.
* post: Contents of directory written back to
* the file in the form of name-number pairs
* on adjacent lines;
* modified is reset to false.
*/
void save ();
• Illustrates pre/post conditions
Chapter 1: Introduction to Software Design 36
Design of Array-Based Phone Directory (5)
Chapter 1: Introduction to Software Design 37
Design of ArrayBasedPD.loadData
Input: a file name; Effect: read initial directory from the file
1. Create a BufferedReader for the input
2. Read the first name
3. while the name is not null
4. Read the number
5. Add a new entry using method add
6. Read the next name
Chapter 1: Introduction to Software Design 38
Design of
ArrayBasedPD.addOrChangeEntry
Input: name and number; Effect: change number of existing
entry, or make new entry if there was none
1. Call method find to see if the name is in the directory
2. if the name is in the directory
3. change number with DirectoryEntry.setNumber
4. Return the previous value of the number
else
5. Add a new entry using method add
6. Return null
Chapter 1: Introduction to Software Design 39
Design of Array-Based Phone Directory (6)
• Remaining method designs proceed along the same
lines
• The class diagram changes, showing private fields and
methods added ....
Chapter 1: Introduction to Software Design 40
Design of Array-Based Phone Directory (7)
Chapter 1: Introduction to Software Design 41
Implementing and Testing the Array-Based
Phone Directory: ArrayBasedPD.java
import java.io.*;
/** This is an implementation of the
* PhoneDirectory interface that uses an
* array to store the data.
* @author Koffman & Wolfgang
*/
public class ArrayBasedPD
implements PhoneDirectory {
...
} // note: import, javadoc, implements
Chapter 1: Introduction to Software Design 42
ArrayBasedPD Data Fields (1)
// Data Fields (with javadoc comments)
/** The initial capacity of the array */
private static final int INITIAL_CAPACITY = 100;
/** The current capacity of the array */
private int capacity = INITIAL_CAPACITY;
/** The current size of the array (number of
directory entries) */
private int size = 0;
Chapter 1: Introduction to Software Design 43
ArrayBasedPD Data Fields (2)
/** The array to contain the directory data */
private DirectoryEntry[] theDirectory =
new DirectoryEntry[capacity];
/** The name of the data file that contains the
directory data */
private String sourceName = null;
/** Boolean flag indicates if the directory was
modified since it was loaded or saved. */
private boolean modified = false;
Chapter 1: Introduction to Software Design 44
ArrayBasedPD.loadData
public void loadData (String sourceName) {
// Remember the source name.
this.sourceName = sourceName;
try { ...
} catch (FileNotFoundException ex) {
// Do nothing — no data to load.
return;
} catch (IOException ex) {
System.err.println(“Directory load failed.");
ex.printStackTrace();
System.exit(1);
}
}
Chapter 1: Introduction to Software Design 45
ArrayBasedPD.loadData (2): Inside try
BufferedReader in = new BufferedReader(
new FileReader(sourceName));
while (true) {
String name, number;
// read name and number from succeeding lines
if ((name = in.readLine()) == null) break;
if ((number = in.readLine()) == null) break;
// insert entry (if got both name and number)
add(name, number);
}
in.close(); // should always close input
• Slightly different loop approach from the text
• Same assign-in-if-condition “hack”
Chapter 1: Introduction to Software Design 46
ArrayBasedPD.loadData (3): alternate
boolean more = true;
while (more) {
more = false;
String name = in.readLine();
if (name != null) {
String number = in.readLine();
if (number != null) {
add(name, number);
more = true;
}
}
}
• Nested if statements not as pleasant (what if 7 inputs?)
• Control variables tend to be harder to understand/get right
Chapter 1: Introduction to Software Design 47
ArrayBasedPD.addOrChangeEntry
public String addOrChangeEntry (String name,
String number) {
String oldNumber = null;
int index = find(name);
if (index > -1) {
oldNumber = theDirectory[index].getNumber();
theDirectory[index].setNumber(number);
}
else {
add(name, number);
}
modified = true;
return oldNumber;
}
Chapter 1: Introduction to Software Design 48
ArrayBasedPD.save
public void save() {
if (!modified) return; // save not needed
try {
// Create PrintWriter for the file.
PrintWriter out = new PrintWriter(
new FileWriter(sourceName));
...
} catch (Exception ex) {
System.err.println(“Directory save failed");
ex.printStackTrace();
System.exit(1);
}
}
Chapter 1: Introduction to Software Design 49
ArrayBasedPD.save (2)
// Write each directory entry to the file.
for (int i = 0; i < size; i++) {
// Write the name.
out.println(theDirectory[i].getName());
// Write the number.
out.println(theDirectory[i].getNumber());
}
// Close the file.
out.close();
modified = false;
Chapter 1: Introduction to Software Design 50
Implementing and Testing the Array-Based
Phone Directory
Chapter 1: Introduction to Software Design 51
ArrayBasedPD.find
private int find (String name) {
for (int i = 0; i < size; i++) {
if (theDirectory[i].getName().equals(name)) {
return i;
}
}
return -1; // Name not found.
}
Chapter 1: Introduction to Software Design 52
ArrayBasedPD.add
private void add (String name, String number) {
if (size >= capacity) {
reallocate();
}
theDirectory[size++] =
new DirectoryEntry(name, number);
}
• Differs from text in use of ++
• Note that size means number of names stored,
• while capacity means the number the array can hold
Chapter 1: Introduction to Software Design 53
ArrayBasedPD.realloc
private void reallocate () {
capacity *= 2;
DirectoryEntry[] newDirectory =
new DirectoryEntry[capacity];
System.arraycopy(theDirectory, 0,
newDirectory, 0,
theDirectory.length);
theDirectory = newDirectory;
}
Arguments to arraycopy are:
• fromDir, fromIndex
• toDir, toIndex
• number of elements to copy
Chapter 1: Introduction to Software Design 54
Testing ArrayBasedPD
• Empty data file
• Data file with only one name-number pair
• Data file with odd number of lines
• Data file with more pairs than initial array size
• Retrieve names not in directory as well as ones that are
• After a change, verify the new information
• Check that after changes, the changes, plus all new
information, are in the newly written file
• Note: This code does not check for empty strings!
Chapter 1: Introduction to Software Design 55
Implementing PDUserInterface
• Text offers two classes that implement the UI interface:
• PDGUI: Uses JOptionPane for graphical UI
• PDConsoleUI: Uses console stream I/O
(System.in and System.out)
• Text gives good recipes here that you can use as
models
• We will not cover them in detail here

More Related Content

Similar to chapter 1 software design.pptx

Design Engineering and Design concepts
Design Engineering and Design conceptsDesign Engineering and Design concepts
Design Engineering and Design conceptsJigyasaAgrawal7
 
Software engineering Satish.pptx
Software engineering Satish.pptxSoftware engineering Satish.pptx
Software engineering Satish.pptxProvatMajhi
 
SE - Lecture 6 - Software Design n Construction.pptx
SE - Lecture 6 - Software Design n Construction.pptxSE - Lecture 6 - Software Design n Construction.pptx
SE - Lecture 6 - Software Design n Construction.pptxTangZhiSiang
 
SE 1a SDLC Session BCU.ppt
SE 1a SDLC Session BCU.pptSE 1a SDLC Session BCU.ppt
SE 1a SDLC Session BCU.pptMahiDivya
 
Software Eng S3 ( Software Design ).pptx
Software Eng S3 ( Software Design ).pptxSoftware Eng S3 ( Software Design ).pptx
Software Eng S3 ( Software Design ).pptxgauriVarshney8
 
Software Development Life Cycle (SDLC )
Software Development Life Cycle (SDLC )Software Development Life Cycle (SDLC )
Software Development Life Cycle (SDLC )eshtiyak
 
MWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVCMWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVCUlrich Krause
 
2nd MODULE Software Requirements _ SW ENGG 22CSE141.pdf
2nd MODULE  Software Requirements   _ SW ENGG  22CSE141.pdf2nd MODULE  Software Requirements   _ SW ENGG  22CSE141.pdf
2nd MODULE Software Requirements _ SW ENGG 22CSE141.pdfJayanthi Kannan MK
 
SE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPTSE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPTnikshaikh786
 
Software engineering.pptx
Software engineering.pptxSoftware engineering.pptx
Software engineering.pptxProvatMajhi
 
6.SE_Requirements Modeling.ppt
6.SE_Requirements Modeling.ppt6.SE_Requirements Modeling.ppt
6.SE_Requirements Modeling.pptHaiderAli252366
 
Chapter 4 software design
Chapter 4  software designChapter 4  software design
Chapter 4 software designCliftone Mullah
 
Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxVigneshkumar Ponnusamy
 
An Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesAn Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesUlrich Krause
 
Android ppt with example of budget manager
Android ppt with example of budget managerAndroid ppt with example of budget manager
Android ppt with example of budget managerNalini Mehta
 
Traditional Process Models
Traditional Process ModelsTraditional Process Models
Traditional Process ModelsAhsan Rahim
 

Similar to chapter 1 software design.pptx (20)

Design Engineering and Design concepts
Design Engineering and Design conceptsDesign Engineering and Design concepts
Design Engineering and Design concepts
 
SE Unit-1.pptx
SE Unit-1.pptxSE Unit-1.pptx
SE Unit-1.pptx
 
Lecture 3.pptx
Lecture 3.pptxLecture 3.pptx
Lecture 3.pptx
 
Software engineering Satish.pptx
Software engineering Satish.pptxSoftware engineering Satish.pptx
Software engineering Satish.pptx
 
SE - Lecture 6 - Software Design n Construction.pptx
SE - Lecture 6 - Software Design n Construction.pptxSE - Lecture 6 - Software Design n Construction.pptx
SE - Lecture 6 - Software Design n Construction.pptx
 
SE 1a SDLC Session BCU.ppt
SE 1a SDLC Session BCU.pptSE 1a SDLC Session BCU.ppt
SE 1a SDLC Session BCU.ppt
 
Software Eng S3 ( Software Design ).pptx
Software Eng S3 ( Software Design ).pptxSoftware Eng S3 ( Software Design ).pptx
Software Eng S3 ( Software Design ).pptx
 
Software Development Life Cycle (SDLC )
Software Development Life Cycle (SDLC )Software Development Life Cycle (SDLC )
Software Development Life Cycle (SDLC )
 
Requirements engineering
Requirements engineeringRequirements engineering
Requirements engineering
 
MWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVCMWLUG 2015 - An Introduction to MVC
MWLUG 2015 - An Introduction to MVC
 
2nd MODULE Software Requirements _ SW ENGG 22CSE141.pdf
2nd MODULE  Software Requirements   _ SW ENGG  22CSE141.pdf2nd MODULE  Software Requirements   _ SW ENGG  22CSE141.pdf
2nd MODULE Software Requirements _ SW ENGG 22CSE141.pdf
 
SE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPTSE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPT
 
Software engineering.pptx
Software engineering.pptxSoftware engineering.pptx
Software engineering.pptx
 
6.SE_Requirements Modeling.ppt
6.SE_Requirements Modeling.ppt6.SE_Requirements Modeling.ppt
6.SE_Requirements Modeling.ppt
 
Chapter 4 software design
Chapter 4  software designChapter 4  software design
Chapter 4 software design
 
Fundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptxFundamentals of Data Structures Unit 1.pptx
Fundamentals of Data Structures Unit 1.pptx
 
An Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPagesAn Introduction To Model  View  Controller In XPages
An Introduction To Model  View  Controller In XPages
 
Waterfall Model
Waterfall ModelWaterfall Model
Waterfall Model
 
Android ppt with example of budget manager
Android ppt with example of budget managerAndroid ppt with example of budget manager
Android ppt with example of budget manager
 
Traditional Process Models
Traditional Process ModelsTraditional Process Models
Traditional Process Models
 

More from recoveraccount1

market anlysis SOFTware.pptx
market anlysis SOFTware.pptxmarket anlysis SOFTware.pptx
market anlysis SOFTware.pptxrecoveraccount1
 
TYPES OF CUSTOMERS- software design.pptx
TYPES  OF  CUSTOMERS-  software design.pptxTYPES  OF  CUSTOMERS-  software design.pptx
TYPES OF CUSTOMERS- software design.pptxrecoveraccount1
 
intellectualproperty CpET Laws and Professional Practices.pptx
intellectualproperty CpET Laws and Professional Practices.pptxintellectualproperty CpET Laws and Professional Practices.pptx
intellectualproperty CpET Laws and Professional Practices.pptxrecoveraccount1
 
NAND AND NOR IMPLEMENTATION.pptx
NAND AND NOR IMPLEMENTATION.pptxNAND AND NOR IMPLEMENTATION.pptx
NAND AND NOR IMPLEMENTATION.pptxrecoveraccount1
 
REPRESENTATION TO FLIP-FLOPS CHARACTERISTICS TABLE AND FLIP-FLOPS EXCITATION ...
REPRESENTATION TO FLIP-FLOPS CHARACTERISTICS TABLE AND FLIP-FLOPS EXCITATION ...REPRESENTATION TO FLIP-FLOPS CHARACTERISTICS TABLE AND FLIP-FLOPS EXCITATION ...
REPRESENTATION TO FLIP-FLOPS CHARACTERISTICS TABLE AND FLIP-FLOPS EXCITATION ...recoveraccount1
 
ROMANDPLAPRESENTATION.pptx
ROMANDPLAPRESENTATION.pptxROMANDPLAPRESENTATION.pptx
ROMANDPLAPRESENTATION.pptxrecoveraccount1
 
e-commercelawinthephilippines-140914041005-phpapp01e-commercelawinthephilippi...
e-commercelawinthephilippines-140914041005-phpapp01e-commercelawinthephilippi...e-commercelawinthephilippines-140914041005-phpapp01e-commercelawinthephilippi...
e-commercelawinthephilippines-140914041005-phpapp01e-commercelawinthephilippi...recoveraccount1
 
emtech chapter 3emtech chapter 3.pptx
emtech chapter 3emtech chapter 3.pptxemtech chapter 3emtech chapter 3.pptx
emtech chapter 3emtech chapter 3.pptxrecoveraccount1
 

More from recoveraccount1 (12)

15286249.ppt
15286249.ppt15286249.ppt
15286249.ppt
 
market anlysis SOFTware.pptx
market anlysis SOFTware.pptxmarket anlysis SOFTware.pptx
market anlysis SOFTware.pptx
 
chapter 2.pdf
chapter 2.pdfchapter 2.pdf
chapter 2.pdf
 
TYPES OF CUSTOMERS- software design.pptx
TYPES  OF  CUSTOMERS-  software design.pptxTYPES  OF  CUSTOMERS-  software design.pptx
TYPES OF CUSTOMERS- software design.pptx
 
intellectualproperty CpET Laws and Professional Practices.pptx
intellectualproperty CpET Laws and Professional Practices.pptxintellectualproperty CpET Laws and Professional Practices.pptx
intellectualproperty CpET Laws and Professional Practices.pptx
 
NAND AND NOR IMPLEMENTATION.pptx
NAND AND NOR IMPLEMENTATION.pptxNAND AND NOR IMPLEMENTATION.pptx
NAND AND NOR IMPLEMENTATION.pptx
 
REPRESENTATION TO FLIP-FLOPS CHARACTERISTICS TABLE AND FLIP-FLOPS EXCITATION ...
REPRESENTATION TO FLIP-FLOPS CHARACTERISTICS TABLE AND FLIP-FLOPS EXCITATION ...REPRESENTATION TO FLIP-FLOPS CHARACTERISTICS TABLE AND FLIP-FLOPS EXCITATION ...
REPRESENTATION TO FLIP-FLOPS CHARACTERISTICS TABLE AND FLIP-FLOPS EXCITATION ...
 
ROMANDPLAPRESENTATION.pptx
ROMANDPLAPRESENTATION.pptxROMANDPLAPRESENTATION.pptx
ROMANDPLAPRESENTATION.pptx
 
e-commercelawinthephilippines-140914041005-phpapp01e-commercelawinthephilippi...
e-commercelawinthephilippines-140914041005-phpapp01e-commercelawinthephilippi...e-commercelawinthephilippines-140914041005-phpapp01e-commercelawinthephilippi...
e-commercelawinthephilippines-140914041005-phpapp01e-commercelawinthephilippi...
 
emtech chapter 3emtech chapter 3.pptx
emtech chapter 3emtech chapter 3.pptxemtech chapter 3emtech chapter 3.pptx
emtech chapter 3emtech chapter 3.pptx
 
emtech blockchain.pptx
emtech blockchain.pptxemtech blockchain.pptx
emtech blockchain.pptx
 
emtech chap 2.pptx
emtech chap 2.pptxemtech chap 2.pptx
emtech chap 2.pptx
 

Recently uploaded

HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 

Recently uploaded (20)

HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 

chapter 1 software design.pptx

  • 2. Chapter 1: Introduction to Software Design 2 Outline • The software challenge and the software life cycle • Activities of each phase of the software life cycle • Using top-down design and object-oriented design • Managing complexity: • Data abstraction • Procedural abstraction • Information hiding • Class diagrams document interactions between classes
  • 3. Chapter 1: Introduction to Software Design 3 Outline (2) • Abstract data types: • Role in modeling • Implementing them with classes and interfaces • Use cases: tool to document interaction with a user • Software design process example: • Design and implementation of an array-based telephone directory • Sequence diagrams: tool for documenting the interaction between multiple classes used in a program
  • 4. Chapter 1: Introduction to Software Design 4 The Software Challenge • Software is ... • Used for a long time • Updated and maintained • By people who did not write it • Initial specification may be incomplete • Specification clarified through extensive interaction between user(s) and system analyst(s) • Requirements specification needed at the beginning of any software project • Designers and users should both approve it!
  • 5. Chapter 1: Introduction to Software Design 5 Things Change! • Users’ needs and expectations change • Use reveals limitations and flaws • Desire for increased convenience, functionality • Desire for increased performance • Environment changes • Hardware, OS, software packages (“software rot”) • Need to interact with clients, parent org., etc. • Law and regulations change • Ways of doing business • Style, “cool” factor
  • 6. Chapter 1: Introduction to Software Design 6 The Software Life Cycle • Software goes through stages as it moves from initial concept to finished product • The sequence of stages is called a life cycle • Must design and document software: • In an organized way for: • Understanding and ... • Maintenance (change) after the initial release • The maintainer is not necessarily the author! • ... and even authors forget • ... and no one can keep all details in mind at once
  • 7. Chapter 1: Introduction to Software Design 7 Software Life Cycle Models: The Waterfall Model • Simplest way to organizing activities in stages • Activities are: • Performed in sequence • Result of one flows (falls) into the next • The Waterfall Model is simple ... but unworkable • Fundamental flaw: Assumption that each stage can and must be completed before the next one occurs • Example: User may need to see finished product to express true requirements!
  • 8. Chapter 1: Introduction to Software Design 8 Waterfall Model
  • 9. Chapter 1: Introduction to Software Design 9 Waterfall Model (2)
  • 10. Chapter 1: Introduction to Software Design 10 Other Software Life Cycle Models • Common theme among models: stages or cycles • Unified Model: • Cycles are called phases and iterations • Activities are called workflows • The four phases of the Unified Model: • Inception • Elaboration • Construction • Transition
  • 11. Chapter 1: Introduction to Software Design 11 Other Software Life Cycle Models (2)
  • 12. Chapter 1: Introduction to Software Design 12 Software Life Cycle Activities Activities essential for successful development: • Requirements specification • Architectural, component, & detailed designs • Implementation • Unit, integration, and acceptance testing • Installation and maintenance
  • 13. Chapter 1: Introduction to Software Design 13 Software Life Cycle Activities Defined
  • 14. Chapter 1: Introduction to Software Design 14 Software Life Cycle Activities (more) • Requirements Specification • System analyst works with users to clarify the detailed system requirements • Questions include format of input data, desired form of any output screens, and data validation • Analysis • Make sure you completely understand the problem before starting the design or program a solution • Evaluate different approaches to the design
  • 15. Chapter 1: Introduction to Software Design 15 Software Life Cycle Activities (continued) • Design • Top-down: break system into smaller subsystems • Object-oriented: identify objects and their interactions • UML diagrams: tool to show interactions between: • Classes (inside the system) • Classes and external entities
  • 16. Chapter 1: Introduction to Software Design 16 Example of Top-Down: Stepwise Refinement
  • 17. Chapter 1: Introduction to Software Design 17 Example of Object-Oriented: Class Diagram
  • 18. Chapter 1: Introduction to Software Design 18 Using Abstraction to Manage Complexity • An abstraction is a model of a physical entity or activity • Models include relevant facts and details • Models exclude matters irrelevant to system/task • Abstraction helps programmers: • Complex issues handled in manageable pieces • Procedural abstraction: distinguishes ... • What to achieve (by a procedure) ... • From how to achieve it (implementation) • Data abstraction: distinguishes ... • Data objects for a problem and their operations ... • From their representation in memory
  • 19. Chapter 1: Introduction to Software Design 19 Using Abstraction to Manage Complexity (2) • If another class uses an object only through its methods, the other class will not be affected if the data representation changes • Information hiding: Concealing the details of a class implementation from users of the class • Enforces the discipline of data abstraction
  • 20. Chapter 1: Introduction to Software Design 20 Abstract Data Types, Interfaces, and Pre- and Post-conditions • A major goal of software engineering: write reusable code • Abstract data type (ADT): data + methods • A Java interface is a way to specify an ADT • Names, parameters, return types of methods • No indication of how achieved (procedural abstraction) • No representation (data abstraction) • A class may implement an interface • Must provide bodies for all methods of the interface
  • 21. Chapter 1: Introduction to Software Design 21 Abstract Data Types, Interfaces, and Pre- and Postconditions (2)
  • 22. Chapter 1: Introduction to Software Design 22 Abstract Data Types, Interfaces, and Pre- and Postconditions (continued) • You cannot instantiate (new) an interface • But you can: • Declare a variable that has an interface type • Use it to reference an actual object, whose class implements the interface • A Java interface is a contract between • The interface designer and ... • The coder of a class that implements the interface • Precondition: any assumption/constraint on the method data before the method begins execution • Postcondition: describes result of executing the method
  • 23. Chapter 1: Introduction to Software Design 23 Requirements Analysis: Use Cases, and Sequence Diagrams • Analysis first step: study input and output requirements: • Make sure they are understood and make sense • Use case: • User actions and system responses for a sub-problem • In the order that they are likely to occur • Sequence diagram: • Shows objects involved across the horizontal axis • Shows time along the vertical axis • See page 26 for an example; shows: • User, PDApplication, PhoneDirectory, BufferedReader, PDUserInterface object + a number of method calls
  • 24. Chapter 1: Introduction to Software Design 24 Design of an Array-Based Phone Directory • Case study shows: • Design • Implementation • Testing of a software-based phone directory • In UML class diagrams: + sign next to a method/attribute means it is public - sign next to a method/attribute means it is private
  • 25. Chapter 1: Introduction to Software Design 25 Design of Array-Based Phone Directory Classes/interfaces to design include: • PDUserInterface: interface; later we consider: • Console (command line) UI class • Graphical (JOptionPane) UI class • PDApplication: main / driving class • PhoneDirectory: interface • ArrayBasedPD: class implementing PhoneDirectory • DirectoryEntry: class, for one item in the directory
  • 26. Chapter 1: Introduction to Software Design 26 Design of Array-Based Phone Directory (2)
  • 27. Chapter 1: Introduction to Software Design 27 Design of Array-Based Phone Directory (3)
  • 28. Chapter 1: Introduction to Software Design 28 Design of DirectoryEntry • Simple class, similar to Person in Java review: • Two private fields, for name and number • Two-argument constructor • Get methods for both fields • Set method for number (only)
  • 29. Chapter 1: Introduction to Software Design 29 Design of Array-Based Phone Directory (4)
  • 30. Chapter 1: Introduction to Software Design 30 The PhoneDirectory Interface /** * The interface for the telephone directory. * @author Koffman & Wolfgang */ public interface PhoneDirectory { ... } • Shows syntax of an interface • Shows a javadoc comment and the @author tag
  • 31. Chapter 1: Introduction to Software Design 31 PhoneDirectory.loadData /** Load the data file containing the * directory, or establish a connection with * the data source. * @param sourceName The name of the file * (data source) with the phone directory * entries */ void loadData (String sourceName); • Shows syntax of method in an interface (note ;) • Shows a javadoc comment with the @param tag • Since returns void, no @return tag
  • 32. Chapter 1: Introduction to Software Design 32 PhoneDirectory.lookupEntry /** Look up an entry. * @param name The name of the person * to look up * @return The number, or null if name * is not in the directory */ String lookupEntry (String name); • Shows a javadoc comment with the @return tag • I prefer a space before the ( in a declaration (not a call)
  • 33. Chapter 1: Introduction to Software Design 33 PhoneDirectory.addOrChangeEntry /** Add an entry or change an existing entry. * @param name The name of the person being * added or changed * @param number The new number to be assigned * @return The old number or, if a new entry, * null */ String addOrChangeEntry (String name, String number); • Shows a javadoc comment with two @param tags
  • 34. Chapter 1: Introduction to Software Design 34 PhoneDirectory.removeEntry /** Remove an entry from the directory. * @param name The name of the person to be * removed * @return The current number. If not in * directory, return null */ String removeEntry (String name);
  • 35. Chapter 1: Introduction to Software Design 35 PhoneDirectory.save /** Method to save the directory. * pre: The directory is loaded with data. * post: Contents of directory written back to * the file in the form of name-number pairs * on adjacent lines; * modified is reset to false. */ void save (); • Illustrates pre/post conditions
  • 36. Chapter 1: Introduction to Software Design 36 Design of Array-Based Phone Directory (5)
  • 37. Chapter 1: Introduction to Software Design 37 Design of ArrayBasedPD.loadData Input: a file name; Effect: read initial directory from the file 1. Create a BufferedReader for the input 2. Read the first name 3. while the name is not null 4. Read the number 5. Add a new entry using method add 6. Read the next name
  • 38. Chapter 1: Introduction to Software Design 38 Design of ArrayBasedPD.addOrChangeEntry Input: name and number; Effect: change number of existing entry, or make new entry if there was none 1. Call method find to see if the name is in the directory 2. if the name is in the directory 3. change number with DirectoryEntry.setNumber 4. Return the previous value of the number else 5. Add a new entry using method add 6. Return null
  • 39. Chapter 1: Introduction to Software Design 39 Design of Array-Based Phone Directory (6) • Remaining method designs proceed along the same lines • The class diagram changes, showing private fields and methods added ....
  • 40. Chapter 1: Introduction to Software Design 40 Design of Array-Based Phone Directory (7)
  • 41. Chapter 1: Introduction to Software Design 41 Implementing and Testing the Array-Based Phone Directory: ArrayBasedPD.java import java.io.*; /** This is an implementation of the * PhoneDirectory interface that uses an * array to store the data. * @author Koffman & Wolfgang */ public class ArrayBasedPD implements PhoneDirectory { ... } // note: import, javadoc, implements
  • 42. Chapter 1: Introduction to Software Design 42 ArrayBasedPD Data Fields (1) // Data Fields (with javadoc comments) /** The initial capacity of the array */ private static final int INITIAL_CAPACITY = 100; /** The current capacity of the array */ private int capacity = INITIAL_CAPACITY; /** The current size of the array (number of directory entries) */ private int size = 0;
  • 43. Chapter 1: Introduction to Software Design 43 ArrayBasedPD Data Fields (2) /** The array to contain the directory data */ private DirectoryEntry[] theDirectory = new DirectoryEntry[capacity]; /** The name of the data file that contains the directory data */ private String sourceName = null; /** Boolean flag indicates if the directory was modified since it was loaded or saved. */ private boolean modified = false;
  • 44. Chapter 1: Introduction to Software Design 44 ArrayBasedPD.loadData public void loadData (String sourceName) { // Remember the source name. this.sourceName = sourceName; try { ... } catch (FileNotFoundException ex) { // Do nothing — no data to load. return; } catch (IOException ex) { System.err.println(“Directory load failed."); ex.printStackTrace(); System.exit(1); } }
  • 45. Chapter 1: Introduction to Software Design 45 ArrayBasedPD.loadData (2): Inside try BufferedReader in = new BufferedReader( new FileReader(sourceName)); while (true) { String name, number; // read name and number from succeeding lines if ((name = in.readLine()) == null) break; if ((number = in.readLine()) == null) break; // insert entry (if got both name and number) add(name, number); } in.close(); // should always close input • Slightly different loop approach from the text • Same assign-in-if-condition “hack”
  • 46. Chapter 1: Introduction to Software Design 46 ArrayBasedPD.loadData (3): alternate boolean more = true; while (more) { more = false; String name = in.readLine(); if (name != null) { String number = in.readLine(); if (number != null) { add(name, number); more = true; } } } • Nested if statements not as pleasant (what if 7 inputs?) • Control variables tend to be harder to understand/get right
  • 47. Chapter 1: Introduction to Software Design 47 ArrayBasedPD.addOrChangeEntry public String addOrChangeEntry (String name, String number) { String oldNumber = null; int index = find(name); if (index > -1) { oldNumber = theDirectory[index].getNumber(); theDirectory[index].setNumber(number); } else { add(name, number); } modified = true; return oldNumber; }
  • 48. Chapter 1: Introduction to Software Design 48 ArrayBasedPD.save public void save() { if (!modified) return; // save not needed try { // Create PrintWriter for the file. PrintWriter out = new PrintWriter( new FileWriter(sourceName)); ... } catch (Exception ex) { System.err.println(“Directory save failed"); ex.printStackTrace(); System.exit(1); } }
  • 49. Chapter 1: Introduction to Software Design 49 ArrayBasedPD.save (2) // Write each directory entry to the file. for (int i = 0; i < size; i++) { // Write the name. out.println(theDirectory[i].getName()); // Write the number. out.println(theDirectory[i].getNumber()); } // Close the file. out.close(); modified = false;
  • 50. Chapter 1: Introduction to Software Design 50 Implementing and Testing the Array-Based Phone Directory
  • 51. Chapter 1: Introduction to Software Design 51 ArrayBasedPD.find private int find (String name) { for (int i = 0; i < size; i++) { if (theDirectory[i].getName().equals(name)) { return i; } } return -1; // Name not found. }
  • 52. Chapter 1: Introduction to Software Design 52 ArrayBasedPD.add private void add (String name, String number) { if (size >= capacity) { reallocate(); } theDirectory[size++] = new DirectoryEntry(name, number); } • Differs from text in use of ++ • Note that size means number of names stored, • while capacity means the number the array can hold
  • 53. Chapter 1: Introduction to Software Design 53 ArrayBasedPD.realloc private void reallocate () { capacity *= 2; DirectoryEntry[] newDirectory = new DirectoryEntry[capacity]; System.arraycopy(theDirectory, 0, newDirectory, 0, theDirectory.length); theDirectory = newDirectory; } Arguments to arraycopy are: • fromDir, fromIndex • toDir, toIndex • number of elements to copy
  • 54. Chapter 1: Introduction to Software Design 54 Testing ArrayBasedPD • Empty data file • Data file with only one name-number pair • Data file with odd number of lines • Data file with more pairs than initial array size • Retrieve names not in directory as well as ones that are • After a change, verify the new information • Check that after changes, the changes, plus all new information, are in the newly written file • Note: This code does not check for empty strings!
  • 55. Chapter 1: Introduction to Software Design 55 Implementing PDUserInterface • Text offers two classes that implement the UI interface: • PDGUI: Uses JOptionPane for graphical UI • PDConsoleUI: Uses console stream I/O (System.in and System.out) • Text gives good recipes here that you can use as models • We will not cover them in detail here