Solved–Lab 4 Templates– Solution

$30.00 $19.00

Guidelines This is an individual lab assignment. You must do the vast majority of the work on your own. It is permissible to consult with classmates to ask general questions about the assignment, to help discover and fix specific bugs, and to talk about high level approaches in general terms. It is not permissible to…

You’ll get a: . zip file solution

 

 

Description

5/5 – (2 votes)

Guidelines

This is an individual lab assignment. You must do the vast majority of the work on your own. It is permissible to consult with classmates to ask general questions about the assignment, to help discover and fix specific bugs, and to talk about high level approaches in general terms. It is not permissible to give or receive answers or solution details from fellow students.

You may research online for additional resources; however, you may not use code that was written specifically *to* solve the problem you have been given, and you may not have anyone else help you write the code or solve the problem. You may use code snippets found online, providing that they are appropriately and clearly cited, within your submitted code.

*By submitting this assignment, you agree that you have followed the above guidelines regarding collaboration and research.*

***

The goal of Lab 4 is to get you to Templatize some of your code from program1, and use your expanding library for another ADT. You are going to expand and refine your existing library by templatizing your Vector using C++ templates. Once you have successfully templated your code, you will use your fancy new template vector library to implement a Queue data structure.

:bulb: You do not have to keep your Vector the same as the project. You can start fresh if you like.

(Parts A must be completed in lab)

Part A: Preparing for Exploration.

Now that you have created Data Structures to simulate the universe with planets and stars, you will next need to explore your universe. We want to prepare a probe that we will send out into our universe to find planets with the possibility of life. Since we do not know what we will find in our universe, we should be prepared for anything.

You are going to templatize your Vector from Program 1 so it can work with many types without writing multiple classes. As you are templatizing your vector, be sure to remember that all of your code must be moved into a header file. You will need to eliminate the .cpp file completely.

Your Vector will also need the following public interface:

* `Vector(int size)`

* an initial starting size of your vector

* `Vector(const Vector<type> & v)`

* A copy constructor

* `<type>& operator [](std::size_t index)`

* Overload the [] operator.

* I do not test out of bounds behavior, so you can handle that however you like

* `unsigned int size()`

* returns the size of your vector

* `void clear()`

* deletes your vector data and sets the size to 0

* :warning: This is not a destructor. It does not delete the vector itself. It just reduces the size of the internal array to 0. Depending on how you implement your vector, you may also need a destructor.

* You may add any additional methods you require. This means you can use your existing methods to implement the above interface if you want to.

:warning: Every instance of `Planet *` from your previous code needs to become a type variable, and no specific calls to Planet methods should remain.

:bulb: Here is a resource that gives a brief overview of templates if you need a syntax reminder

* https://www.tutorialspoint.com/cplusplus/cpp_templates.htm

Part B: Using your Vector Library for a Queue

Once you have completed your templated Vector, you will use it to create Queue template data structure. Your Queue should be in separate file (Queue.h), and should use your Vector as an internal data structure. This means you need to have the following methods:

* `Queue(unsigned int size)`

* A max size for your queue

* Your internal vector should never expand or shrink

* `bool enqueue(<type> data)`

* Adds a data object onto the queue and returns a boolean if successful

* `<type> dequeue()`

* removes a data object from the queue and returns it

* `<type> peek()`

* returns a copy (not a reference) of the data object but does not remove it from the list

* `bool empty()`

* returns `true` if the queue is empty, `false` otherwise.

* `void clear()`

* deletes all items from the queue

:bulb: You may add any additional methods you like. For example, you may need a destructor, depending on your implementation.

Part C : Code Organization and Submission

* Required code organization:

* lab4.cpp (driver code – You must include this file in your submission)

* Vector.h

* Queue.h

* makefile

* executable should be called: lab4

* You must have the following targets in your makefile:

* `all` – only compiles your source code using separate compilation for each .cpp file

* `clean` – removes all object files and binary executables

* `run` – compiles if necessary and runs your executable

* `memcheck` – compiles your source if necessary, then runs your executable with valgrind

Below is just a reminder of the commands you should use to submit your code. If you cannot remember the exact process, please review lab 1.

_These commands all presume that your current working directory is within the directory tracked by `git`._

You will need to do the following when your submission is ready for grading.

“`shell

git commit -am “final commit”

git push

“`

To complete your submission, you must copy and paste the commit hash into MyCourses. Go to MyCourses, select CS240, and then assignments. Select Lab 4, and where it says text submission, paste your commit hash. You can get your latest commit hash with the following command:

“`shell

git rev-parse HEAD

“`

:warning: Remember, you __MUST__ make a submission on mycourses before the deadline to be considered on time.