COMP104-15B Introduction to Computer Science 2
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
COMP104-15B (HAM)
2015 B SEMESTER EXAMINATIONS
Introduction to Computer Science 2
1. Explain the difference between the following pairs of C# object-oriented terminology:
(a) object and class
(b) base class and sub-class
(c) abstract method and virtual method
(d) call by reference and call by value
(e) a class property and a public class variable
(f) iteration and recursion
(g) inheriting from an interface and inheriting from a class
(h) public methods and protected methods
(i) composition and aggregation
(j) white-box testing and black-box testing (10 marks, 1 for each part)
2. (a) Add the following 8-bit binary numbers: 00111010 and 00111100 . (2 marks)
(b) Write binary 00111100 in hexadecimal . (2 marks)
(c) Write down – 124 in 8-bit 2s complement form. (2 marks)
(d) What is the 8-bit 2s complement value 00111010 in decimal . (2 marks)
(e) In 8-bit 2s complement form you can store – 128, but not +128 . Explain why . (2 marks)
3 . Indicate for each of the following statements whether it is true or false:
(a) a post-condition for a method is expressed using a Contract.Requires statement .
(b) when an arithmetic calculation exceeds the maximum or minimum value that can be
represented, the C# program always throws an exception .
(c) an abstract class cannot be instantiated to give an object .
(d) to be called a recursive solution the code in question must use a method that calls itself from within its own body .
(e) a private method is only accessible from within that class or one of its subclasses .
(f) an argument in a method passed as a ref parameter must be unassigned before the method is called .
(g) a virtual method must be overridden in a subclass.
(h) typecasting a variable to be a superclass may cause a run time error .
(i) items stored in a List<Shape> are required to be of type Shape, or a subclass of it .
(j) If the input is large enough, an algorithm of complexity O(n2) will outperform an algorithm of complexity O(2n) . (10 marks, 1 for each part)
4 . (a) Write out the Truth Table for the following circuit:
(3 marks)
(b) Write out the Boolean expression for the circuit shown in (a) above (3 marks)
(c) Apply De Morgan’s law to eliminate the OR operation(s) for your expression in (b) (2 marks)
(d) For your expression in (c) draw it as a circuit using only NAND gates. (2 marks)
5 . Problem Description: Comp104 Party Hire is a Hamilton firm that hires equipment and sells supplies for holding a party . Equipment can be hired by the day, or by the hour provided the return time is before 6pm. All equipment has a description, a barcode and a cost per day . The cost per hour is 1/8 of the cost per day . Some equipment requires additional safety equipment, such as cold resistant gloves for the dry ice smoke machine . Also, some equipment requires specific supplies to be bought to use with the equipment, such as the frozen carbon dioxide cubes to use with the dry ice smoke machine . Supplies have a description, a barcode and a cost . The major difference between supplies and equipment is that supplies are not returned .
When equipment is hired, a bond is paid to ensure it is returned undamaged . The bond is calculated at $10 per item, unless the equipment requires safety gear, which means a $100 bond instead . This bond is refunded when they return the hired items undamaged, or spent on repairs or replacements if needed .
The Comp104 Party Hire firm keeps track of its customers, storing their name, address, contact phone number and credit card details. Customers are listed as either good, poor or unknown . Customers gain the good status if they returned their last hire equipment without damage, i .e . they get their bond back, and receive the poor status if they lost some of their bond . Customers who have not hired anything before are listed as unknown . Only good customers can hire more equipment before they have returned their last hire .
(a) Identify 6 nouns in the description above that would make suitable candidates for
representation as objects in the design of a program that supports the required functionality . (2 marks)
(b) Identify two nouns in the above description that would benefit from sharing the same
base-class through inheritance . Detail any separation of fields between the base-class and its subclasses . Note: the nouns chosen need not be restricted to those given in your answer to (a) . (2 marks)
(c) Show how you could represent the relationship of your two classes from (b) using UML . (2 marks)
(d) Identify two classes that are in a ‘has a’ relationship in the above description . Show how these two classes would be related using a UML diagram. (2 marks)
(e) Write a suitable enumerated type for the categories of customer who can hire
equipment, i .e . good, poor or unknown . (2 marks)
6 . This question relates to MASM instructions and programs .
(a) For each of the three specifications below write one MASM instruction that satisfies
the specification:
(i) Get the value from the stack and store it in eax
(ii) Add the value in ebx to the value in eax and store the result in eax
(iii) Copy the value that appears at the address held in eax into ebx (3 marks, 1 for each part)
(b) Read and understand the following MASM program
1 _main:
2 mov ebx, 12
3 _loop: ; label
4 sub ebx, 2
5 push word ptr [star ] ; push word = push 2 bytes
6 pop word ptr [msg+ebx] ; pop word = pop 2 bytes
7 call _P ; _P will print string msg to the
; console and do nothing else at all
8 cmp ebx, 6
9 jge _loop ; jump greater than or equal to
10 _end:
11 ret
msg: db "Hello, World+!",0Dh, 0Ah,0
star: db "**",0
What is printed to the console when the above code is executed? (3 marks)
(c) Based on the above MASM program write a MASM program to output
Hello, World+!
Hello, W**ld+!
Hell** W**ld+! (4 marks)
7 . (a) You have to write a method sumN that calculates the total sum of the first N integers (N>=1) starting at 1. For example, sumN(5) is 15. Here is a recursive definition:
“The sum of the first N integers when N is 1, is 1. The sum of the first N integers when N is greater than 1 is N PLUS the sum of the first N– 1 integers.”
Turn this recursive definition into a C# recursive function . (4 marks)
The following bitmap is used in parts (b)–(d) . In the code excerpts below, the C# variable bexample is freshly assigned to be this bitmap at the start of each question .
(b) Copy the starting bitmap above and show how it would appear after the following
code has been executed (use ‘B’ to represent the pixels that are changed to Black):
static public void FloodFillA(Bitmap bitmap, int x, int y)
{
Color color = bitmap.GetPixel(x, y);
if (color != Color.White)
{
return;
}
else
{
bitmap.SetPixel(x, y, Pens.Black.Color);
FloodFillA(bitmap, x, y - 1);
FloodFillA(bitmap, x + 1, y);
}
}
// …
Bitmap.FloodFillA(bexample, 6, 3); (2 marks)
(c) Copy the starting bitmap above and show how it would appear after the following code has been executed (use ‘B’ to represent the pixels that are changed to Black, and ‘R’ for pixels that are set to Red):
s斗e斗S) duqtS) voSp 」toop」Sttg)gS斗med qS斗med‘ Sn斗 x‘ Sn斗 人(
}
DotoJ )otoJ = qS斗med .9o斗dSxot)x‘ 人(!
S} ))otoJ != DotoJ .W|S斗o(
}
Jo斗uJn!
杨
otso
}
S} ))x % C( == 6(
}
qS斗med .So斗dSxot)x‘ 人‘ dons .gte)入 .DotoJ(!
杨
otso
}
qS斗med .So斗dSxot)x‘ 人‘ dons .Nop .DotoJ(!
杨
」toop」Sttg)qS斗med‘ x - t‘ 人(!
」toop」Sttg)qS斗med‘ x + t‘ 人(!
」toop」Sttg)qS斗med‘ x‘ 人 - t(!
」toop」Sttg)qS斗med‘ x‘ 人 + t(!
杨
杨
// …
gS斗med .」toop」Sttg)qoxemdto‘ 9‘ E(! (2 marks)
(d) Copy the starting bitmap above and show how it would appear after the following code has been executed (use ‘B’ to represent the pixels that are changed to Black):
s斗e斗S) duqtS) voSp 」toop」SttD)gS斗med qS斗med‘ Sn斗 x‘ Sn斗 人(
}
DotoJ )otoJ = qS斗med .9o斗dSxot)x‘ 人(!
S} ))otoJ != DotoJ .W|S斗o(
}
Jo斗uJn!
杨
otso
}
qS斗med .So斗dSxot)x‘ 人‘ dons .gte)入 .DotoJ(!
」toop」SttD)qS斗med‘ x - t‘ 人 - t(!
」toop」SttD)qS斗med‘ x + t‘ 人 - t(!
」toop」SttD)qS斗med‘ x - t‘ 人 + t(!
」toop」SttD)qS斗med‘ x + t‘ 人 + t(!
杨
杨
// …
gS斗med .」toop」SttD)qoxemdto‘ 9‘ E(! (2 marks)
8 . The following is a syntactic description of a Turing Machine:
; State 1
S1 0 0 r S1
S1 1 1 r S2
S1 _ _ * halt-accept
; State 2
S2 0 0 r S2
S2 1 1 r S1
S2 _ _ * halt-reject
Where the start state is S1 and the input tape consists of 1, 0 and the blank symbols.
The syntax used in the description above is the same as the on-line Turing Machine web- site studied in lectures, where:
• Each line contains one tuple of the form '<current state> <current symbol> <new symbol> <direction> <new state>'.
• You can use any number or word for <current state> and <new state>, e.g., 10, a, state1. State labels are case-sensitive.
• You can use any character for <current symbol> and <new symbol>, or '_' to represent blank (space). Symbols are case-sensitive.
• <direction> should be 'l', 'r' or '*', denoting 'move left', 'move right' or 'do not move', respectively.
• Anything after a ';' is a comment and is ignored.
• The machine halts when it reaches any state starting with 'halt', e.g., halt, halt- accept.
(a) Draw the Turing Machine Diagram of the above machine description. (2 marks)
(b) What are the halt states for the following inputs:
11100111
10100101
00110010
10101010 (2 marks)
(c) What does the Turing Machine algorithm determine? (2 marks)
(d) Write a C# method with the signature:
public bool testBinaryArray(List<char> tape)
{
// ...
}
that accomplishes the same computation as the Turing Machine. Assume the TM tape is passed in as a parameter of type List<char>, and contains the characters '1', '0', and '_' to represent the tape input values 1, 0 and blank (space) respectively. (3 marks)
(e) What is the Big O notation for the method you have written in part (d)? (1 mark)
9 . Here are the AlienBomb and Missile classes from an in-development space invaders program.
public class AlienBomb {
private int x;
private int y;
private Brush black_brush;
private const int RADIUS = 20;
public AlienBomb(int x, int y){
this.x = x;
this.y = y;
black_brush = new SolidBrush(Color.Black);
}
public int X { get { return x; } }
public int Y { get { return y; } }
public void Move(int v) {
y += v;
}
public void Draw(Graphics paper){
Rectangle rect
= new Rectangle(x-RADIUS,y-RADIUS,2*RADIUS,2*RADIUS);
paper.FillEllipse(black_brush, rect);
}
}
public class Missile {
private int x;
private int y;
private Brush[] alternate_brush;
private const int WIDTH = 10;
private const int HEIGHT = 30;
public Missile(int x, int y) {
this.x = x;
this.y = y;
alternate_brush = new Brush[2] { new SolidBrush(Color.Red),
new SolidBrush(Color.Yellow) };
}
public int X { get { return x; } }
public int Y { get { return y; } }
public void Move(int v) {
y -= v;
}
public void Draw(Graphics paper) {
Rectangle rect
= new Rectangle(x - WIDTH/2, y - HEIGHT/2, WIDTH, HEIGHT);
int brush_pos = (y%20<10) ? 0 : 1;
Brush brush = alternate_brush[brush_pos];
paper.FillRectangle(brush, rect);
}
}
(a) Write an abstract class called Sprite that contains all code elements common to
both AlienBomb and Missile, so that they can both inherit from it . (5 marks)
(b) Rewrite both AlienBomb and Missile to be subclasses of your new Sprite class. (5 marks)
10 . Recall the sorting algorithms (given in pseudo code):
Bubble Sort:
for i = 1:n,
swapped = false
for j = n:i+1,
if a[j] < a[j-1],
swap a[j,j-1]
swapped = true
→ invariant: a[1 . .i] in final position
break if not swapped
end
Selection Sort:
for i = 1:n,
k = i
for j = i+1:n, if a[j] < a[k], k = j
→ invariant: a[k] smallest of a[i . .n]
swap a[i,k]
→ invariant: a[1 . .i] in final position
end
For the following array of eight integers:
Answer the following questions:
(a) When using BubbleSort, how many number comparison operations and swap
operations are performed? (Note: the number of comparisons and swaps need not be the same .) (3 marks)
(b) When using SelectionSort, how many number comparison operations and swap
operations are performed? (Note: the number of comparisons and swaps need not be the same .) (2 marks)
(c) Write out the Big O notation for the following runtime complexities:
T(f(A)) = 2n3 + 5n4 + 9n
T(f(A)) = 5n3 + 2n2 + 10n + 110
T(f(A)) = 3n + 3n3
T(f(A)) = 4n + 6n3 + 2n! (4 marks)
(d) In terms of the Big O notations given in (c), which one has (or ones share) the biggest growth rate? (1 mark)
11 . The following code is a (minimalist) set of classes that supports the creation and manipulation of Circles (integer x-y position and radius) . The signatures to all the required methods exists, however some have not yet been implemented . Assuming the correct implementation of these ‘missing’ methods, the main program creates a variety of Circle objects, and then stores them in a List object, only if no such Circle object already exists (i .e . a circle with the exact same x-y position and radius) in the List object . The main program finishes by printing out information about the List of Circles that has been created .
Your task in this question is to provide implementations of these missing methods, and to show what the output of the main program is.
public class Point2D {
public int X;
public int Y;
public Point2D(int x, int y) {
this .X = x;
this .Y = y;
}
public Point2D(Point2D ptin) {
this .X = ptin .X;
this .Y = ptin .Y;
}
public override bool Equals(Object obj) {
// … to be implemented
}
public override string ToString() {
return "pt(x=" + X + ",y=" + Y + ")";
}
}
class Circle {
private Point2D _org;
private int _rad;
public Circle(Point2D origin, int radius) {
_org = origin;
_rad = radius;
}
public override bool Equals(Object obj) {
// … to be implemented
}
public override string ToString() {
return "circle(" + _org.ToString() + ",r=" + _rad.ToString() + ")";
}
}
s斗e斗S) )tess dJo8Jem }
s斗e斗S) 1Ss斗>DSJ)to< )SJ)to一tSs斗!
s斗e斗S) dJSve斗o voSp AppI}UnSbuo)DSJ)to nowDSJ)( }
// Ont人 epp ,nowDSJ), 斗o ,)SJ)to一tSs斗, S} e DSJ)to oq车o)斗 // wS斗| 斗|o semo vetuos poos no斗 etJoep人 oxSs斗
// … 斗o qo Smdtomon斗op
杨
s斗e斗S) voSp WeSn)( }
)SJ)to一tSs斗 = now 1Ss斗>DSJ)to<)(!
doSn斗Za d斗一Jo)斗一)oJnoJt = now doSn斗Za)t6‘ 66 (!
doSn斗Za d斗一Jo)斗一)oJnoJZ = now doSn斗Za)v6‘ v6 (!
doSn斗Za d斗一Jo)斗一)oJnoJE = now doSn斗Za)t6‘ 66 (!
DSJ)to )SJ)tot = now |
DSJ)to)d斗一Jo)斗一)oJnoJt‘ E6 (! |
DSJ)to )SJ)toZ = now |
DSJ)to)d斗一Jo)斗一)oJnoJZ‘ S6 (! |
DSJ)to )SJ)toE = now |
DSJ)to)d斗一Jo)斗一)oJnoJE‘ E6 (! |
DSJ)to )SJ)tov = now |
DSJ)to)d斗一Jo)斗一)oJnoJt‘ E6 (! |
DSJ)to )SJ)toS = now |
DSJ)to)d斗一Jo)斗一)oJnoJt‘ S6 (! |
DSJ)to )SJ)to9 = now |
DSJ)to)d斗一Jo)斗一)oJnoJZ‘ E6 (! |
AppI}UnSbuo))SJ)tot(!
AppI}UnSbuo))SJ)toZ(!
AppI}UnSbuo))SJ)toE(!
AppI}UnSbuo))SJ)tov(!
AppI}UnSbuo))SJ)toS(!
AppI}UnSbuo))SJ)to9(!
Donsoto .Ou斗 .WJS斗o1Sno)"NumqoJ o} )SJ)tos Sn tSs斗 = " +
)SJ)to一tSs斗 .Doun斗(!
}oJoe)| )DSJ)to )uJJDSJ) Sn )SJ)to一tSs斗( }
Donsoto .Ou斗 .WJS斗o1Sno)" " + )uJJDSJ) .ToS斗JSn8)((!
杨
杨
杨
(a) Implement the 3buets)( method in the doSn斗 class. (1 mark)
(b) Implement the 3buets)( method in the DSJ)to class. (2 marks)
(c) Implement the AppI}UnSbuo)( method . Include pertinent contract information (Contract .Requires and Contract .Ensures) in the method, where appropriate/relevant . (5 marks)
(d) What is the output produced by running the program? (2 marks)
2022-10-24