Exam 1 Solutions | Data Structures - Fall 2005 | CS 225, Study notes of Data Structures and Algorithms

Material Type: Notes; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2005;

Typology: Study notes

Pre 2010

Uploaded on 03/16/2009

koofers-user-jzv-1
koofers-user-jzv-1 🇺🇸

9 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of Illinois at Urbana-Champaign
Department of Computer Science
First Examination
CS 225 Data Structures and Software Principles
Fall 2005
7:00pm 8:15pm Monday, October 3
Name: SOLUTIONS
NetID:
Lab Section (Day/Time):
This is a closed book and closed notes exam. No electronic aids are allowed, either.
You should have 7 sheets total (the cover sheet, plus numbered pages 1-13). The last sheet
is scratch paper; you may detach it while taking the exam, but must turn it in with the
exam when you leave. The back of the second-to-last page contains some class and function
declarations you have seen before; you can use this sheet as reference while taking the exam.
Unless otherwise stated in a problem, assume the best possible design of a particular imple-
mentation is being used.
Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any
compiler error is an exam typo (though hopefully there are not any typos), and (2) assume
you are NOT allowed to write any helper methods to help solve the problem, nor are you
allowed to use additional arrays, lists, or other collection data structures unless we have said
you can.
Problem Points Score Grader
1 15
2 25
3 15
4 20
5 15
Total 90
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Exam 1 Solutions | Data Structures - Fall 2005 | CS 225 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

University of Illinois at Urbana-Champaign

Department of Computer Science

First Examination

CS 225 Data Structures and Software Principles

Fall 2005

7:00pm – 8:15pm Monday, October 3

Name: SOLUTIONS

NetID:

Lab Section (Day/Time):

  • This is a closed book and closed notes exam. No electronic aids are allowed, either.
  • You should have 7 sheets total (the cover sheet, plus numbered pages 1-13). The last sheet is scratch paper; you may detach it while taking the exam, but must turn it in with the exam when you leave. The back of the second-to-last page contains some class and function declarations you have seen before; you can use this sheet as reference while taking the exam.
  • Unless otherwise stated in a problem, assume the best possible design of a particular imple- mentation is being used.
  • Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any compiler error is an exam typo (though hopefully there are not any typos), and (2) assume you are NOT allowed to write any helper methods to help solve the problem, nor are you allowed to use additional arrays, lists, or other collection data structures unless we have said you can.

Problem Points Score Grader

Total 90

  1. [Basic Syntax Issues – 15 points].

class Triple { public:

Triple(); // initializes object to (0, 0, 0)

Triple(int theX, int theY, int theZ); // initializes object to // (theX, theY, theZ) int getX(); // returns x value

int getY(); // returns y value

int getZ(); // returns z value

void initVals(int theX, int theY, int theZ); // assigns object to // (theX, theY, theZ) private:

int x; int y; int z; };

On the next page, there are three code snipppets. For each snippet, answer the question above the snippet.

  1. [Dynamic Memory – 25 points].

Consider the following class:

class Values { public: // no-argument constructor Values();

// assignment operator

// ANSWER: Values const & operator=(Values const & origVal);

private: Array<String> firstNames; Array numbers; Array<String> lastNames; String collectionName; // should point to a single String object, // not an array of String objects };

The no-argument constructor of this class should initialize the object so that each array is of size 10 and every pointer points to an object of the appropriate type, rather than NULL or garbage. Non-pointer values can be left as garbage. We don’t care about the indices of the arrays as long as there are 10 cells in each array. Write the declaration for the assignment operator above, and write the definition for both the no-argument constructor and the assignment operator (below and/or on the next page), as they would appear in the .cpp file. (Do not worry about #include statements.)

(Dynamic Memory, continued)

Values::Values() { firstNames = new Array<String>(1, 10); for (int i = 1; i <= 10; ++i) (firstNames)[i] = new String(); numbers.setBounds(1, 10); lastNames.setBounds(1, 10); for (int i = 1; i <= 10; ++i) lastNames = new String(); collectionName = new String(); }

Values const & Values::operator=(Values const & origVal) { if (this != &origVal) { for (int i = firstNames->lower(); i <= firstNames->upper(); ++i) delete (*firstNames)[i]; delete firstNames;

for (int i = lastNames.lower(); i <= lastNames.upper(); ++i) delete lastNames[i];

delete collectionName;

firstNames = new Array<String>(origVal.firstNames->lower(), origVal.firstNames->upper()); for (int i = firstNames->lower(); i <= firstNames->upper(); ++i) (firstNames)[i] = new String((((origVal.firstNames))[i]));

numbers = origVal.numbers; lastNames.setBounds(origVal.lastNames.lower(), origVal.lastNames.upper()); for (int i = lastNames.lower(); i <= lastNames.upper(); ++i) lastNames[i] = new String(*((origVal.lastNames)[i]));

collectionName = new String(*(origVal.collectionName)); } return *this; }

  1. [Generic Programming – 20 points].

(a) Convert the following class declaration, into the declaration of a template class Pair which holds both an int, and some type associated with that int (not necessarily a String, as below, but any type). (You do not have to reproduce the comments.)

class NamedInt { public: // initializes member variables to parameter values NamedInt(int theValue, String theName);

// returns value of integer member variable int getValue();

// returns value of String member variable String getName();

private: int value; String name; };

template class Pair { public: Pair(int theValue, Etype theItem);

int getValue();

Etype getItem();

private: int value; Etype item; };

(b) Write the copy constructor definition for this template class, as if the copy constructor had been declared in the class declaration and you were writing part of the template definition file.

template Pair::Pair(Pair const & origVal) { value = origVal.value; item = origVal.item; }

  1. [Inheritance – 15 points].

Consider the following class:

class Bottle { public: // parameter capacity is in liters, and becomes capacity of bottle; // bottle will also be completely full (i.e. contents equals capacity) Bottle(double theCapacity);

virtual ~Bottle() {}

// fills bottle back to completely full void refill();

// parameter sipSize is some amount of liquid (in liters). If // sipSize <= contents, reduce contents by parameter amount // and return true; else return false. boolean drink(double sipSize);

double getCapacity(); // returns capacity of bottle (in liters) double getContents(); // returns amount of liquid in bottle (in liters)

private: double capacity; // number of liters the bottle can hold double contents; // number of liters the bottle currently holds };

You want to write a class SpigotBottle that is a derived class of Bottle. A SpigotBottle has a button-activated valve in it; when you press the button, a pre-set amount of liquid flows out of the bottle.

  • There is one additional member variable, a double to hold the amount of liquid that flows through a valve in the bottle after one press of the button.
  • There is a constructor that has two parameters, the first of which holds the bottle’s capacity (in liters), and the second of which holds the amount of liquid that flows out of the bottle after one button press (again, in liters). Initialize bottle to be full and to otherwise have parameter values.
  • There is a method useSpigot() which has no parameters and returns a double. The method will return the amount of liquid that leaves this bottle after one press of the button. Note that if the amount left in the bottle is less than what would normally be released after one press of the button, then that small amount is all that would leave the bottle. (Meaning, if .04 liters normally leaves the bottle after a press of the button, but there’s only .02 liters left to begin with, then only .02 liters could leave the bottle.) The bottle should also be altered to represent that that much liquid has left the bottle.
  • Remember that the variables in the Bottle class are private.

(Inheritance, continued)

class SpigotBottle : public Bottle { public: SpigotBottle(double capacity, double theFlow); double useSpigot(); private: double valveFlow; };

SpigotBottle::SpigotBottle(double capacity, double theFlow) : Bottle(capacity) { valveFlow = theFlow; }

double SpigotBottle::useSpigot() { double whatIsLeft = getContents(); if (valveFlow <= whatIsLeft) { drink(valveFlow); return valveFlow; } else { drink(whatIsLeft); return whatIsLeft; } }

(scratch paper, page 1)

(scratch paper, page 2)