#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();
}
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.
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.
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.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. int getScore(Person p)
returns the score for a given player in a given 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.
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:
boolean isEmpty()
that returns true if the list is empty and false otherwiseint size()
returns the number of elementsvoid add(T item)
that adds an item to the list. If item
is null, a NullPointerException is thrown; repeated elements are allowedT 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.
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.