Assignment #3: Introduction to OOP
作业c++ | 作业html | assignment – 该题目是一个常规的c++的练习题目代写, 涵盖了c++/html等方面, 这个项目是assignment代写的代写题目
Due Date 1:Friday, March 3rd, 2023, 5:00 pm EST
Due Date 2:Friday, March 10th, 2023, 5:00 pm EST
Learning objectives:
- Object-Oriented programming, Invariants and Encapsulation
- c++ classes, constructors, destructors, and operations
- Dynamic memory allocation for C++ objects and arrays
- Iterator design pattern
- Questions 1a, 2a and 3a are due on Due Date 1; questions 1b, 2b, and 3b, are due on Due Date 2.
- On this and subsequent assignments, you will take responsibility for your own testing. This assignment is designed to get you into the habit of thinking about testingbeforeyou start writing your program. For each question you will be given a compiled executable program that is a program representing a solution to each question. You should use these provided executables to help you write your test cases, as they can show you the resultant output for given inputs. If you look at the deliverables and their due dates, you will notice that there isnoC++ code due on Due Date 1. Instead, you will be asked to submit test suites for C++ programs that you will later submit by Due Date 2. Test suites will be in a format compatible with that of the latter questions of Assignment 1, so if you did a good job writing yourrunSuitescript, that experience will serve you well here.
- Design your test suites with care; they are your primary tool for verifying the correctness of your code. Note that test suite submissionzipfiles are restricted to contain a maximum of 40 tests. The size of each input (.in) file is also restricted to 300 bytes. This is to encourage you not to combine all of your testing eggs in one basket.
- You must use the standard C++ I/O streaming and memory management (MM) facilities on this assignment; you may notuse C-style I/O or MM. More concretely, you may#includethe following C++ libraries (and no others!) for the current assignment:iostream,fstream,sstream,iomanip, andstring. Marmoset will be setup toreject submissions that use C-style I/O or MM, or libraries other than the ones specified above.
- We will manually check that you follow a reasonable standard of documentation and style, and to verify any assignment requirements that are not automatically enforced by Marmoset. Code to a standard that you would expect from someone else if you had to maintain their code. Further comments on coding guidelines can be found here:https://www. student.cs.uwaterloo.ca/ cs246/W23/codingguidelines.shtml
- We have provided some code and sample executables under the appropriatea3subdirectories.These executables have been compiled in the CS student environment and will not run anywhere else.
- You may not ask public questions on Piazza about what the programs that make up the assignment are supposed to do.A major part of this assignment involves designing test cases, and questions that ask what the programs should do in one case or another will give away potential test cases to the rest of the class. Questions found in violation of this rule will be marked private or deleted; repeat offences could be subject to discipline.
Note:We suggest creating the directory~/cs246/w23/a3and creating all of your assignment solutions in this directory.
Question 1
(25% DD1 and DD2)What is the largest integer? Ask a mathematician, and they will tell you theres no such thing. Ask a computer scientist, and they might sayINTMAX. Ask Bob Odenkirk, and he will tell you that 24 is the highest number. In this question, you write a classExtendedInt, which supports the operations+, -, *, /, >>,and<<. This class will have support for the regular range of integers supported in C/C++, as well as positive and negative infinity. On top of regular integer arithmetic, you must support the following relations. For addition:
- += += 0 += 0 +=
- aZ, a+=+a= a+=+a=
For subtraction:
- = 0 = = = 0
- aZ, a= a=
- aZ, a= a=
For multiplication:
- = = = =
- 0 = 0=0 = 0= 0
- a > 0 Z, a=a= a=a= a=a= a= a=
For division:
- /= 1 /= 1 /= 1 /= 1
- a > 0 Z, a/0 =, a/0 =
- aZ, a/=a/= 0
- a > 0 Z, /a= /a= /a= /a=
The expressions 0 / 0 and/ 0 are left undefined. The>>operator should read in an integer, the string+infinity, or the string-infinityfrom an input stream, and populate the ExtendedIntobject with the read-in value. The<< operator should print either the integer, the string+infinity, or the string-infinityto the output stream. When default constructed, anExtendedIntobject should represent the integer 0. A testing harness is provided for you in1231/a3/q1/a3q1.cc. You are given an array of 5 ExtendedInts, accessed with the charactersa, b, c, d,ore. Your permitted commands are:
- p [a-e]: Prints the value stored in ExtendedInta, b, c, d, oreto stdout.
- r [a-e] value: Sets the value stored in ExtendedInta, b, c, doretovalue.valueshould be an integer, the string+infinity, or the string-infinity.
- [+-/*] [a-e] [a-e]: Add, subtract, multiply, or divide the values stored in the twoExtendedInts, and print the value.
- q: Quits the program
You are encouraged to reada3q1.ccif you have any questions about the testing harness.
(a)Due on Due Date 1:Design a test suite for this program. Submit a file calleda3q1a.zipthat contains the test suite
you designed, calledsuiteq1.txt, along with all the.infiles.
(b)Due on Due Date 2:Write your solution in filesExtendedInt.handExtendedInt.cc. Submit these together
in a file calleda3q1b.zip.
Question 2
(25% DD1 and DD2) In this exercise, you will write a C++ class (implemented as astruct) to control a simple robotic drone exploring some terrain. Your drone starts at coordinates (0,0), facing north. Use the following structure definition for coordinates:
struct Position { int ew, ns; Position( int ew = 0, int ns = 0 ); };
The east-west direction is the first component of a position, and the north-south direction is the second. YourDroneclass must be properly initialized via a constructor, and must provide the following methods:
Method Description
void forward() Move the drone one unit forward.
void backward() Moves the drone one unit backward.
void left() Turns the drone 90 degrees to the left, while remaining in the same
location.
void right() Turns the drone 90 degrees to the right, while remaining in the same
location.
Position current() Returns the current position of the drone.
int totalDistance() Returns the total units of distance travelled by the drone.
int manhattanDistance() Returns the Manhattan distance between the current position and the
origin where the Manhattan distance defined as the absolute north-south
displacement plus the absolute east-west displacement.
bool repeated() Returns true if the current position is one that the drone has previously
visited.
For simplicity, you may assume that the drone will never visit more than 50 positions before running out of fuel or otherwise breaking down. Implement the specified operations for theDrone. (Some starter code has been provided for you in the filedrone.h, along with a sample executable.) You may not change the contents ofdrone.hother than by adding your instance variables and comments i.e. the interface must stay exactly the same. The test harnessa3q2.ccis provided with which you may interact with your drone for testing purposes. The test harness is not robust and you are not to devise tests for it, just for theDroneclass. Do not change this file.
(a)Due on Due Date 1:Design the test suitesuiteq2.txtfor this program and zip the suite intoa3q2a.zip.
(b)Due on Due Date 2:Implement this in C++ and place the filesMakefile,a3q2.cc,drone.handdrone.ccin
the zip file,a3q2b.zip. Call your executablea3q2.
Question 3
(50% DD1 and DD2) A markup language is a system for annotating documents. In markup languages such as html and XML, annotations consist of tags, identified by the starting and end characters< >. Tags can further contain attributes or children tags/text and are closed by</ >. For example, in the following excerpt,document,body, andtextare tags. Thetexttag contains attributesidandvalue. Currently the first text tag has"t1"as theid, and"CS246"as thevalueattribute.
In this exercise, you will write a class to manage tags in a document. Our markup language is loosely inspired by HTML and XML, but it is quite simplified. Our tags can be of any type (such asdocument,body, ortext) and can optionally contain the attributesidandvalue(and no other). Additionally, they can contain any number of children tags. There is always a top-leveldocumenttag. The example document above is a valid document in our markup language and also an example of the formatting that should be used by the program when printing the tags. We have provided the declaration of theTagclass in the filetag.hin thea3/q3directory.You may not change the contents oftag.hother than by adding private instance members and comments, i.e., the public interface must stay exactly the same.
class Tag { std::string type; // the type of the tag std::string id; // the id attribute of the tag std::string value; // the value attribute of the tag Tag *parent; // pointer to the parent tag (or nullptr for the document // tag, which does not have a parent) Tag **children; // array of children tag pointers int childrenLength; // current number of children tags int childrenCapacity; // current capacity of the children array // public methods; please check tag.h for details };
The diagram in Figure 1 shows an overview of the tags composing the example document and the pointers to their respec- tive parent and children tags. Your job is to implement all the methods of theTagandTagIteratorclasses in the filetag.cc. Please read carefully the documentation for each method intag.h. The comments explain what they must do.
Implementation details:
- Thechildrenarray contains the children Tags. When a Tag is created, initializechildrentonullptr. Initialize childrenLengthandchildrenCapacityto zero. When the first child Tag is added, create an array in the heap with size one. You must keep track of the current capacity (size) of the array inchildrenCapacityand the current number of children tags (positions used in the array) inchildrenLength. You will increase the capacity of the array using a doubling strategy. Each time a new child Tag is added, if the length of the array is already equal to the capacity, you must double the current capacity of the array (i.e., create a new array with double the capacity of the current one and move all the children Tags over to the new array). When children tags are removed from the array, you do not need to reduce the capacity.
Figure 1:
- Because we test your submission by comparing the output of your program with the expected output of the correct program, you will lose marks if yourTag::print()does not precisely match the output of our implementation. You can use the document provided at the start of the question as an example of what the formatted output should look like. Note that each tag start markup and tag end markup should be printed on a new line. Children tags must be indented with two spaces per level. We are also providing you a sample executable that produces the correct output. Please test the provided executable for every potential type of output and ensure that your solutions matches the sample output precisely (use an utility likediffto ensure that the outputs are identical; dont rely only on a visual inspection). The runSuitescript that you wrote as part of A1 can help you do that.
- We also provided the declaration ofTagIterator, which is a implementation of theIteratordesign pattern for thechildrenarray. You must also implement the methods of this class. Used together with the methods Tag::begin()andTag::end(), it can be used by a client to iterate through all the children of a tag.
- You can implement theoperator<<for Tags (also declared intag.h) by just calling the tagsprintmethod.
- Your program must not leak any memory.
A test harness is provided ina3q3.cc(which usescontroller.[h|cc]). You can use it to test yourTagclass.The test harness is not robust and you are not to devise tests for it, just for theTagclass. Do not change these files.We have also provided a sample executable (a3q3Sample) that you can use for tests. Both files can be found in the directorya3/q3. The test harness supports the following commands. Please read the contents ofa3q3.ccandcontroller.[h|cc] if you need more details about them. Note that the harness keeps track of the current tag at any moment and most of the commands operate on it. Also, note that the harness automatically creates a top-leveldocumenttag. You can set its attributes and add children to it.However, you cannot delete, cut, or copy thedocumenttag and you should not create new tags of this type.
Command Description
print Prints the current tag to the standard output.
addtype Creates a new tag of thetypespecified by the argument, adds it as a child of the current tag, and
makes it the new current tag. Note thattypecannot be"document".
delete Deletes the current tag, removes it from its parent, and makes its parent the new current tag.
parent Sets the parent of the current tag as the new current tag.
up Sets the topmost tag (<document>) as the current tag.
idval Sets theidattribute of the current tag toval. Note thatvalcannot contain whitespaces.
valueval Sets thevalueattribute of the current tag toval. Note thatvalcannot contain whitespaces.
findtype Finds the first tag that is a child of the current tag and has the specified type. If such a tag is
found, it is set as the current tag. Otherwise, nothing happens.
findIdid Finds the first tag that is a child of the current tag and has the specified id. If such a tag is found,
it is set as the current tag. Otherwise, nothing happens.
list Lists the type of the direct children of the current tag.
cut Removes the current tag from its parent and moves it to the programs internal clipboard. Sets its
parent tag as the current tag.
copy Copies the current tag to the programs internal clipboard.
paste Adds a new child to the current tag, whose contents are a copy of the tag currently in the clipboard,
and updates the pointers of the child and current tag accordingly.
quit Quits the program.
Submission:
(a)Due on Due Date 1:Design the test suitesuiteq3.txtfor this program and zip the suite intoa3q3a.zip.
(b)Due on Due Date 2:Implement this in C++ and place the filestag.handtag.ccin the zip file,a3q3b.zip. We will provide theMakefile,a3q3.cc, andcontroller.[h|cc]to test your submission.