Скачать презентацию Chapter 1 Software Engineering Principles and Java Classes Скачать презентацию Chapter 1 Software Engineering Principles and Java Classes

231e97e3678075167628ec9acb4bb686.ppt

  • Количество слайдов: 49

Chapter 1 Software Engineering Principles and Java Classes Data Structures Using Java 1 Chapter 1 Software Engineering Principles and Java Classes Data Structures Using Java 1

Chapter Objectives • Learn about software engineering principles • Discover what an algorithm is Chapter Objectives • Learn about software engineering principles • Discover what an algorithm is and explore problem-solving techniques • Become aware of structured design and objectoriented design programming methodologies • Learn about user-defined classes • Learn about private, protected, and public members of a class Data Structures Using Java 2

Chapter Objectives • Explore how classes are implemented • Become aware of Unified Modeling Chapter Objectives • Explore how classes are implemented • Become aware of Unified Modeling Language (UML) notation • Examine constructors and destructors • Learn about the abstract data type (ADT) • Explore how classes are used to implement ADT Data Structures Using Java 3

Software Life Cycle • Life cycle: the many phases a program goes through from Software Life Cycle • Life cycle: the many phases a program goes through from the time it is conceived until the time it is retired • Three fundamental stages of a program – Development – Use – Maintenance Data Structures Using Java 4

Software Development Phase • Analysis – Understand problem – Requirements analysis – Data analysis Software Development Phase • Analysis – Understand problem – Requirements analysis – Data analysis – Divide problem into subproblems and perform analysis Data Structures Using Java 5

Software Development Phase • Design – Algorithm – Structured design • Divide problem into Software Development Phase • Design – Algorithm – Structured design • Divide problem into subproblems and analyze each subproblem – Object-oriented design • Identify components (objects) which form the basis of solution • Determine how these objects interact with one another Data Structures Using Java 6

Software Development Phase • Three basic principles of object-oriented design (OOD) – Encapsulation: ability Software Development Phase • Three basic principles of object-oriented design (OOD) – Encapsulation: ability to combine data and operations in a single unit – Inheritance: ability to create new data types from existing data types – Polymorphism: ability to use the same expression to denote different operations Data Structures Using Java 7

Software Development Phase • Implementation – Write and compile programming code to implement classes Software Development Phase • Implementation – Write and compile programming code to implement classes and functions discovered in design phase Data Structures Using Java 8

Software Development Phase • Testing and Debugging – Test the correctness of the program Software Development Phase • Testing and Debugging – Test the correctness of the program to make sure it does what it is supposed to do – Find and fix errors – Run program through series of specific tests, test cases Data Structures Using Java 9

Software Development Phase • Testing and Debugging – Black-box testing: test based on inputs Software Development Phase • Testing and Debugging – Black-box testing: test based on inputs and outputs not the internal working of the algorithm or function – White-box testing: relies on the internal structure and implementation of a function or algorithm; ensures that every part of the function or algorithm executes at least once Data Structures Using Java 10

Algorithm Analysis: The Big-O Notation Data Structures Using Java 11 Algorithm Analysis: The Big-O Notation Data Structures Using Java 11

Algorithm Analysis: The Big-O Notation Data Structures Using Java 12 Algorithm Analysis: The Big-O Notation Data Structures Using Java 12

Algorithm Analysis: The Big-O Notation Data Structures Using Java 13 Algorithm Analysis: The Big-O Notation Data Structures Using Java 13

Algorithm Analysis: The Big-O Notation Data Structures Using Java 14 Algorithm Analysis: The Big-O Notation Data Structures Using Java 14

Algorithm Analysis: The Big-O Notation • Definition: Let f be a function of n. Algorithm Analysis: The Big-O Notation • Definition: Let f be a function of n. The term “asymptotic” means the study of the function f as n becomes larger and larger without bound. Data Structures Using Java 15

Asymptotic Notation (O) • Definition f(n) = O(g(n)) iff there exist positive constants c Asymptotic Notation (O) • Definition f(n) = O(g(n)) iff there exist positive constants c and n 0 such that f(n) cg(n) for all n, n n 0. • Examples – – – 3 n+2=O(n) /* 3 n+2 4 n for n 2 */ 3 n+3=O(n) /* 3 n+3 4 n for n 3 */ 100 n+6=O(n) /* 100 n+6 101 n for n 10 */ 10 n 2+4 n+2=O(n 2) /* 10 n 2+4 n+2 11 n 2 for n 5 */ 6*2 n+n 2=O(2 n) /* 6*2 n+n 2 7*2 n for n 4 */ Data Structures Using Java 16

Algorithm Analysis: The Big-O Notation Data Structures Using Java 17 Algorithm Analysis: The Big-O Notation Data Structures Using Java 17

 • • O(1): constant O(n): linear O(n 2): quadratic O(n 3): cubic O(2 • • O(1): constant O(n): linear O(n 2): quadratic O(n 3): cubic O(2 n): exponential O(logn) O(nlogn) Data Structures Using Java 18

nlogn n logn Data Structures Using Java 19 nlogn n logn Data Structures Using Java 19

Classes • class: reserved word; collection of a fixed number of components • Components: Classes • class: reserved word; collection of a fixed number of components • Components: member of a class • Members accessed by name • Class categories/modifiers – Private – Protected – public Data Structures Using Java 20

Classes • private: members of class not accessible outside class • public: members of Classes • private: members of class not accessible outside class • public: members of class accessible outside class • Class members: can be methods or variables • Variable members declared like any other variables Data Structures Using Java 21

Syntax • General syntax for defining a class: modifier(s) class Class. Identifier modifier(s) { Syntax • General syntax for defining a class: modifier(s) class Class. Identifier modifier(s) { class. Members } • General syntax for using the operator new: new class. Name() or new class. Name(argument 1, argument 2, . . . , argument. N) Data Structures Using Java 22

Classes • Syntax to access data member of class object or method: reference. Variable. Classes • Syntax to access data member of class object or method: reference. Variable. Name. member. Name • Shallow copying: two or more reference variables of the same type point to the same object • Deep copying: each reference variable refers to its own object Data Structures Using Java 23

Constructors • Guarantee that data members are initialized when object is declared • Automatically Constructors • Guarantee that data members are initialized when object is declared • Automatically execute when class object created • Name of constructor is name of class • More than one constructor can be present in one class • Default constructor: constructor without parameters Data Structures Using Java 24

The Copy Constructor • Executes when an object is instantiated • Initialized using an The Copy Constructor • Executes when an object is instantiated • Initialized using an existing object • Syntax public Class. Name(Class. Name other. Object) Data Structures Using Java 25

UML Diagram Data Structures Using Java 26 UML Diagram Data Structures Using Java 26

UML Diagram • Top box: Name of class • Middle box: data members and UML Diagram • Top box: Name of class • Middle box: data members and their data types • Bottom box: member methods’ names, parameter list, return type of method • + means public method • - means private method • # means protected method Data Structures Using Java 27

Example: class Clock my. Clock = new Clock(); your. Clock = new Clock(9, 35, Example: class Clock my. Clock = new Clock(); your. Clock = new Clock(9, 35, 15); Data Structures Using Java 28

Example: class Clock Data Structures Using Java 29 Example: class Clock Data Structures Using Java 29

Example: class Clock Data Structures Using Java 30 Example: class Clock Data Structures Using Java 30

The Method to. String • • • Public value-returning method Takes no parameters Returns The Method to. String • • • Public value-returning method Takes no parameters Returns address of String object Output using print and println methods Default definition creates String with name of object’s class name followed by hash code of object Data Structures Using Java 31

The Modifier Static • In method heading, specifies that method can be invoked by The Modifier Static • In method heading, specifies that method can be invoked by using name of class • If used to declare data member, data member invoked by using class name • Static data members of class exist even when no object of class type instantiated • Static variables are initialized to their default values Data Structures Using Java 32

static Data Members of a Class Illustrate illus. Object 1 = new Illustrate(3); Illustrate static Data Members of a Class Illustrate illus. Object 1 = new Illustrate(3); Illustrate illus. Object 2 = new Illustrate(5); Data Structures Using Java 33

Finalizers • Automatically execute when class object goes out of scope • Have no Finalizers • Automatically execute when class object goes out of scope • Have no parameters • Only one finalizer per class • Name of finalizer: finalize Data Structures Using Java 34

Creating Packages • Can create packages using reserved word package – Define the class Creating Packages • Can create packages using reserved word package – Define the class to be public (If class not public it can only be used within package) – Choose name for package. Organize package (create subdirectories) Data Structures Using Java 35

The Reference this • Refers to instance variables and methods of a class • The Reference this • Refers to instance variables and methods of a class • Used to implement cascaded method calls Data Structures Using Java 36

Inner Classes • Defined within other classes • Can be either a complete class Inner Classes • Defined within other classes • Can be either a complete class definition or anonymous inner class definition • Used to handle events Data Structures Using Java 37

Abstract Data Types • Definition: A data type that specifies the logical properties without Abstract Data Types • Definition: A data type that specifies the logical properties without the implementation details Data Structures Using Java 38

Programming Example: Candy Machine (Problem Statement) • A common place to buy candy is Programming Example: Candy Machine (Problem Statement) • A common place to buy candy is from a candy machine. A new candy machine is bought for the gym, but it is not working properly. The machine sells candies, chips, gum, and cookies. You have been asked to write a program for this candy machine so that it can be put into operation. • The program should do the following: 1. Show the customer the different products sold by the candy machine. 2. Let the customer make the selection. 3. Show the customer the cost of the item selected. 4. Accept money from the customer. 5. Release the item. Data Structures Using Java 39

 • Input Programming Example: Candy Machine (Input and Output) – Item Selection – • Input Programming Example: Candy Machine (Input and Output) – Item Selection – Cost of Item • Output – The selected item Data Structures Using Java 40

Programming Example: Candy Machine • Components – Cash Register – Several Dispensers Data Structures Programming Example: Candy Machine • Components – Cash Register – Several Dispensers Data Structures Using Java 41

Programming Example: Candy Machine Data Structures Using Java 42 Programming Example: Candy Machine Data Structures Using Java 42

Programming Example: Candy Machine Data Structures Using Java 43 Programming Example: Candy Machine Data Structures Using Java 43

Programming Example: Candy Machine Object chips Data Structures Using Java 44 Programming Example: Candy Machine Object chips Data Structures Using Java 44

Programming Example: Candy Machine Sample Run: In this sample run, the user input is Programming Example: Candy Machine Sample Run: In this sample run, the user input is shaded. *** Welcome to Shelly's Candy Shop *** To select an item, enter 1 for Candy 2 for Chips 3 for Gum 4 for Cookies 9 to exit 1 Please deposit 50 cents 50 Collect your item at the bottom and enjoy. Data Structures Using Java 45

Object-Oriented Design • Simplified methodology 1. Write down detailed description of problem 2. Identify Object-Oriented Design • Simplified methodology 1. Write down detailed description of problem 2. Identify all (relevant) nouns and verbs 3. From list of nouns, select objects 4. Identify data components of each object 5. From list of verbs, select operations Data Structures Using Java 46

Object-Oriented Design Example • Problem Statement – Write a program to input the dimensions Object-Oriented Design Example • Problem Statement – Write a program to input the dimensions of a cylinder and calculate and print the surface area and volume • Nouns – Dimensions, surface area, volume, cylinder Data Structures Using Java 47

Chapter Summary • Software Life Cycle – Development, Use, Maintenance • • • Algorithm Chapter Summary • Software Life Cycle – Development, Use, Maintenance • • • Algorithm Analysis: The Big-O Notation Classes UML Diagram Constructors, Destructors Abstract Data Types Data Structures Using Java 48

Chapter Summary • Software Development Phase – Analysis – Design • Structured design • Chapter Summary • Software Development Phase – Analysis – Design • Structured design • Object-oriented design – Encapsulation, Inheritance, Polymorphism – Implementation – Testing and Debugging Data Structures Using Java 49