程序代写案例-COMP 2150

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top





page 1 of 14



THE UNIVERSITY OF MANITOBA
FINAL EXAMINATION

COURSE CODE: COMP 2150 DATE: April 25, 2019
TITLE: Object Orientation TIME: 9:00 AM
INSTRUCTORS: Boyer (A01) / Domaratzki (A02) DURATION: 3 HOURS






















Academic Integrity Contract

I understand that cheating is a serious offence.

integrity. Any Student who engages in Academic Misconduct in relation to a University Matter will be
subject to discipline

(2.4 - Student Academic Misconduct Procedure).

Student Signature:______________________________


Instructions
1. Answer all questions on the paper Crowdmark will only show answers written on the page.
2. No aids permitted (e.g., no calculators, no books); turn cell phones off, and keep them out of sight.








page 2 of 14
Section A: Predict the output (10x1 mark)
For each of the following code blocks, write the output on the right-hand side. The examples are from Java (first 3), Ruby
(middle 3) and C++ (last 4). You can assume that any namespace and include statements are present in the code to
ensure they do not cause any errors. You may assume java classes are in appropriately named files. If the code
otherwise does not compile, indicate this and the specific reason why.

public class A {
public static void main (String[] args) {
T a = new U();
System.out.println( a.x() ) ;
}
}
class T {
public String x() { return "b"; }
}
class U extends T {
public String x() { return "a"; }
}

public class B {
public static void main (String[] args) {
T aa = new U();
T bb = new U();
System.out.println( aa.x( bb ) ) ;
}
}
class T {
public String x(T y) { return "b"; }
}
class U extends T {
public String x(U y) { return "a"; }
}

public class C {
public static void main (String[] args) {
Abs c = new conc();
c.something();
}
}

abstract class Abs {
public abstract void nothing();
public abstract void something();
}

class Conc extends Abs {
public void something() {
System.out.println("aa"); }
}


class One
def info

end
end

class Two < One
def info

end
end

x = Two.new
puts(x.instance_of?(One))
puts(x.respond_to?(:info))








page 3 of 14

class A # semicolons used to save space only
def A.new(*args)
if self == A
puts('1')
else
puts('2'); super
end
end
def initialize()
puts('3'); end

end

class B < A
def initialize()
puts('4'); end
end

b = B.new
puts('5')

module M # semicolons used to save space only
def modif (y)
y-1 ; end
def inh
puts 'AA'; end
end

class A
def inh
puts 'BB'; end
end

class B < A
def modif (x)
x+1; end

include M

def do
inh
if modif(3) == 2
puts 'CC'
else
puts 'DD'
end
end
end

b = B.new
b.do

class MyClass {
private:
string info;
public:
MyClass() { info = "S";}
MyClass (const MyClass& src) {
info = src.info + "C"; }
MyClass& operator= ( MyClass& right) {
info = right.info + "A"; }
void getInfo() { cout << info << endl; }
};

int main(void) {
MyClass first;
MyClass second (first);
MyClass third = second;
first = third;
third.getInfo();
first.getInfo();
return 0;
}







page 4 of 14


class A {
protected:
int y;
public:
A () { y = 2; }
virtual int method() { return y;}
};

class B: public A {
public:
B() { y = 3; }
int method() { return y*y;}
};

int main() {
B b;
A a = b;
cout << a.method() << b.method();
return 0;
}

class Gen1 {
public:
Gen1() {}
int f1() { return 1; }
int f2() { return 0; }
};

class Gen2 : private Gen1 {
public:
Gen2() {}
int f1() { return 1+Gen1::f1(); }
};

class Gen3: protected Gen2 {
public:
Gen3() {}
int m1() { return 1+this->f1(); }
};

int main() {
Gen3* g3 = new Gen3();
cout << g3->m1()<< " " << g3->f2() << endl;
}

class Parent {
protected:
string info;
public:


};

class Child: public Parent {
public:
Child()

};

int main() {
Parent* var = new Child();
cout << var->method();
return 0;
}






page 5 of 14
Section B: Short Answer (20 marks)

1. [2 marks] What two components are necessary in an OO language to allow polymorphism?
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
2. [2 marks] Consider the following two C++ class Parent1 and Parent2:
class Parent1 {
public:
string fun1() { return "a"; }
string fun2() { return "b"; }
};
class Parent2 {
public:
string fun1() { return "c"; }
string fun2() { return "d"; }
};
You may assume that all the required includes and namespace declarations have been made. Write a class Child that
inherits from both Parent1 and Parent2 and has a method combo() that returns a string. The method combo() should
or variables in the Child class and must
use the methods defined in Parent1 and Parent2. The methods of Parent1 and Parent2 must not be callable when using a
Child object.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________

3. [2 marks] Suppose that Child is a subclass of Parent in C++ and we have the following line of code:
Parent* obj = new Child();
After this line, what code do we need to write to be allow obj to safely access the methods that are defined as public in
Child but not defined in Parent? Write the line(s) of code only.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________





page 6 of 14
4. [2 marks] Using an example, show the relationships between two classes described by specialization.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
5. [2 marks] Using an example, show the relationships between two classes described by containment.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
6. [2 marks] Describe how - differ in Java.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________

7. [2 marks] In Ruby, show how private and protected are different through an example.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
8. [2 marks] List the four methods that are important under OCCF.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
9. [2 marks] Describe how a class variable (@@var, not a class instance variable) behaves in ruby in a class and its
subclasses.
___________________________________________________________________
___________________________________________________________________





page 7 of 14
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
10. [2 marks] In Ruby, describe what happens when two methods with the same name are defined in the same class. How
does Ruby allow a programmer to deal with this situation?
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
Section C: Ruby Programming (10 marks)

Consider the following iterator class from Assignment 3, written in Java:

public class Iter implements JSONIter {
private Node curr;
public Iter (Node start) {
curr = start;
}
public boolean hasNext() {
return (curr != null);
}

public Value getNext() {
if (curr != null) {
Value li = curr.getItem();
curr = curr.getNext();
return li;
} else {
throw new NoSuchElementException("no more elements");
}
}
}

The Iter class uses a standard Node class, with getNext() and getItem() methods. It assumes the Node class holds values
that satisfy the Value Interface:

public interface Value {
public String toString();
}

Implement the Iter class and an abstract class Value in Ruby. Make sure you use appropriate encapsulation features. You
do not need to write the Node class but may assume it has accessors for the data members (attributes) called item and
next.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________





page 8 of 14
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
Section D: Java Programming (10 marks)

Consider a category of numbers T that have four operations defined on them:

next number in the category. The next operation on an object of type
T takes no parameters and returns an object of the same type T. This operation represents the next object of the
same type according to an ordering of the set T.
divisible sible by another object. The divisible method takes
a parameter p of type T and determines if the object is divisible by p. It returns a boolean.
equals s if two T objects are equal. The equals method takes a single parameter of type of
type T and returns a boolean that gives if the two objects represent the same element of T.
first the unique object of type T
set, for a definition of first that depends on T. The method always returns the same result.

As an example, if T is the category of all integers, then the next operation for an object n would return n+1, and
divisible would be normal integer divisibility (i.e., in pseudocode, 10.divisible(4) would return false and 10.divisible(5)
would return true.) The first method should always return the number 1.

If a category of objects T has these four operations then we call it PrimeTestable.

a) Write a Java interface called PrimeTestable that captures the specifications for the four operations described
above.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________

b) Write a Java class called IntTestable that satisfies the PrimeTestable interface. The class should have a single
field, of type integer. The methods that implement the interface should use the standard definitions for divisibility, next and
equals. As mentioned above, first should always return an IntTestable object with value 1. The constructor for IntTestable
should take a single int parameter.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________





page 9 of 14
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
c) Write a Java class called Prime that has one static method called primeTest. The primeTest method should take a
single parameter of type PrimeTestable and returns a boolean according to whether the number is prime or not. To test
whether a PrimeTestable x is prime or not, look at all the numbers between (but not including) x and check if
any of them divide x.



PrimeTestable t1 = new IntTestable(25);
PrimeTestable t2 = new IntTestable(23);

System.out.println(Prime.primeTest(t1));
System.out.println(Prime.primeTest(t2));

___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________






page 10 of 14
Section E: C++ Programming (15 marks)

In this question, you will write code to support a smart home. Our smart home will have three different types of smart
devices in it:
A single thermostat for the house, which controls the temperature for the house.
Zero or more lightswitches, which have a location and a status (on or off).
A single controller, which can control all of the lightswitches and the thermostat for the house. There is one controller
per house.
The code below is organized into .h and .cpp files for each of the required classes (Thermostat, Lightswitch and
Controller) as well as the abstract superclass of Thermostat and Lightswitch (called Thing).

Additionally, the .h files for the Node and List classes (similar to what was shown in class) and the Iterator class (similar to
the Java code provided in Section C) are provided. The .cpp files for these classes are not provided here, but you may
assume that you have them and that they work. You are only required to provide forward references/includes for these .h
files.

Instructions and notes:
Fill in all underlined areas in all .h files: these are either includes or forward references.
For all Lightswitches and Thermostats:
o Each device needs to be aware of its controller (to be able to call methods from the controller).
For the Lightswitch class:
o When created, all lightswitches are off.
o Each lightswitch has a location, which is typically a room in the house. It is stored as a string.
o Each lightswitch has a toggle function, which switches the light from on to off (or vice versa).
For the Thermostat class:
o When created, a thermostat is set to 20 degrees.
o The method setTemperature sets the temperature on the thermostat.
For the Controller class:
o There are two methods, addLightSwitch and setThermostat, to add devices to the house. addLightswitch adds a
new lightswitch (in addition to the others already added). setThermostat sets the only thermostat in the house
(overwriting the previous one if applicable).
o The method setHome() turns all the lights on and sets the temperature to 20 degrees.
o The method setAway() turns all the lights off and sets the temperature to 16 degrees.

For this page, fill in the underlined areas ONLY. You do not have to provide .cpp files for ANY of these classes.

Node.h List.h Iterator.h
_____________________ // for Thing
________________________ // for Node
________________________// for Thing
_____________________// for Iterator
______________________ // for Node
______________________// for Thing







page 11 of 14
Thing Lightswitch
for Controller class
for Thing class







page 12 of 14
Controller Thermostat
Controller.h
for List
for Thermostat
for Lightswitch
Thermostat.h
for Thing
Controller.cpp Thermostat.cpp





page 13 of 14

If necessary, use this page for extra answers. Indicate clearly which question you are answering here:
____________________






page 14 of 14
If necessary, use this page for extra answers. Indicate clearly which question you are answering here:
____________________


欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468