52
1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek.

1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

1

Case Studies and Review

Lecture #14

Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek.

Page 2: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

2

Case1: Vehicle Management system(P197)

Specifications A tracking program for company vehicles; Identifying how and when vehicles are checked out

for usage by employees. Cargo transportation, business trip, loaner cars, … Record ID number, date checked out, driver name,

mileage, type of usage, and contents. Company use: each trip is recorded including date, time,

miles driven, fuel cost, and the purpose of the trip. Personal use: the insurance information should be

recorded. Carrying passengers: list of passengers; Carrying cargo, an inventory of the vehicle contents

Page 3: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

3

Analysis By Vehicle Usage

Vehicle

PersonalVehicle

Company Vehicle

Insurance Info

Triplog

Page 4: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

4

Classify By Vehicle Contents

Vehicle

CargoVehicle

Passenger Vehicle

TripLog

Insurance Info

CompanyVehicle

PersonalVehicle

Page 5: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

5

Vehicle classtypedef unsigned long ulong;class Vehicle {public: enum VehicleEnum { personal, company }; Vehicle( ulong idNum, VehicleEnum vtype ); Vehicle & Input(); void SetOdometer( ulong odom ); friend ostream & operator <<( ostream & os,const Vehicle & v );private: ulong id; // vehicle ID FString driver; // name of driver ulong odometer; // total mileage Date chkDate; // date checked out VehicleEnum vehicleType;};

Page 6: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

6

CompanyVehicle class

class CompanyVehicle :public Vehicle {public: CompanyVehicle( ulong idNum ); CompanyVehicle & Input(); friend ostream & operator <<( ostream & os, const CompanyVehicle & cv );private: TripLog tripRecord; // a single trip record};

Page 7: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

7

PersonalVehicle class

class PersonalVehicle :public Vehicle {

public:

PersonalVehicle( ulong id );

PersonalVehicle & Input();

friend ostream & operator <<( ostream & os, const PersonalVehicle & pv );

private:

InsuranceInfo insurance;

};

Page 8: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

8

InsuranceInfo class

class InsuranceInfo {

public:

InsuranceInfo & Input();

friend ostream & operator <<(ostream & os,

const InsuranceInfo & I );

private:

FString company; // name of insurance company

FString policyNumber;

};

Page 9: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

9

TripLog Classclass TripLog {public: TripLog(); TripLog & Input(); void SetMiles( unsigned m ); void SetFuelCost( float f ); friend ostream & operator <<( ostream & os, const TripLog & tLog );private: Date tDate; // date trip taken Time tTime; // time trip taken unsigned miles; // miles driven FString purpose; // why the trip was taken float fuelCost; // cost of fuel, in dollars};

Page 10: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

10

Date class

class Date {public:Date( unsigned dy, unsigned mn, unsigned yr ); Date & Input(); friend ostream & operator <<( ostream & os, const Date & dType );private: unsigned year; // 0 - 2100 unsigned month; // 1 - 12 unsigned day; // 1 - 31 void SetDay( unsigned dy ); void SetMonth( unsigned mn ); void SetYear( unsigned yr );};

Page 11: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

11

Time class

class Time {public:// Time(){hour =0; minute=0;}; Time( unsigned hr, unsigned mn ); Time & Input(); friend ostream & operator <<( ostream & os, const Time & tm );private: unsigned hour; // 0 - 23 unsigned minute; // 0 - 59 void SetHour( unsigned hr ); void SetMinute( unsigned mn );};

Page 12: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

12

Implementation:Date

Date::Date( unsigned dy = 0, unsigned mn = 0, unsigned yr = 0 )

{ SetDay( dy );

SetMonth( mn );

SetYear( yr );

}

Page 13: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

13

DateDate & Date::Input()

{ char temp[80];

char ch;

cout << "(mm/dd/yy format): ";

cin.getline( temp, '\n' );

istrstream input( temp, 80 );

input >> dec;

unsigned n;

input >> n;

SetMonth( n );

input.get( ch );

input >> n;

SetDay( n );

input.get( ch );

input >> n;

SetYear( n );

return *this;

}

Page 14: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

14

Date

void Date::SetDay( unsigned d )

{ day = d;}

void Date::SetMonth( unsigned m )

{ month = m;}

void Date::SetYear( unsigned y )

{ year = y; }

ostream & operator <<( ostream & os, const Date & D )

{

os << D.month << "-" << D.day << "-" << D.year;

return os;

}

Page 15: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

15

Time

Time::Time( unsigned hr = 0, unsigned mn = 0 )

{

SetHour( hr );

SetMinute( mn );

}

Page 16: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

16

TimeTime & Time::Input(){ char temp[80]; char ch; cout << "(hh:mm format): "; cin.getline( temp, '\n' ); istrstream input( temp, 80 ); input >> dec;

// Force decimal input format, to allow the // user to type leading zeros in numbers.

unsigned n;

input >> n;

SetHour( n );

input.get( ch );

input >> n;

SetMinute( n );

return *this;

}

Page 17: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

17

Timevoid Time::SetHour( unsigned h ){ hour = h;}void Time::SetMinute( unsigned m ){ minute = m;}ostream & operator <<( ostream & os, const Time & T ){ os << T.hour << ":" << T.minute; return os;}

Page 18: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

18

InsuranceInfoInsuranceInfo & InsuranceInfo::Input()

{ cout << "Insurance Information:" << '\n' << "Insurance company: ";

company.GetLine( cin );

cout << "Policy Number: ";

policyNumber.GetLine( cin );

return *this;

}

ostream & operator <<( ostream & os, const InsuranceInfo & I )

{ os << "Insurance Information............." << '\n'

<< " Insurance company: " << I.company << '\n'

<< " Policy number: " << I.policyNumber << endl;

return os;

}

Page 19: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

19

TripLog

TripLog::TripLog() :purpose(),tDate(0,1,1),tTime(0,0)

{

miles = 0;

fuelCost = 0.0;

}

Page 20: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

20

TripLog

TripLog & TripLog::Input(){ cout << "Entering the trip log." << '\n' << " Date of trip "; tDate.Input(); cout << " Time "; tTime.Input(); cout << " Miles driven: ";

unsigned n; cin >> n; SetMiles( n ); cin.ignore( 255, '\n' ); cout << " Purpose: "; purpose.GetLine( cin );

cout << " Fuel cost: ";

float f;

cin >> f;

SetFuelCost( f );

cin.ignore( 255, '\n' );

return *this;

}

Page 21: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

21

TripLog

void TripLog::SetMiles( unsigned m ){ miles = m;}void TripLog::SetFuelCost( float f ){ fuelCost = f;}ostream & operator <<( ostream & os, const TripLog & T ){ os << "Trip Log...................." << '\n' << " Date: " << T.tDate << '\n' << " Time: " << T.tTime << '\n' << " Miles: " << T.miles << '\n' << " Purpose: " << T.purpose << '\n' << " Fuel cost: " << T.fuelCost << endl; return os;}

Page 22: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

22

Vehicle

Vehicle::Vehicle( ulong idVal, VehicleEnum vType )

:id(idVal), vehicleType(vType),chkDate(0,1,1)

{

SetOdometer( 0 );

}

Page 23: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

23

VehicleVehicle & Vehicle::Input()

{ cout << "Driver name [last,first]: ";

driver.GetLine( cin );

cout << " Odometer reading: ";

long n;

cin >> n;

SetOdometer( n );

cin.ignore( 255, '\n' );

cout << " Date checked out ";

chkDate.Input();

return *this;

}

Page 24: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

24

Vehicle

void Vehicle::SetOdometer( ulong od )

{ odometer = od;}

ostream & operator <<( ostream & os, const Vehicle & V )

{ const char * vstring[] = { "personal","company" };

os << " ID: " << V.id << '\n'

<< " Usage: " << vstring[V.vehicleType] << '\n'

<< " Driver: " << V.driver << '\n'

<< " Odometer: " << V.odometer << '\n'

<< " Date out: " << V.chkDate << endl;

return os;

}

Page 25: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

25

CompanyVehicle

CompanyVehicle::CompanyVehicle( ulong idVal ) :Vehicle(idVal, company) { }CompanyVehicle & CompanyVehicle::Input(){ Vehicle::Input(); tripRecord.Input(); return *this;}ostream & operator <<( ostream & os, const CompanyVehicle & V ){ os << "Vehicle ....................." << '\n' << Vehicle( V ) << V.tripRecord << endl; return os;}

Page 26: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

26

PersonalVehicle

PersonalVehicle::PersonalVehicle( ulong idVal ) :Vehicle( idVal, personal ) { }PersonalVehicle & PersonalVehicle::Input(){ Vehicle::Input(); insurance.Input(); return *this;}ostream & operator <<( ostream & os, const PersonalVehicle & V ){ os << "Vehicle ...................." << '\n' << Vehicle( V ) // display base object << V.insurance << endl; return os;}

Page 27: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

27

Main program#include "Vehicle.h"#include <fstream.h>ofstream ofile("vehicle.out", ios::app );void Slicer( const Vehicle & V ){ cout << V << endl;}void Create_PersonalVehicle( ulong id ){ PersonalVehicle V(id); V.Input(); Slicer( V ); // demonstrate object slicing ofile << V << endl;}void Create_CompanyVehicle( ulong id ){ CompanyVehicle V(id); V.Input(); ofile << V << endl;}

Page 28: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

28

Main programvoid Create_Vehicle(){ int usage; ulong id; cout << "Vehicle ID: "; cin >> id; cout << "Type of Usage (1)personal, (2)company:"; cin >> usage; cin.ignore( 255, '\n' ); if( usage == 1 ) Create_PersonalVehicle( id ); else if( usage == 2 ) Create_CompanyVehicle( id );}int main(){ Create_Vehicle(); return 0;}//cis601source/ch6/vehicle/main.cpp

Page 29: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

29

Page 30: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

30

Page 31: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

31

Page 32: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

32

Page 33: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

33

Case2: Shape

Specification: Design a program that supports the

drawing, moving, computing the area and the perimeter of shapes such as circle, rectangle, triangle, etc.

Page 34: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

34

Class HierarchyShape

interior_color, shadowmove(), draw(), paint(), area(),

perimeter(), color()

Trianglevertices

height(), base()

Rectangleupper_left, lower_right

width(), length()

Square

Circle

A

Page 35: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

35

Circle

Shape

Point

Circle 1

1

Page 36: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

36

Circle Object creation interaction diagram

Circle Shape PointCircle(x,y,r)

Shape(color)

Point(x,y)

Process initialization list: initialize base class

Process initialization list: initialize center

Complete initializationConstructor’s body

Page 37: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

37

Rectangle and Square

Shape

Point

1

1Circle

Rectangle

Square

2 1

Page 38: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

38

Square Object Creation interaction diagram

Square Rectangle ShapeSquare(x1,y1,x2,y2,color)

Rectangle()

Process initialization list: initialize shape segment

Process initialization list: initialize upper_left

Complete initialization Constructor’s body

Point

Constructor’s body

Shape(color)

Point(x1,y1)

Point(x2,y2)Process initialization list: initialize lower_right

Call base class constructor

Page 39: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

39

Class Shape

typedef bool Boolean ;

enum Color{RED, BLUE, GREEN, WHITE, BLACK, YELLOW} ;

class Shape

{

private:

Color interior ;

Shape( const Shape & ) ;

Shape & operator =( const Shape & ) ;

protected:

inline Boolean change_color( Color new_color ) ;

Page 40: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

40

Class Shape

public:

inline Shape( Color interior_color ) ;

virtual ~Shape() { } ; // do-nothing

inline Color color( void ) const ;

virtual Boolean draw( void ) const = 0 ;

virtual Boolean paint( Color new_color ) = 0 ;

virtual Boolean move( Coordinate x , Coordinate y ) = 0 ;

virtual float area( void ) const = 0 ;

virtual float perimeter( void ) const = 0 ;

} ;

Page 41: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

41

Class Shape

inline Shape:: Shape( Color color ): interior ( color ) {};

inline Color Shape:: color( void ) const

{ return( interior ) ;}

inline Boolean Shape:: change_color( Color new_color )

{ Boolean accepted = true ;

if( interior != new_color )

{ interior = new_color ; }

else

{ accepted = false ; }

return( accepted ) ;

};

Page 42: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

42

Class Circle

typedef short Radius ;

const double PI = acos( -1.0 ) ;

class Circle: public Shape

{ private:

Point center ; // center coordinate

Radius r ;

Boolean erase( void ) const ;

Circle( const Circle & ) ;

Circle & operator = ( const Circle & ) ;

Page 43: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

43

Class Circlepublic:

Circle( Coordinate x, Coordinate y, Radius new_radius, Color interior_color);

~Circle( ) ;

inline Radius radius( void ) const ;

inline virtual Boolean draw( void ) const ;

virtual Boolean paint( Color new_color ) ;

virtual Boolean move( Coordinate x , Coordinate y ) ;

virtual float area( void ) const ;

virtual float perimeter( void ) const ;

} ;

inline Radius Circle:: radius( void ) const

{ return( r ) ;}

Page 44: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

44

Class Rectangle

class Rectangle: public Shape{ private:

Point upper_left , lower_right ; Boolean erase( void ) const ;Rectangle( const Rectangle & ) ;Rectangle & operator = ( const Rectangle & ) ;

public:Rectangle( Coordinate x1, Coordinate y1, Coordinate

x2, Coordinate y2, Color interior_color ) ; virtual ~Rectangle( ) ;virtual Boolean draw( void ) const ;virtual Boolean paint( Color new_color ) ;virtual Boolean move( Coordinate x , Coordinate y ) ;virtual float area( void ) const ;virtual float perimeter( void ) const ;

} ;

Page 45: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

45

Class Square

typedef unsigned short Length ;

class Square: public Rectangle

{ private:

Square( const Square & ) ;

Square & operator = ( const Square & ) ;

public:

Square( Coordinate x1, Coordinate y1, Length side,

Color interior_color ) ;

virtual ~Square( ) { } ;

virtual Boolean draw( void ) const ;

virtual Boolean paint( Color new_color ) ;

} ;

Page 46: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

46

class Point

typedef float Coordinate ;class Point{ private:

Coordinate x , y ;public:

inline Point() ; // default constructorinline Point( Coordinate new_x, Coordinate

new_y ) ;inline Coordinate x_coordinate( void ) const ;inline Coordinate y_coordinate( void ) const ;

} ;

Page 47: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

47

Main()

#define MAX_NUM_SHAPES 3int main( int , char ** ) ;Shape * make_shape( int i ) ;void delete_shape ( Shape *current ) ;Boolean process_shape( ostream &output_stream, Shape

*current ) ;int main( int , char ** ){ Shape *list[MAX_NUM_SHAPES] ;

int i ;for( i = 0 ; i < MAX_NUM_SHAPES ; i++ ){ list[i] = make_shape( i ) ; }

Page 48: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

48

Main()

for( i = 0 ; i < MAX_NUM_SHAPES ; i++ ){ cout << "\n ** Shape No. " << i ;

process_shape( cout, list[i] ) ;}cout << endl ;for( i = 0 ; i < MAX_NUM_SHAPES ; i++ ){ cout << "\n ** Destroying Shape No. " << i ;

delete_shape( list[i] ) ;}return( 0 ) ;

}

Page 49: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

49

process_shape(…)Boolean process_shape( ostream &output_stream, Shape *current ){ Boolean status ;

status = current->draw() ;if( !status ){output_stream << "\n\t Draw Tests ............. Failed " ; }status = current->paint( YELLOW ) ;if( !status ){output_stream << "\n\t Paint Tests ............. Failed " ; }status = current->move( 1 , 1 ) ;if( !status ){output_stream << "\n\t Move Tests .............. Failed " ;}output_stream << "\n\t Area of Shape:" << current->area() ;output_stream << "\n\t Perimeter of Shape:" << current->perimeter() ;return( status ) ;

}

Page 50: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

50

make_shape(…)Shape * make_shape( int i )

{ Shape *current ;

if( i == 0 )

{ current = new Circle( 1 , 3 , 4 , RED ) ; }

else if( i == 1 )

{current = new Rectangle( 8, 5, 12, -2, GREEN ) ; }

else

{current = new Square( -6 , 3 , 3 , BLUE ) ;}

return( current ) ;

}

void delete_shape ( Shape *current )

{ delete current ;}//uo/draw/testshape.dsw

//draw/wzd.dsw

Page 51: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

51

Page 52: 1 Case Studies and Review Lecture #14 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised

52