Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lab2 C++ Warmup: Pet Applicatioin Cop 4331 and EEL4884 © Dr. David A. Workman School of EE and Computer Science University of Central Florida February 12, 2010 Revised: February 15, 2010 Problem Statement Write a C++ application for modeling "pets" – those furry animals called "cats" and "dogs." The application shall read instances of pets commands from an external ASCII file, call methods appropriate to the command and type of pet (cat or dog) to simulate its behavior. In addition, the command and the pet's response shall be written to an ASCII output stream. Detailed functional and non-functional requirements will be given for each class in this application. February 15, 2010 (c) Dr. David A. Workman 2 Input Requirements Sample Input Command{ action: name? pet1: Pet{ name: Thing dna: pet sex: female }Pet }Command Command{ action: speak! pet1: Dog{ name: Fido dna: dog sex: male }Dog }Command Command{ pet1: Dog{ name: Fido dna: dog sex: male }Dog action: mate! pet2: Dog{ name: Suade dna: dog sex: female }Dog }Command Command{ action: color? pet1: Cat{ name: Calvin dna: cat sex: male color: white }Cat }Command END Requirements: 1. The OO data format shall be used for Command, Pet and each subclass of Pet. 2. The set of legal action verbs is specific to the pet class. 3. An END token terminates the input stream. February 15, 2010 (c) Dr. David A. Workman 3 Output Requirements Sample Output Command{ action: name? pet1: Pet{ name: Thing dna: pet sex: female }Pet My name is: Thing }Command Command{ action: speak! pet1: Dog{ name: Fido dna: dog sex: male }Dog }Command My bark is: Woof Command{ pet1: Dog{ name: Fido dna: dog sex: male }Dog action: mate! pet2: Dog{ name: Suade dna: dog sex: female }Dog }Command The offspring is: Dog{ name: Fido+Suade dna: dog sex: male }Dog Command{ action: color? pet1: Cat{ name: Calvin dna: cat sex: male color: white }Cat }Command My color is: white Requirements: 1. The OO data format shall be used for Command, Pet and each subclass of Pet. 2. The set of legal action verbs is specific to the pet class. 3. A blank line shall separate the group of lines defined by a Command and its output. February 15, 2010 (c) Dr. David A. Workman 4 Other Application Requirements 1. The application shall use the facilities of IOMgmt for managing the input and output streams; specifically, InMgr, OutMgr and TokenError. 2. Each Command shall be echoed to the output stream (if valid) followed by the command output on a separate line. 3. All string manipulation shall involve operations on instances of C++ class string (no features of C). February 15, 2010 (c) Dr. David A. Workman 5 The Command Class Command // TBD - developer’s discretion +Command() +Command( ifstream& ) +Execute( ostream& ) // AppError exception +Extract( ifstream& ) // TokenError exception +Insert( ostream& ) friend operator>> //TokenError exception friend operator<< #Get( ifstream& ) // TokenError exception #Put( ostream& ) The Command class defines instances in one of two forms: (a) Command{ action: unaryOp pet1: pet-instance-1 }Command (b) Command{ pet1: pet-instance-1 action: binaryOp pet2: pet-instance-2 }Command Where unaryOp is a string that defines a unary operation on a single instance of the Pet class (or subclass), and where binaryOp is a string that defines a binary operation on two instances of the Pet class (or subclass). Both unaryOp and binaryOp must be validated by the class to which the operation applies. This shall be done By calling the appropriate Perform() method of the given class. The Execute() method executes a valid command and produces its resulting output on the output stream passes as a reference parameter. Execute() may propagate an AppError exception thrown by Perform(). February 15, 2010 (c) Dr. David A. Workman 6 The Pet Class Unary Ops Response Pet name: string dna: string sex: string Name? Species? Sex? // default = Apet // default = Pet //(male | female) +Pet() +Pet( string, string, string ) +Perform( string ) // AppError exception +MyNameIs(): string +MySpeciesIs(): string +MySexIs(): string +Extract( ifstream& ) // TokenError exception +Insert( ostream& ) friend operator>> //TokenError exception friend operator<< #Get( ifstream& ) // TokenError exception #Put( ostream& ) February 15, 2010 My name is: MyNameIs() My species is: MySpeciesIs() My sex is: MySexIs() void Perform( string cmd, ostream& fout ) throw (AppError); This method validates the (cmd) parameter and throws an appropriate AppError exception if it not a valid command verb. If the verb is valid, this method produces the appropriate response on the output stream (fout). (c) Dr. David A. Workman 7 Dog Class Interface Dog Command Verb // value of dna must == "dog" +Dog() +Dog( string, string, string ) //MyNameIs() is inherited from Pet //MySpeciesIs() is inherited from Pet //MySexIs() is inherited from Pet +Perform( string, ostream&) //Overrides Pet +Perform( string, ostream&, Dog&) +Extract( ifstream& ) //Overrides Pet +Insert( ostream& ) //Overrides Pet +Bark(): string +Mate( Dog ) : Dog //parameter == B #Get( ifstream& ) #Put( ostream& ) Name? Species? Sex? Speak! Mate! Response My name is: MyNameIs() My species is: MySpeciesIs() My sex is: MySexIs() My bark is: Bark() The offspring is: A.Breed(B) string Bark() Returns "Woof" (male) or "Woof Woof" (female) Dog Mate ( Dog B) throw(AppError) Given A, B are instances of Dog and the call is made: A.Mate(B). This results from A.Perform( cmd, fout, B) This method throws an AppError exception if A and B are of the same sex, or either of them is not of the "dog" species. If both instances are well-defined, the resulting offspring shall have a name formed by concatenating A.MyNameIs() with B.MyNameIs(). The sex of the offspring shall always be A.MySexIs(). February 15, 2010 (c) Dr. David A. Workman 8 Dog Class Requirements 1. Every Dog shall provide the same behavior common to all pets. 2. The image of a Dog instance shall have the following format: Dog{ name: <pet name> dna: dog sex: <male | female> }Dog 3. Dogs will bark when asked. A male Dog will bark "Woof", a female Dog will bark "Woof Woof". 4. Dogs shall mate, provided they are of the opposite sex. A Dog can only mate with a member of the same species. The name of the offspring shall be constructed from the names of the parents to the breed operation. The sex of the offspring shall be the same as the Dog to which the breed operation is applied; e.g., A.Mate(B) => C, where C has name "nameof(A)+nameof(B)" and the same sex as A. 5. An AppError exception shall be thrown by the breed operation if there is a species violation or a sex violation. The exception message shall indicate which violation occurred. February 15, 2010 (c) Dr. David A. Workman 9 Cat Class Interface Command Verb Cat color: string +Cat() +Cat( string, string, string, string ) //MyNameIs() is inherited from Pet //MySpeciesIs() is inherited from Pet //MySexIs() is inherited +Perform( string, ostream& ) //overrides Pet +MyColorIs(): string +Cry(): string +Extract( ifstream& ) //Overrides Pet +Insert( ostream& ) //Overrides Pet #Get( ifstream& ) #Put( ostream& ) Name? Species? Sex? Speak! string Cry() Response My name is: MyNameIs() My species is: MySpeciesIs() My sex is: MySexIs() My cry is: Cry() Returns "Meow" Note1: Extract() and Get throw a TokenError exception if there are syntax violations. February 15, 2010 (c) Dr. David A. Workman 10 Cat Class Requirements 1. Every Cat shall provide the same behavior common to all pets. 2. The image of a Cat instance shall have the following format: Cat{ name: <pet name> dna: cat sex: <male | female> color: <pet color> }Cat 3. Cats will cry when asked. A Cat's cry is "Meow" (male and female). February 15, 2010 (c) Dr. David A. Workman 11 Pet Application Design Model Command AppError 1..2 IOMgmt Pet TokenError Tokenizer OutMgr IOError StringTokenizer InMgr PetApp Cat Dog (main) The main() function of PetApp shall open the input stream and create the output stream using classes InMgr and OutMgr. It shall process the input stream by extracting and executing commands until the “END” token in encountered. After processing the input stream, both input and output streams shall be closed by main(). Appropriate catch-blocks shall be provided to handle exceptions. Exceptions shall be handled by outputting Them to the user terminal. February 15, 2010 (c) Dr. David A. Workman 12 Lab2 Deliverables 1. 2. 3. 4. 5. Source files for the Pet Application ( PetApp.cpp, Pet.h, Pet.cpp, Cat.h, Cat.cpp, Dog.h, Dog.cpp, Command.h, Command.cpp ) Test data input and output files. UNIX Makefile for the Pet Application. Personal Evaluation forms. A Word document (professional style) that describes how your team would modify the design of PetApp to add the capability, for example, of saving the result of a “mate” operation on class Dog so that it could be used in a subsequent “mate” command? More generally, suppose we want the capability of saving the result of any operation that produces an instance of the class to which the operation applies for later reference in a subsequent command. This capability amounts to designing and implementing a “Pet Programming Language”. Your design shall deviate as little as possible from the present design. That is, the existing classes should still be valid with perhaps minor modifications. You may add additional modules to the design as you see fit. (Hint: consider an addressable memory). Your design document (in Word) should describe the changes necessary to each existing module of the PetApp and any new modules you add to the design. The goal here is enhance the application with least effort spent on rework and testing. 6. All deliverables shall be submitted via email to the instructor ([email protected]) in the form of a single (.zip) file (perhaps with a disguised extension) as you did for Lab1. February 15, 2010 (c) Dr. David A. Workman 13 Team Guidelines • The primary learning objectives are the following: – For each student to develop and test C++ code in the context of a team project; team leads shall make this a priority. I suggest having those members that are least experienced with C++ write the code, while those with the most C++ experience independently test the code. This is the “paired programming” concept used in industry. – For each student to become familiar with the OO Data Format and Design protocol and how it exploits runtime binding in C++. – For each student to know how to write a simple Makefile and use it to compile and run the pet application in both a UNIX and a Windows environment. – Finally, for all members to become more experienced in developing OO design exploiting information hiding, data encapsulation and separation of concerns. • I strongly encourage leads to employ design reviews and code walkthroughs as part of the software quality assurance process for your team. February 15, 2010 (c) Dr. David A. Workman 14