Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright  1997 Oxford University Press All Rights Reserved

Similar presentations


Presentation on theme: "Copyright  1997 Oxford University Press All Rights Reserved"— Presentation transcript:

1 Copyright  1997 Oxford University Press All Rights Reserved
Transparency Masters for Chapter 1 of Data Structures via C++ Objects by Evolution A. Michael Berman Copyright  1997 Oxford University Press All Rights Reserved

2 Copyright  1997 Oxford University Press All Rights Reserved
Chapter 1 Software Engineering and | Computer Programming Overview A preview and a context for the subjects covered in this book. Copyright  1997 Oxford University Press All Rights Reserved

3 Copyright  1997 Oxford University Press All Rights Reserved
Chapter Objectives 1. To outline the relationship between Computer Science, Software Engineering, and the topics covered in this book. 2. To define “Data Structures” and “Abstract Data Types”. 3. To provide an overview of the process of software development 4. To describe the rationale for using the programming language C++. Copyright  1997 Oxford University Press All Rights Reserved

4 Copyright  1997 Oxford University Press All Rights Reserved
Figure 1-1: The concerns of the computer scientist and the software engineer Copyright  1997 Oxford University Press All Rights Reserved

5 Copyright  1997 Oxford University Press All Rights Reserved
Definition 1-1 A data structure consists of a base storage method (e.g., an array) and one or more algorithms that are used to access or modify that data. Copyright  1997 Oxford University Press All Rights Reserved

6 Copyright  1997 Oxford University Press All Rights Reserved
Code Example 1-1: A program demonstrating a dice data structure (part 1 of 2) // cx1-1.cpp // Code Example 1-1: a dice program that does not use an adt // // Illustrates the solution to a "dice simulation", without using // abstract data types. Compare this approach to cx1-2.cpp. #include "dslib.h" // contains standard header files for this book #include <stdlib.h> int main() { int die1, die2, total; randomize(); // initialize the pseudorandom number generator Copyright  1997 Oxford University Press All Rights Reserved

7 Copyright  1997 Oxford University Press All Rights Reserved
Code Example 1-1: A program demonstrating a dice data structure (part 2 of 2) die1 = random(6) + 1; // random(6) returns random number between 0 and 5 die2 = random(6) + 1; total = die1 + die2; cout << "first die: " << die1 << ", second die: " << die2 << endl; cout << "total for roll is: " << total << endl; return 0; } Copyright  1997 Oxford University Press All Rights Reserved

8 Copyright  1997 Oxford University Press All Rights Reserved
Definition 1-2 An Abstract Data Type (ADT) is a well- specified collection of data and a group of operations that can be performed upon the data. The ADT’s specification describes what data can be stored (the characteristics of the ADT), and how it can be used (the operations), but not how it is implemented or represented in the program. Copyright  1997 Oxford University Press All Rights Reserved

9 Copyright  1997 Oxford University Press All Rights Reserved
ADT 1-1: Dice (part 1 of 2) Characteristics: Represents a pair of 6-sided dice, that can be rolled to get a random sum between 2 and 12. Operations: int roll() Precondition: none. Postcondition: A random value between 1 and 6 is stored for each of the dice. Returns: The sum of the two dice values, lying between 2 and 12. Copyright  1997 Oxford University Press All Rights Reserved

10 Copyright  1997 Oxford University Press All Rights Reserved
ADT 1-1: Dice (part 2 of 2) int die1() Precondition: The dice have been rolled at least once. Postcondition: None. Returns: The value of the first die. int die2() Returns: The value of the second die. Copyright  1997 Oxford University Press All Rights Reserved

11 Code Example 1-2: A program using a Dice ADT (Part 1 of 4)
// cx1-2.cpp // Code Example 1-2: a dice program that uses an adt // // Illustrates the solution to a "dice simulation", using // abstract data types. Compare this approach to cx1-1.cpp. // Dice ADT // Characteristics: // Represents a pair of 6-sided dice, that can be rolled to get a // random sum between 2 and 12. Copyright  1997 Oxford University Press All Rights Reserved

12 Code Example 1-2: A program using a Dice ADT (Part 2 of 4)
// Operations: // int roll() // Preconditions: none // Postcondition: A random value between 1 and 6 is stored for each of // the dice. // Returns: The sum of the two dice values, lying between 2 and 12 // int die1() // Precondition: The dice have been rolled at least once. // Postcondition: None // Returns: The value of the first die. // int die2() // Returns: The value of the second die. Copyright  1997 Oxford University Press All Rights Reserved

13 Code Example 1-2: A program using a Dice ADT (Part 3 of 4)
// Note on encapsulation: The representation of the dice is contained // within global variables -- we'll see shortly a better way to do // this. #include <iostream.h> #include <stdlib.h> #include "dslib.h" int dice_1, dice_2; int roll() { // note -- you don't really want to call randomize every time you // roll the dice, but for this simplified design you don't have // much choice. randomize(); dice_1 = random(6) + 1; dice_2 = random(6) + 1; return dice_1 + dice_2; } Copyright  1997 Oxford University Press All Rights Reserved

14 Code Example 1-2: A program using a Dice ADT (Part 4 of 4)
int die1() { return dice_1; } int die2() return dice_2; // end of Dice ADT int main() // test program to demonstrate Dice ADT cout << "The value rolled is: " << roll() << endl; cout << "The first die was: " << die1() << endl; cout << "The second die was: " << die2() << endl; return 0; Copyright  1997 Oxford University Press All Rights Reserved

15 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 1-1 In the software distribution for this book you can find the programs cx1-1 and cx1-2, representing the Data Structure and ADT described above. Read, compile, and compare these two programs. 1-2 Design a different implementation of Dice, and modify cx1-2 to reflect your new implementation. How is the “main” function in cx1-1 affected by your change? 1-3 Design a different implementation of Dice, and modify cx1-2 to reflect your new implementation. How is the “main” function in cx1-2 affected by your change? Copyright  1997 Oxford University Press All Rights Reserved

16 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 1-4 Some games for young children use colored squares on a board and a “spinner” that, in effect, picks one of the colors at random. Suppose that the game has five colors  red, green, blue, yellow, and orange. Design a “Spinner” ADT. 1-5 Implement your “Spinner” from Exercise 1-4. Copyright  1997 Oxford University Press All Rights Reserved

17 Figure 1-2: One version of the waterfall model of software development
Initiation Analysis Design Implementation Testing Maintenance Copyright  1997 Oxford University Press All Rights Reserved

18 Copyright  1997 Oxford University Press All Rights Reserved
Table 1-1: The waterfall model: A summary of the players and the deliverables Copyright  1997 Oxford University Press All Rights Reserved

19 Copyright  1997 Oxford University Press All Rights Reserved
Exercises 1-6 What are the pluses and minuses of using comments in source code for systems documentation? 1-7 Determine the name and version number of the C++ compiler you will be using for your course. What standards, if any, does your compiler correspond with? 1-8 Determine which of the following relatively recent features of C++ your compiler supports: templates, exceptions, namespaces, Standard Template Library. Copyright  1997 Oxford University Press All Rights Reserved

20 Chapter Summary (part 1 of 2)
The subject matter of this book includes topics from Computer Science  the study of the principles that underlie computer software  and hardware and Software Engineering  the application of these principles to the development of software systems. Three processes encompass the work of the computing professional: theory, abstraction, and design. Data Structures implement Abstract Data Types. The Waterfall models the process of software development. Copyright  1997 Oxford University Press All Rights Reserved

21 Chapter Summary (Part 2 of 2)
Software engineers often supplement the waterfall model with development of prototypes. The trend toward reusable software has driven the development of new models of software development. C++ supports data abstraction and object-oriented programming. Copyright  1997 Oxford University Press All Rights Reserved

22 Programming Laboratory Problems
1-1 Using the Dice ADT from Section 1.1.1, write a program to measure the results of rolling 1000 pair of dice. Keep track of how many times the pair sums to each possible value between 2 and 12, and print out a histogram of the results. 1-2 Using the Dice ADT from Section 1.1.1, write a program that plays the game “Craps”. In Craps, the player makes a bet, then rolls a pair of dice. If the first roll is 7, the player wins the bet; if the first roll is 2, 11, or 12, the player loses. Otherwise, the total is called the “point”, and the player rolls pairs of dice until the dice add up to the point again (a winner) or until 7 is rolled (called “craps”, a loser). Copyright  1997 Oxford University Press All Rights Reserved

23 Programming Laboratory Problems
1-3 Design and implement an ADT to represent a coin flip. Your ADT should have an operation, “flip”, which randomly returns a value “heads” or “tails” each time it is called. Test your ADT in a program that flips 10,000 coins and reports the number of heads and tails. 1-4 Design and implement an ADT for money. Note that money cannot be represented accurately by a floating point number, because of rounding. Test your ADT by writing a program that simulates a cash register  the user enters amounts which are added together and a total is reported. You can enhance your program by adding a tax computation, and a way to back out a transaction after it has been entered. Copyright  1997 Oxford University Press All Rights Reserved

24 Programming Laboratory Problems
1-5 Find a program that you wrote last semester. Write a functional specification for the program. Develop, in consultation with your instructor, a significant improvement to the program, and write a revised functional specification for the new program. Modify your program to correspond with the new specification. What things about the way you wrote your original program made it easier to modify? What things made it harder? 1-6 Find a program that you wrote in a language other than C++, and convert it to C++. In what ways is the program different in C++ than it was in the original language? Copyright  1997 Oxford University Press All Rights Reserved

25 Programming Laboratory Problems
1-7 The C++ standard includes a definition of a String ADT. C++ also supports the traditional “C”-style string (char *). Write a program, using the String ADT, that creates a string of length 1000, puts random characters into the string, and then counts the number of times the letter “A” is found. Rewrite the program using “C”- style strings. Using the “Stopwatch” class provided by the author of this book, compare the performance of the two programs. If you see no difference, try making the problem bigger. Report the performance difference, if any, between the two String implementations. Copyright  1997 Oxford University Press All Rights Reserved

26 Programming Laboratory Problems
1-8 (For those already familiar with C++ classes.) Rewrite the Dice ADT as a C++ class. Test your class by instantiating several Dice objects. What are the advantages of using a C++ class? Are there any disadvantages? 1-9 Design an ADT and a data structure for storing an header. You can assume that the following fields are required: From, To, Reply-To, Date, and Subject, and that the each line can be stored in a single string. Implement your ADT, and a program that tests it by first prompting the user to enter the data for the header and then printing it out. (Hint: I recommend you use C++ strings, not C (char *) strings.) Copyright  1997 Oxford University Press All Rights Reserved

27 Programming Laboratory Problems
1-10 Using the facilities provided by your implementation of C++, write a program that uses a graphical user interface (GUI) to open a window, read in a word, and convert it to uppercase. What abstraction mechanisms does the GUI software use? Copyright  1997 Oxford University Press All Rights Reserved


Download ppt "Copyright  1997 Oxford University Press All Rights Reserved"

Similar presentations


Ads by Google