Final Exam Review for CS 2390 Winter '96

  1. You are working on a project using Smalltalk where you need linked lists, but you've decided that you don't like the implementation of linked lists provided in the Smalltalk image. You decide to implement your own class MyLinkedList. You need the following functions in your implementation:

    (a) Write the class definition for MyLinkedList

    Object subclass: #MyLinkedList
    	instanceVariableNames: YOU FILL IN HERE
    	classVariableNames: 
    	poolDictionaries: 
    	category: 'My-Data-Structures'
    
    (b) Write the method definitions for each of the desired functions.

    Discussion

  2. You are building a model in which you will measure how much electricity is used in a typical home on a typical day and at specific times during the day. You have decided to implement a Coad & Nicola-style simulation (e.g., like the traffic flow or vending machine models). Your customer is most concerned with modeling the following appliances: Analyze this problem and come up with a model for designing and programming this model. (You can imagine using a script and/or a user-interface to run the model.) Use Coad & Nicola's graphical notation to describe your model. You are encouraged to explain your work with text as well, including what your assumptions are about this problem.

    A key requirement for this problem is maximal reuse. Try to define classes that you can reuse either through specialization-generalization or whole-part.

    Discussion

  3. You are building a simulation of a hospital including the following components: (a) Of the following questions, which call for a discrete simulation and which call for a continuous simulation? And WHY? (b) Define two other question for which you'll need a discrete simulation.

    Discussion

  4. Below is the definition in C++ of the class Count.
    template
    class Count { 
    public:
    //Constructors/destructors
    	Count() { } 
    	virtual ~Count() { } 
    //Implementors
    	virtual void decrement() =0;
    	virtual void increment() =0;
    	void reset() {  value = reset_value; } 
    //Accessors
    	Count_Type get_value() {  return value; } 
    	void set_value(Count_Type new_value) {  value = new_value; } 
    	Count_Type get_reset_value() {  return reset_value; } 
    	void set_reset_value(Count_Type new_value) {  value = new_value; } 
    
    protected:
    //Data members
    	Count_Type value;
    	Count_Type reset_value;} ;
    

    (a) In terms of inheritance, does this definition differ from the definition of the class Count in Smalltalk? Can subclasses of Count in Smalltalk access anything more or less than derived class of Count in C++? Can external classes that use Count in Smalltalk access anything more or less than external classes that use Count in C++?

    (b) Why are the data members declared protected in the class above? Why not declare them private?

    (c) Why are ~Count(), increment(), and decrement() declared virtual? What does that mean?

    (d) Suppose that we donÕt want to allow the data members of Count to be arbitrarily manipulated from outside of this class or classes derived from Count. What would you change in the above definition? Why?

    Discussion

  5. Please answer the following questions:

    (a) Describe the relationship between two classes using generalization-specialization inheritance. (For example, what can you say about how IntegerCount and Count are similar/dissimilar given that IntegerCount is a subclass of Count?)

    (b) Describe the relationship between two classes that are related by implementation inheritance.

    (c) What feature(s) of C++ enable use of implementation inheritance? Explain how.

    (d) Since implementation inheritance is not supported in Smalltalk, what can a programmer do to achieve much the same effect?

    Discussion

  6. What is a virtual function in C++? Why would you want to declare a function virtual?

    Discussion

  7. Below is a class definition for C++.
    class Time {
    public:
    	Time(int = 0, int = 0, int = 0); // constructor
    	// set functions
    	void setTime(int, int, int); 	//set hour, min. sec.
    	void setHour(int);		// set hour
    	void setMinute(int);		// set minute
    	void setSecond(int);		// set second
    	// get functions
    	int getHour();		//return hour
    	int getMinute();		//return minute
    	int getSecond();		//return second
    	
    	void printMilitary();	//output military time
    	void printStandard();	//output standard time
    private:
    	int hour;		// 0 - 23
    	int minute;		// 0 - 59
    	int second;		// 0 - 59
    };
    

    A. Why should hour, minute, and second variables be private? Why not just make them public and do away with the get and set functions?

    B. On the back of this page, define a class DateAndTime, derived from class Time which includes a day (1-31), a month (1-12), and a year (0-9999). Think about what public and private parts are needed in this new class.

    Discussion

  8. Below are two partial class definitions from C++ from an employee database system which will used mostly by payroll, but may be used by health care and other departments later:
    class employee {
      public:
    	employee(char *, char *, float);
    	void show_employee(void);
      protected:
    //LINES DELETED HERE - A
    
      private:
    //LINES DELETED HERE - B
    
    };
    
    class manager : public employee {
      public:
    	manager(char *, char *, float, char*, float, int);
    	void show_manager(void);
      protected:
    //LINES DELETED HERE - C
    
      private:
    //LINES DELETED HERE - D
    
    };
    
    Here are some facts about employees and managers in the company being modeled: What declarations go into the spots A, B, C, and D?

    Discussion

  9. Below is a C++ class definition.
    class book {
    public:
    	book(char *, char *, int);
    	void show_book(void);
    private:
    	char title[64];
    	char author[64];
    	int pages;
    };
    
    book::book(char *title, char *author, int pages)
    {
    	strcpy(book::title,title);
    	strcpy(book::author,author);
    	book::pages = pages;
    }
    
    void book:show_book(void)
    {
    	cout << "Title: " << title << endl;
    	cout << "Author: " << author << endl;
    	cout << "Pages: " << pages << endl;
    }
    

    a) Explain how you would implement this same class in Smalltalk. Tell me what instance variables the class would have, how you would initializing new objects in Smalltalk, and how you would provide the functionality of show_book (WRITE THE METHOD showBook).

    b) In terms of information hiding, how would your Smalltalk class differ from the C++ class?

    Discussion

  10. You're building a simulation of computer use at a cluster on campus. The cluster has 25 Apple Macintosh computers and 15 Pentium computers. Your job is to inform your boss how many students are waiting in line Š maximum and average.. (Not that they might buy more Š just so that they know how many chairs to put out.)

    a) Using the Simulation Package discussed in class, what would you model as the Simulation? As SimulationObjects? As Resources?

    b) What statistics would you record and at what events (you donÕt have to get the names right, but you should be able to describe the significant events in the simulation)?

    Discussion

  11. Short answer:

    (a) What is the difference between class methods and instance methods in Smalltalk?

    (b) If you're going to keep track of a roster of students in Smalltalk (e.g., names, student IDs, and grades -- in order sorted by last names), what kind of data structure(s) would you use? Justify your decision.

    Discussion