COMP 2150 Final Examination
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
COMP 2150
Final Examination
Fill-in-the-Blank
Fill in the most appropriate word in the space provided.
1. In C++, a subclass method can hide (or shadow) the implementation of a superclass method if the method is not in the superclass.
2. In C++, a(n) declaration of a class lets you declare pointers to that class, without having to define the entire class (yet).
Short Answer
Answer each of these questions in the space provided (20 marks total).
3. (4 marks) Given the Java class below:
class StringArray {
private String[] data;
public StringArray() {
data = new String[0];
}
public void add(String newString ) {
// replace data with a array that’s one larger , containing
// the old data and newString (code not shown)
}
}
(a) Write a copy constructor for StringArray that creates an appropriately deep copy.
(b) Write one line of Java code that uses the copy constructor to copy arr.
StringArray arr = new StringArray();
// stuff gets put in arr
StringArray deepCopy;
// make deepCopy a deep copy of arr using the copy constructor:
4. (2 marks) Given the class hierarchy below, indicate for each part (a)– (d) whether or not the class will compile without error. Just answer “yes” or “no”: no explanation required.
class Super {
public :
void f() { … }
} ;
class ProtSub : protected Super {
public :
void g () { … }
} ;
class PrivSub : private Super {
public :
void h() { … }
};
(b) class B : public ProtSub {
public :
void test() {
Super *s = new ProtSub;
}
} ;
Does it compile?
(d) class D : public PrivSub {
public :
void test() {
h();
}
};
Does it compile?
5. ( 1 mark) What is one thing that you can do with an inner and outer class that you cannot do with two separate (two outer) classes.
6. (2 marks) Give a specific example of one situation where you would want to use an anonymous class. Explain why.
Part B: Programming
• Answer all questions using programs written in the Java language.
• Comments are not necessary, though they may help when your exam is marked.
• Use good programming techniques (e.g. make instance variables private, choose readable variable names, use the appropriate type of loop to solve a problem).
• Write only the code you are asked to write; do not write main programs or import statements.
[12] 8. Write JavaScript Pet and Dog classes with the following instance variables
and behaviours:
• Pet has a name, Dog has a weight;
• Pet has a compare() method that compares by name, Dog has a compare() that compares by weight (ascending or descending order, doesn’t matter);
• Pet has a name getter (using “get” not “getName”) and a do(command) method that prints the command prefixed by the word “Pet”;
• Dog has a weight getter (also using “get”) and a do(action) method that prints the action prefixed by the word “Pet” .
Then implement a PetDog that incorporates both sets of behaviours using the same technique we used for Java “multiple inheritance”: that is, composition plus making the (in this case informal) interface of both Pet and Dog available to a PetDog, plus the following:
• If the do() method is passed the commands “sit” or “stay”, the Pet object will process them; otherwise they will be given to Dog.
• There is a compare() method that will first compare by weight, and if they have equal weight, they will compare by name.
Finally, given a List class with the following methods, construct a new list and add a Pet, a Dog, and a PetDog to it (construct all three):
• constructor(comparator) { this.#comparator = comparator; }
• an insert() method that includes the following code for ordering: if (#comparator.compare(a,b) < 0) { /* a comes before b */ }
2023-06-17