Unified Modeling Language a case study

Similar documents
Inheritance and Polymorphism

Lecture 7. Log into Linux New documents posted to course webpage

Intermediate Programming, Spring 2017*

Software Design Abstract Data Types

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

CSCE 110 PROGRAMMING FUNDAMENTALS

Templates and Vectors

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Lab 1: First Steps in C++ - Eclipse

Linked List using a Sentinel

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Queue Implementations

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CS24 Week 3 Lecture 1

CPSC 427: Object-Oriented Programming

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

EL2310 Scientific Programming

EL2310 Scientific Programming

Exam duration: 3 hours Number of pages: 10

Hashing. mapping data to tables hashing integers and strings. aclasshash_table inserting and locating strings. inserting and locating strings

EECE.3220: Data Structures Spring 2017

Unit Testing. Contents. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...

C++ Constructor Insanity

การทดลองท 8_2 Editor Buffer Array Implementation

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

Unit Testing. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...

CPSC 427: Object-Oriented Programming

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

Hashing. inserting and locating strings. MCS 360 Lecture 28 Introduction to Data Structures Jan Verschelde, 27 October 2010.

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Simulations. buffer of fixed capacity using the STL deque. modeling arrival times generating and processing jobs

Quad Doubles on a GPU

Program template-smart-pointers-again.cc

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

Expression Trees and the Heap

ECE 462 Exam 1. 6:30-7:30PM, September 22, 2010

Common Misunderstandings from Exam 1 Material

PHY4321 Summary Notes

Homework 4. Any questions?

Documentation. Programming / Documentation Slide 42

EL2310 Scientific Programming

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Implementing an ADT with a Class

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

PROGRAMMING IN C++ CVIČENÍ

Lab 2: ADT Design & Implementation

An Implementation Demo. Project 08: Binary Tree I/O

Class Destructors constant member functions

Programmazione. Prof. Marco Bertini

Computer Programming

Lecture 5 Files and Streams

Classes and Objects in C++

Due Date: See Blackboard

Strings and Stream I/O

CS93SI Handout 04 Spring 2006 Apr Review Answers

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

Abstract Data Types (ADT) and C++ Classes

CSE 333 Lecture smart pointers

CS 103 Unit 10 Slides

Due Date: See Blackboard

Exercise 1.1 Hello world

Namespaces and Class Hierarchies

2.1 Introduction UML Preliminaries Class diagrams Modelling delegation... 4

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

C++ Namespaces, Exceptions

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Lecture-5. STL Containers & Iterators

C++ Lab 03 - C++ Functions

C++ Lab 05 - C Structures and C++ Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Program template-smart-pointers.cc

Chapter 1: Object-Oriented Programming Using C++

CS 103 Unit 10 Slides

Object-Oriented Programming, Iouliia Skliarova

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

EL2310 Scientific Programming

The following program computes a Calculus value, the "trapezoidal approximation of

aggregate data types member functions cis15 advanced programming techniques, using c++ lecture # II.1

Graphs. directed and undirected graphs weighted graphs adjacency matrices. abstract data type adjacency list adjacency matrix

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

Due Date: See Blackboard

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Communication With the Outside World

Polymorphism Part 1 1

lecture04: Constructors and Destructors

PENN STATE UNIVERSITY Department of Economics

C++ Crash Kurs. Exceptions. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

Programming in C++: Assignment Week 8

CE221 Programming in C++ Part 1 Introduction

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

Object Oriented Design

Chapter 2. Procedural Programming

Inheritance, and Polymorphism.

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Introduction. Lecture 5 Files and Streams FILE * FILE *

Due Date: See Blackboard

Transcription:

Unified Modeling Language a case study 1 an online phone book use case diagram encapsulating a file 2 Command Line Arguments arguments of main arrays of strings 3 Class Definition the filesphonebook.h andphonebook.cpp the main program MCS 360 Lecture 5 Introduction to Data Structures Jan Verschelde, 8 September 2017 Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 1 / 26

Unified Modeling Language 1 an online phone book use case diagram encapsulating a file 2 Command Line Arguments arguments of main arrays of strings 3 Class Definition the filesphonebook.h andphonebook.cpp the main program Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 2 / 26

An Online Phone Book As case study we consider the management and the consultation of an online phone book. Two types of use: 1 manager: add and delete entries; 2 reader: lookup phone numbers. Two types of diagrams in UML: 1 class diagram: defines data and methods; 2 use case diagram: who uses what methods. Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 3 / 26

the classphonebook We use an array of strings to represent the entries in a phone book. A class diagram: -: private +: public PhoneBook - string* - number + PhoneBook() + PhoneBook() + add() + delete() + lookup() Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 4 / 26

Use Case Diagram for PhoneBook a behavior modeling diagram Managers and readers differ in their use of the PhoneBook: manager PhoneBook() PhoneBook() add() delete() lookup() PhoneBook reader Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 5 / 26

Unified Modeling Language 1 an online phone book use case diagram encapsulating a file 2 Command Line Arguments arguments of main arrays of strings 3 Class Definition the filesphonebook.h andphonebook.cpp the main program Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 6 / 26

Encapsulating a File What does the creatorphonebook() do? 1 read number of entries from file; 2 allocate memory; 3 read data from file into array. The destructor PhoneBook() deallocates the memory. Principle of information hiding: 1 actual file and its format hidden from the user; 2 programmer considers array of strings. Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 7 / 26

Command Line Arguments Main program is used in two different modes: 1 administrative mode by manager; 2 reader only consults the phone book. One possible way of implementation: -a is command line argument of phonebook program. Run in administrative mode: $ phonebook -a Without option, program runs in reader mode. Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 8 / 26

Unified Modeling Language 1 an online phone book use case diagram encapsulating a file 2 Command Line Arguments arguments of main arrays of strings 3 Class Definition the filesphonebook.h andphonebook.cpp the main program Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 9 / 26

Arguments ofmain int main ( int argc, char *argv[] ) Two optional arguments: 1 argc: the number of arguments, argc 1 If there are no command line arguments, then argc = 1. 2 argv: the arguments of the command line argv is an array of strings, argv[0] is the name of the program argv[i] is the (i-1)-th argument of the program. Example at the command prompt$: $ command_line_args -a somefile.txt Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 10 / 26

Unified Modeling Language 1 an online phone book use case diagram encapsulating a file 2 Command Line Arguments arguments of main arrays of strings 3 Class Definition the filesphonebook.h andphonebook.cpp the main program Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 11 / 26

arrays of strings #include <iostream> #include <string> using namespace std; int main ( int argc, char *argv[] ) { cout << "name of the program : \"" << argv[0] << "\"" << endl; cout << "number of command line arguments : " << argc-1 << endl; if(argc > 1) for(int i=1; i<argc; i++) cout << "argument " << i << " : \"" << argv[i] << "\"" << endl; } return 0; Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 12 / 26

Unified Modeling Language 1 an online phone book use case diagram encapsulating a file 2 Command Line Arguments arguments of main arrays of strings 3 Class Definition the filesphonebook.h andphonebook.cpp the main program Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 13 / 26

Files The data is stored on file, inphonebook_data.txt. Example: 2 111-222-4444 Elliot Koffman 333-666-9999 Paul Wolfgang Number of entries comes first, one entry per line. On each line, a phone number is in the first 11 characters followed by a name. Other files: phonebook.h: public and private attributes; phonebook.cpp: defines the class methods; use_phonebook.cpp: the main program. Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 14 / 26

the filephonebook.h #ifndef PHONEBOOK_H #define PHONEBOOK_H #include <string> class PhoneBook { public: PhoneBook(); ~PhoneBook(); int length() const; std::string operator[](size_t k) const; void add(const std::string s); private: int number; // number of entries std::string *data; // array of strings }; #endif Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 15 / 26

specifications of methods Add as documentation inphonebook.h: PhoneBook(); /* Reads phone book entries from file. Precondition: file phonebook_data has valid entries. Postcondition: for PhoneBook b, there are b.length() entries b[k], with 0 <= k < b.length(). */ ~PhoneBook(); /* Deallocates memory occupied by entries. Postcondition: b.number == 0 after b.~phonebook(). */ Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 16 / 26

specifications continued int length() const; /* Returns the number of entries in phone book. Precondition: constructor PhoneBook() executed correctly. Postcondition: length() >= 0. */ std::string operator[](size_t k) const; /* Returns element at index k in phone book. Precondition: k < b.length() for PhoneBook b. Postcondition: b[k] is k-th entry in phone book, matching appropriate line on file. */ Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 17 / 26

specifications ofadd void add(const std::string s); /* Adds a new entry defined by the data in s. Precondition: s matches the data format for file, contains phone number and name. Postcondition: after PhoneBook b; b[b.length()-1] == s. */ In a more elaborate design, a separate class would define the layout of the strings on file. Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 18 / 26

constructor and destructor #include <limits> #include <fstream> #include "phonebook.h" PhoneBook::PhoneBook() { std::ifstream ins("phonebook_data.txt"); ins >> number; data = new std::string[number]; ins.ignore(std::numeric_limits<int>::max(), \n ); for(int k=0; k<number; k++) getline(ins,data[k], \n ); ins.close(); } PhoneBook::~PhoneBook() { delete[] data; number = 0; } Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 19 / 26

selectors and modifier int PhoneBook::length() const { return number; } std::string PhoneBook::operator[](size_t k) const { return data[k]; } void PhoneBook::add(const std::string s) { std::ofstream outs("phonebook_data.txt"); number = number + 1; outs << number << std::endl; for(int k=0; k<number-1; k++) outs << data[k] << std::endl; outs << s << std::endl; outs.close(); } Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 20 / 26

Unified Modeling Language 1 an online phone book use case diagram encapsulating a file 2 Command Line Arguments arguments of main arrays of strings 3 Class Definition the filesphonebook.h andphonebook.cpp the main program Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 21 / 26

the main program #include <iostream> #include "phonebook.h" using namespace std; int main ( int argc, char* argv[] ) { PhoneBook b; int n = b.length(); cout << "number of entries : " << n << endl; if(argc == 1) for(int k=0; k<n; k++) cout << " entry " << k << " : " << b[k] << endl; Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 22 / 26

running as manager else { string new_entry; cout << "give new entry : "; getline(cin,new_entry); } b.add(new_entry); } return 0; Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 23 / 26

Dynamic Allocation and Deallocation int main ( int argc, char* argv[] ) { PhoneBook *b; // statements welcoming user b = new PhoneBook; // allocation // statements using b b->~phonebook(); // deallocation ForPhoneBook *b, need to replace b.length() byb->length() b[k] by(*b)[k] b.add(new_entry) byb->add(new_entry) Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 24 / 26

the makefile defines themake use_phonebook 1 which c++ compiler to use, 2 the compilation of the definition of the class, 3 the compilation of the main program, 4 the linking of the files with the object code. gpp=/usr/bin/g++ use_phonebook: $(gpp) -c phonebook.cpp $(gpp) -c use_phonebook.cpp $(gpp) -o /tmp/use_phonebook \ phonebook.o use_phonebook.o clean: /bin/rm -f *.o Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 25 / 26

Summary + Exercises We ended Chapter 1: Introduction to Software Design. Exercises: 1 Modifycopy_files.cpp of lecture 3 so that the names for the two files are given on the command line. 2 Add the command line option-v to thehello_world.cpp program of the first lecture. Without-v, the program executes as before. With-v, the program prints the name of the author and the version number, e.g.: Release 1.0. 3 Provide another constructor to the classphonebook with allows the name of the file as input parameter. 4 Add a methoddelete(size_t k) to remove an entry from file, given the indexkin the array. 5 Develop asearch(const string name) method to search a phone number given a name. Introduction to Data Structures (MCS 360) Unified Modeling Language L-5 8 September 2017 26 / 26