CSci 4651 Problem set 3 (55 points).

Due Monday, May 2nd at 11:59pm (by e-mail)

Problem 1: C++ objects (12 points)

You are given the following class hierarchy:

#include <iostream>
using namespace std;

class A {
protected:
  int x;
public:
  A() { x = 0;}
  void increment() { x++; }
  virtual void decrement() { x--; }
  virtual void print() {cout << "x = " << x << endl;}
};

class B : public A {
protected:
  int y;
public:
  B() {x = 0; y = 0;}
  void increment() { x = x + 2; }
  virtual void decrement() { x = x - 2; }
  virtual void print() {cout << "x = " << x << " y = " << y << endl;}
};

In the following two code fragments please clearly explain what will be contained in the stack frame for main and each of the two methods. Explain exactly what method will be called and why; also explain the output of each print statement. Diagrams may be helpful.

Fragment 1

  void method1(A a1, A a2) {
  a1.increment();
  a2.decrement();
  a1.print();
  a2.print();
}

void method2(A* a1, A* a2) {
  a1 -> increment();
  a2 -> decrement();
  a1 -> print();
  a2 -> print();
}

int main() {
  B b1;
  B b2;

  method1(b1, b2);
  b1.print();
  b2.print();

  method2(&b1, &b2);
  b1.print();
  b2.print();

}

Fragment 2

  void swap1(A a1, A a2) {
  A temp = a1;
  a1 = a2;
  a2 = temp;
  a1.print();
  a2.print();
}

void swap2(A *a1, A *a2) {
  A temp = *a1;
  *a1 = *a2;
  *a2 = temp;
  a1 -> print();
  a2 -> print();
}


int main() {
  B *bptr1 = new B();
  bptr1 -> increment();
  bptr1 -> print();
  B *bptr2 = new B();
  bptr2 -> decrement();
  bptr2 -> print();

  swap1(*bptr1, *bptr2);
  bptr1 -> print();
  bptr2 -> print();

  swap2(bptr1, bptr2);
  bptr1 -> print();
  bptr2 -> print();

}
  

Problem 2: Java subtyping (12 points)

Consider this example. In main there are 6 calls to the method combine that's defined in the Shape class and in Circle and Square.

Question 1 Question 2

Problem 3: developing class hierarchy in C++ and Java (15 points)

Suppose you are developing a type hierarchy for representing interactive games. All games belong to a type Game that has a method playRound. Within this type, you need to represent the following groups of subtypes. A game is classified by all of these categories. For isntance, a game might be MultiPlayer, Card and Board ScoreBased Children's game.

Your goal is to develop the hierachy of types for this problem in C++ and Java. You don't need to implement anything, just say what kinds of inheritance relations your types have, what kind of type it is (say, a purely virtual class in C++ or an interface in Java). You also need to say whether any of the listed methods (including playRound, etc.) are virtual (in C++) and are overwritten in subclasses.

Submit you hierarchy (diagrams help) and method signatures. Explain the choices that you were considering and the reasons why you ended up with the given hierarchy in each of the languages.

Problem 4: ordered linked list and lambdas (16 points)

Use Java 8 for this problem.

Implement a generic ordered linked list in Java. Specifically, the list must accept any objects that are comparable to each other and use their compareTo method to position the elements in order from smallest to largest.
Let T be the name of the type parameter, then the methods you need to implement are:

After you have implemented the list, write a method map that takes a function (defines as a "lambda" expression), goes through the list, and applies the function to every item (note that the list is mutable: the items are changed within the list, instead of creating a new list). Note that the function has to return the same type of elements as the one given.
Try the function on a list of strings, use a function that converts a string into its all-lowercase version. Then use it on a list of Integer elements that adds 1 to every element. Add at least one more test case.
Next write a method filter that takes a predicate and removes the elements of the list that do not satisfy the predicate. Write at least three test cases with different predicates. Make sure to test it on the case when the resulting list is empty.

How to submit

Submit your file(s) to me by e-mail. The subject must be Problem Set N, where N is the problem set number.
Handwritten problems can be scanned and attached (good quality scanning, please!) or submitted in class or in my office in person. If submitting handwritten work, please clearly write your name and problem set number on it.


CSci 4651 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.