## 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
• Guess which method will be call in each of the 6 calls
• Run the code
• Revisit your predictions. Explain the results. Feel free to run more tests (modify the classes and/or the calls, if you find it helpful).
• After exploring the calls, clearly explain why the methods are called as they are. Include all the relevant testing
Question 2
• Change the original code to try to assign the results of these methods to various variables
• State what assignments are allowed as is, which ones require casting, and which ones do not compile (if any).
• Explain why these decisions make sense in Java.

#### 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.

• SinglePlayer and MultiPlayer games. When a round of a single-player game is played, a `player` field of type `Person` is accessed. When a round of a multi-player game is played, a field `players` is accessed, which is an array of `Person`s.
• Card games with methods `dealCards` and `getCard`, Board games with a method `updateBoard`, and Dice games with a method `rollDice` (this would also roll a single die for games that have only one). There are games that belong to more than one (possibly all three) categories and there are games that are in neither category.
• ScoreBased games: some, but not all, games have scores for each player. A method `int getScore(Person p)` returns the score for a given player in a given game.
• Childrens games, Grownup games, and MixedAge games. These categories are non-overlapping. There are no specific methods in these types, but there may be methods in the system that would be restricted to only one of these types.

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:

• A constructor that creates an empty list
• `boolean isEmpty()` that returns true if the list is empty and false otherwise
• `int size()` returns the number of elements
• `void add(T item)` that adds an item to the list. If `item` is null, a NullPointerException is thrown; repeated elements are allowed
• `T get(int i)` that returns the element at the index i. If i is negative or larger than size()-1, null is returned.

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.

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.