CSE 2341 - Honors Principles of Computer Science I

24
Spring 2008 Mark Fontenot [email protected] CSE 2341 - Honors Principles of Computer Science I Note Set 3 1

description

CSE 2341 - Honors Principles of Computer Science I. Spring 2008 Mark Fontenot [email protected]. Note Set 3. Take Note!!!. Next Tuesday, January 22, 2008 Follows a Monday Schedule!!!. Quick Look. Activity Diagrams and the UML Use default and overloaded constructors - PowerPoint PPT Presentation

Transcript of CSE 2341 - Honors Principles of Computer Science I

Page 1: CSE 2341 - Honors Principles of Computer Science I

Spring 2008

Mark [email protected]

CSE 2341 - HonorsPrinciples of Computer Science I

Note Set 31

Page 2: CSE 2341 - Honors Principles of Computer Science I

Take Note!!!

2

Next Tuesday,January 22, 2008

Follows a Monday Schedule!!!

Page 3: CSE 2341 - Honors Principles of Computer Science I

Quick Look

3

Activity Diagrams and the UMLUse default and overloaded constructorsDeclare and work with an array of objectsValidate user input from within a class

Page 4: CSE 2341 - Honors Principles of Computer Science I

UML®

4

“helps you specify, visualize, and document models of software systems, including their structure and design”*

Used heavily in industry to design software (and other things as well)

Can be used to explore a current code base by reverse engineering to UML models

UML specifies 13 different diagrammatic tools to use in design of system architecture

* Siegel, Jon. Introduction to OMG's Unified Modeling Language™ (UML®), http://www.omg.org/gettingstarted/what_is_uml.htm. July 2005

Page 5: CSE 2341 - Honors Principles of Computer Science I

Our First UML Diagram – Activity Diagram

5

Activity Diagram models logiccan be used at various levels of detail (30,000 foot view or

microscopic view of)Somewhat similar to flowcharting

Sample Activity Diagram

Page 6: CSE 2341 - Honors Principles of Computer Science I

Main Activity Diagram Artifacts

6

Initial Node – indicates starting point of logic

Final Node – indicates stopping point of logic

Activity – rounded rectangle – represents the activities that are taking place

Flow - arrow indicating flow of logic

Decision – Diamond with one flow entering and two or more exiting

Ambler, Scott. UML 2 Activity Diagrams, August 20 2006. http://www.agilemodeling.com/artifacts/activityDiagram.htm

Page 7: CSE 2341 - Honors Principles of Computer Science I

Main Activity Diagram Artifacts (2)

7

Merge – Diamond – two or more flows entering and one exiting

Condition – text in square brackets indicated a condition that must be satisfied to traverse its flow.

[credentials valid] [credentials not valid]

Ambler, Scott. UML 2 Activity Diagrams, August 20 2006. http://www.agilemodeling.com/artifacts/activityDiagram.htm

Page 8: CSE 2341 - Honors Principles of Computer Science I

Examples with pseudocode

8

If x is greater than 60print “YOU PASSED”

Page 9: CSE 2341 - Honors Principles of Computer Science I

Examples with pseudocode

9

Initialize value to – 3Loop while value is less than 10

value = value + 2

Page 10: CSE 2341 - Honors Principles of Computer Science I

Overloaded Constructors

10

#ifndef INVENTORYITEM_H#define INVENTORYITEM_H#include <cstring>using namespace std;class InventoryItem{private:char *description;int units;

public:InventoryItem()

{ description = new char[51]; }InventoryItem(char *desc)

{ description = new char[strlen(desc)+1]; strcpy(description, desc);

}

Inve

ntoryItem.h

Page 11: CSE 2341 - Honors Principles of Computer Science I

Overloaded Constructors

11

InventoryItem(char *desc, int u) { description = new char [strlen(desc) + 1]; strcpy(description,desc); units = u; } ~InventoryItem() { delete[] description; } void setDescription(char *d) { strcpy(description, d); } void setUnits (int u) { units = u; } char *getDescription() { return description; } int getUnits() { return units; }};#endif

Inve

ntoryItem.h

Page 12: CSE 2341 - Honors Principles of Computer Science I

Declaring Objects

12

Because of the overloaded constructors, can instantiate in different ways

InventoryItem ii;InventoryItem ii2(“Wrench”);InventoryItem ii3(“Hammer”, 10);

Page 13: CSE 2341 - Honors Principles of Computer Science I

Constructors and Destructors

13

A class can only have one default constructorA class can have any number of overloaded constructorsA class can have only one destructor

Page 14: CSE 2341 - Honors Principles of Computer Science I

Objects and Arrays

14

Possible to declare arrays of objects

Default constructor would be called for each element in the array.

InventoryItem store[10];

Page 15: CSE 2341 - Honors Principles of Computer Science I

Objects and Arrays

15

May use initialization lists to call overloaded constructors

Square tres[3] = {5, 6, 10};

InventoryItem store[3] = { “Wrench”, “Hammer”, “Pliers” };

Page 16: CSE 2341 - Honors Principles of Computer Science I

Objects and Arrays

16

Need more complex initialization list if number of args to constructor is greater than 1

InventoryItem inventory[3] = {

“Hammer”, InventoryItem(“Wrench”,17), }

First Element

Second Element

Third Element- Which constructor????

Page 17: CSE 2341 - Honors Principles of Computer Science I

Objects and Arrays

17

InventoryItem inventory[3] = {

InventoryItem(“Hammer”, 12), InventoryItem(“wrench”, 17), InventoryItem(“pliers”, 10) };

for (int i =0; i < 3; i++){ cout << inventory[i].getDescription(); cout << inventory[i].getUnits();}

Page 18: CSE 2341 - Honors Principles of Computer Science I

Design Time

18

Design an array class. The class should create an array of integers of any size at runtimeperform bounds checkingallows setting of a particular element of an arrayretrieves a particular element from the array

Page 19: CSE 2341 - Honors Principles of Computer Science I

Intlist

19

#ifndef INTLIST_H#define INTLIST_Hclass IntList{ private: int *list; int numElements; bool isValid(int);public: IntList(int); ~IntList();

bool set(int, int);bool get(int, int &);

};#endif

Intlist.h

Page 20: CSE 2341 - Honors Principles of Computer Science I

Intlist

20

#include “IntList.h”

IntList::IntList (int size){ list = new int[size]; numElements = size; for (int ndx =0; ndx < size; ndx++) list[ndx] = 0;}

IntList::~IntList(){ delete [] list;}

Intlist.cpp

Page 21: CSE 2341 - Honors Principles of Computer Science I

Intlist

21

bool IntList::isValid(int element){ if (element < 0 || element >= numElements) return false; else return true;}bool IntList::set(int element, int value){ if (isValid(element)) { list[element] = value; return true; } else return false;}

Intlist.cpp

Page 22: CSE 2341 - Honors Principles of Computer Science I

Intlist

22

bool IntList::get(int element, int &value){ if (isValid(element)) { value = list[element]; return true; } else return false;}

Intlist.cpp

Page 23: CSE 2341 - Honors Principles of Computer Science I

Take Note!!!

23

Next Tuesday,January 22, 2008

Follows a Monday Schedule!!!

Page 24: CSE 2341 - Honors Principles of Computer Science I

24

?