








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Earls; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Summer 2005;
Typology: Exams
1 / 14
This page cannot be seen from the preview
Don't miss anything!









3:00pm – 4:15pm Tuesday, July 5
Consider the following code, which contains a class representing an exam for a course of at most 20 students. The code below would appear in the header file for this class, which we’ll assume is named examdata.h.
#ifndef EXAMDATA_225_H #define EXAMDATA_225_H
#include "string.h" // we will assume the string.h file here // is the same one you’ve seen on the MPs
class ExamData { public:
// Assigns the internal String to be equal to the // parameter String, and sets this collection of // exam scores to be of size zero ExamData(String theName);
// If there are less than 20 exam scores so far, // add the parameter score in the next available cell, // (thus incrementing the number of scores by 1) and // return true. Otherwise, do not change the member variables // at all, and return false. bool addValue(double examScore);
private:
String examName; int numScoresSoFar; double theValues[20]; };
#endif
On the next page, write the examdata.cpp file for this class. The comments on the two functions above indicate what needs to be done by those two functions. You do not need to have any comments in your answer.
Consider the following class:
class Circle { public:
// initializes member variables to parameter values Circle(double theRadius, double theX, double theY);
// calculates and returns area of circle double area();
// moves the center of the circle by the first parameter’s // value in the positive X direction and by the second // parameter’s value in the positive Y direction. void translate(int xMove, int yMove);
private:
double radius; // radius of the circle double xCenter; // x-coordinate of the center of the circle double yCenter; // y-coordinate of the center of the circle };
(a) Consider the following code: Circle* cPtr; Given the variable cPtr above, write additional code that will (1) create a local Circle object with radius 3.1, x-coordinate 5.6, and y-coordinate 2.3, (2) assign the pointer above to point to that Circle object, and (3) using the pointer, obtain the area of the circle and print it out.
(b) Imagine you have the following function:
double Foo(Circle const & theParam) { Circle c2(theParam); double theArea = theParam.area(); return (theArea + c2.area()); }
The above function will not compile with the given Circle class. Explain what change you would need to make to the Circle class so that the above code and the Circle class would both compile correctly.
(The Big Three, continued)
(a) You want to write a class whose instances are function objects. The class should be named XCharsInRange, and the operator() for the class has a return value of type bool and has three parameters. The first two parameters are of type int, and the third parameter is of type String. You can assume the first parameter is less than or equal to the second parameter. The function will return true if, in the parameter String, the total number of times the ’X’ and ’x’ characters appear, is between the two integers inclusive, return true; otherwise, return false. For example, if the first two arguments to the function were 5 and 7 , then the function returns true if the ’X’ and ’x’ characters appear between 5 and 7 times, inclusive. in the parameter String, total. If the String had two ’X’ characters and five ’x’ characters, for example, then the total is seven and so you would return true. (It is okay to write the definition for this class right into the class declaration itself, i.e. you don’t need to divide things up into a .h and .cpp, though you can if you want to.)
Consider the following class:
class Window { public:
// initializes member variables to be equal to parameter values Window(String theTitle, int theWidth, int theHeight);
virtual ~Window() { }
// prints out // This window has title T, width W, and height H // where T, W, and H are the values of the member variables virtual void print();
String getTitle(); // returns title of window int getWidth(); // returns width of window int getHeight(); // returns height of window
private:
String title; int width, height; };
You want to write a class DialogWindow that is a derived class of Window, and whose speci- fication is as follows:
(Inheritance, continued)
(scratch paper, page 1)
(scratch paper, page 2)