View the step-by-step solution to:

ICS / CSE 45C Programming in C++ - Winter 2015 Homework 4 Deadline: 2/27/2015 Similar to last homework, each problem should be separated into its own...

I'm a beginner to computer C++ programming and need help with an assignment, specifically starting the first problem.

ICS / CSE 45C Programming in C++ – Winter 2015 Homework 4 Deadline: 2/27/2015 Similar to last homework, each problem should be separated into its own function and called within main(). Each problem should have its own .h / .cpp file associated with it and include any components needed. Please submit all of your .cpp / .h files when turning in this homework. Problem 1: a.) A list is a collection of an unordered sequence of elements. C++ has a std::list class that is an Abstract Data Type encapsulating details on how a list is managed. For this problem, you will write your own list implementation. A few constraints on your implementation: § Implement this class using templates where this list can store any type. § All return types will be actual objects and not references to objects. § You may assume that your list will not exceed 10 elements. § You will use an array representing the actual list. Since we know there will be at most 10 elements, you can statically allocate this array. § You will want to keep track of the number of elements in the list using a class variable named size. Your list class implementation should support the following functions: § at(int i) – returns the element at the index i. Throws an exception if there is no element at index i. § empty() – returns true if the list is empty, false otherwise. § first() – returns the element at index 0. Throws an exception if the list is empty. § last() – returns the element at index size – 1. Throws an exception if the list is empty. § insert(T item) – inserts the item at the end of the list. Throws an exception if the list is full. § remove(int i) – removes the element at index i. “Removing” doesn’t mean erasing the contents from memory in this case. You will need to shift all elements inserted after the i th element down 1 in the array in order to prevent any “holes” in the list. Throws an exception if there is no element at index i. Since this problem requires you to throw exceptions, I recommend defining your own types for each possible exception and using those types when throwing / catching exceptions. These can be defined in the same .h file as your List class. You may consider using the following: InvalidIndexException EmptyListException FullListException b.) Write test cases in your Problem 1() function that illustrate your list class works as expected. You should test a normal case and the edge case(s) for each function. These test cases will also allow you to practice wrapping function calls in try / catch blocks and handling them appropriately.
Background image of page 1
Problem 2 : a.) Organize and write several classes representing zoo animals. Each animal should have its own class representation that inherits from a base class named “ Animal ”. All animals have the following common characteristics, which should be defined in an Animal base class (with the appropriate accessor / mutator / constructor): string name – string representing the name of an animal (for example: “fluffy”). int weight – int representing the weight of the animal in lbs. int numOfLegs – int representing the number of legs the animal has. The different types of animals your zoo can contain are: Dog – Contains an additional field int numOfClaws. Fish – Contains an additional field int numOfScales . Chicken – Contains an additional field int numOfBeaks . Spider – Contains an additional field bool isVenomous . Each of these classes should have their own accessor / mutator functions for their additional field as well as its own constructor. These animals were chosen for their variety more than anything else … or else this would be a really boring zoo to visit! To practice polymorphism (late binding), in your Animal class, define a pure virtual function string getSound() that returns a string that the particular Animal type makes. Also, you should define another pure virtual function string getDescription() that returns a string with all of its attributes including the attributes from its base class. b.) Using your list solution from the previous problem, in your Problem2() function, create an Animal list (i.e. your zoo) and create/insert various animals into this list (at least one animal of each type). Traverse this list and print out the Animal attributes and sounds that each animal makes using your getDescription() function. For example, if you insert a Dog, you can print its attributes inherited from Animal (name, weight, numOfLegs), it’s own distinct attribute (numOfClaws), and the sound it makes (which you define when writing your getSound() function).
Background image of page 2
Sign up to view the entire interaction

Top Answer

View the full answer

#ifndef List_H
#define List_H
#include <stdexcept>
using namespace std;
template<typename T>
class Node
T element;
Node<T>* next;
next = NULL;
} }; Node(T...

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.


Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online