Figure 1: The er diagram for the terrorist database ... - ByteLABS
Figure 1: The er diagram for the terrorist database ... - ByteLABS
Figure 1: The er diagram for the terrorist database ... - ByteLABS
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Name<br />
Nationality<br />
Name<br />
City<br />
T<strong>er</strong>rorists<br />
Belong_To<br />
Groups<br />
Wanted_For<br />
Stolen<br />
Date<br />
Location<br />
Crimes<br />
Used_In<br />
Weapons<br />
Date<br />
Location<br />
Type<br />
S<strong>er</strong>ial<br />
<strong>Figure</strong> 1: <strong>The</strong> <strong>er</strong> <strong>diagram</strong> <strong>for</strong> <strong>the</strong> t<strong>er</strong>rorist <strong>database</strong>.<br />
Module Title: Inf1B<br />
Exam Diet (Dec/April/Aug): April 2005<br />
Brief notes on answ<strong>er</strong>s:<br />
Part A DATA AND ANALYSIS ANSWERS<br />
Part A, answ<strong>er</strong>s to questions 1 - 3<br />
1. (a) A possible <strong>er</strong> <strong>diagram</strong> is shown in <strong>Figure</strong> 1.<br />
(b) For <strong>the</strong> <strong>er</strong> <strong>diagram</strong> of <strong>Figure</strong> 1, <strong>the</strong> sql statements are <strong>the</strong> following:<br />
create table Groups (name : char(30), city : char(30),<br />
primary key (name));<br />
create table T<strong>er</strong>rorists (name : char(30),<br />
nationality : char(30),<br />
gname : char(30),<br />
primary key (name, gname),<br />
<strong>for</strong>eign key (gname)<br />
ref<strong>er</strong>ences groups<br />
on delete cascade);<br />
create table Crimes (date : char(20), location : char(30),<br />
primary key (date, location));<br />
create table Weapons (s<strong>er</strong>ial : char(20), type : char(30),<br />
primary key (s<strong>er</strong>ial));<br />
create table Wanted_For (name : char(30), gname(30),<br />
date : char(20), location : char(30),<br />
primary key (name, gname,<br />
date, location),<br />
<strong>for</strong>eign key (name, gname)<br />
ref<strong>er</strong>ences T<strong>er</strong>rorists,<br />
<strong>for</strong>eign key (date, location)<br />
ref<strong>er</strong>ences Crimes);<br />
create table Used_In (date : char(20), location : char(30),<br />
i
s<strong>er</strong>ial : char(20),<br />
primary key (date, location, s<strong>er</strong>ial),<br />
<strong>for</strong>eign key (date, location)<br />
ref<strong>er</strong>ences Crimes,<br />
<strong>for</strong>eign key (s<strong>er</strong>ial),<br />
ref<strong>er</strong>ences Weapons);<br />
create table Stolen (name : char(30), s<strong>er</strong>ial : char(20),<br />
date : char(20), location : char(30),<br />
primary key (name, s<strong>er</strong>ial),<br />
<strong>for</strong>eign key (name)<br />
ref<strong>er</strong>encesGroups,<br />
<strong>for</strong>eign key (s<strong>er</strong>ial)<br />
ref<strong>er</strong>ences Weapons);<br />
(c) <strong>The</strong> qu<strong>er</strong>y in tuple-relational calculus is:<br />
{P | T ∈ T<strong>er</strong>rorists ∧ W ∈ Wanted For∧<br />
U ∈ Used In ∧ V ∈ Weapons ∧<br />
T.name = W.name ∧<br />
W.date = U.date ∧ W.location = U.location ∧<br />
U.s<strong>er</strong>ial = V.s<strong>er</strong>ial ∧<br />
P.name = T.name ∧ P.type = V.type}<br />
2. (a) (i). <strong>The</strong> cosine between two vectors ⃗x and ⃗y is defined as:<br />
cos(⃗x, ⃗y) =<br />
⃗x · ⃗y<br />
|⃗x||⃗y| =<br />
∑ ni=1<br />
x i y i<br />
√ ∑ni=1<br />
x 2 √ ∑ni=1<br />
y 2 (1)<br />
(ii).<br />
cos(Q, D 1 ) =<br />
0 · 2 + 1 · 4 + 1 · 2 + 0 · 1 + 0 · 0<br />
√<br />
02 + 1 2 + 1 2 + 0 2 + 0 2√ 2 2 + 4 2 + 2 2 + 1 2 + 0 = 6<br />
2 5 √ 2 (2)<br />
cos(Q, D 2 ) =<br />
0 · 0 + 1 · 0 + 1 · 2 + 0 · 2 + 0 · 1<br />
√<br />
02 + 1 2 + 1 2 + 0 2 + 0 2√ 0 2 + 0 2 + 2 2 + 2 2 + 1 = √ 2 (3)<br />
2 2<br />
This means that D 1 is clos<strong>er</strong> to Q than D 2 and will be ranked high<strong>er</strong> by <strong>the</strong><br />
in<strong>for</strong>mation retrieval engine.<br />
(b) <strong>The</strong> basic patt<strong>er</strong>n <strong>for</strong> noun phrases is: DT? JJ* NN. Instead of det<strong>er</strong>min<strong>er</strong>s,<br />
we can have num<strong>er</strong>als, and instead of simple nouns, we can have compounds<br />
(such as Apogee common stock). This yields <strong>the</strong> following regular expression:<br />
(DT|CD)? JJ* (NN|JJ)* NN. If we allow pre-nominal det<strong>er</strong>min<strong>er</strong> phrases such<br />
as each of, we get: (DT IN)? (DT|CD)? JJ* (NN|JJ)* NN.<br />
In CQP notation (as required in <strong>the</strong> question), this regular expression is:<br />
([pos="DT"] [pos="IN"])? [pos="DT|CD"]? [pos="JJ"]*<br />
[pos="NN|JJ"]* [pos="NN"]<br />
ii
Oth<strong>er</strong> solutions are possible. For example, two prop<strong>er</strong>ties of NPs have not been<br />
taken into account: coordination (e.g., <strong>the</strong> cat and <strong>the</strong> dog) and genitives (e.g.,<br />
Tony’s wife). Howev<strong>er</strong>, students should receive full credit if <strong>the</strong>ir regular expression<br />
cov<strong>er</strong>s all <strong>the</strong> NPs given in <strong>the</strong> question.<br />
iii
Part B OBJECT ORIENTED PROGRAMMING ANSWERS<br />
Part B, answ<strong>er</strong>s to questions 4 - 6<br />
3. Examples of appropriate definitions are as follows.<br />
(a) An it<strong>er</strong>ative v<strong>er</strong>sion is:<br />
public static int fibonacci( int n )<br />
{ int a = 0;<br />
int b = 1;<br />
int f = 0;<br />
if ( n == 0 || n == 1 )<br />
return n;<br />
else<br />
return fib( a, b, f, n );<br />
}<br />
public static int fib( int a, int b, int f, int n)<br />
{ <strong>for</strong> ( int i = 2; i
Marking guide: 3 marks <strong>for</strong> correct handling of <strong>the</strong> array. 2 marks <strong>for</strong> <strong>the</strong> <strong>for</strong><br />
loop (or similar).<br />
(e) A printing definition is:<br />
public class fibbo {<br />
public static void main( String args[] )<br />
{ int numb<strong>er</strong> = 7;<br />
int array[] = new int[numb<strong>er</strong>];<br />
fibonacciArray( array );<br />
<strong>for</strong> ( int count<strong>er</strong> = 0; count<strong>er</strong> < array.length; count<strong>er</strong>++ )<br />
System.out.println(count<strong>er</strong> + " has fibonacci " + array[ count<strong>er</strong> ]);<br />
}<br />
}<br />
Marking guide: 2 marks <strong>for</strong> correct handling of <strong>the</strong> array. 2 marks <strong>for</strong> <strong>the</strong> <strong>for</strong><br />
loop (or similar). 1 mark <strong>for</strong> an appropriate print statement.<br />
4. (a) public abstract class Animal {<br />
private String name;<br />
public Animal () { this(""); }<br />
public Animal (String n) { setName(n); }<br />
public void setName (String n) { name = n; }<br />
public String getName () { return name; }<br />
public abstract boolean canWalk ();<br />
public abstract boolean canFly ();<br />
}<br />
public abstract boolean canSwim ();<br />
public class Bird {<br />
public Bird () { sup<strong>er</strong>(); }<br />
public Bird (String n) { sup<strong>er</strong>(n); }<br />
public boolean canWalk () { return false; }<br />
public boolean canFly () { return true; }<br />
}<br />
public boolean canSwim () { return false; }<br />
public class Mammal {<br />
public Mammal () { sup<strong>er</strong>(); }<br />
v
public Mammal (String n) { sup<strong>er</strong>(n); }<br />
public boolean canWalk () { return true; }<br />
public boolean canFly () { return false; }<br />
}<br />
public boolean canSwim () { return false; }<br />
public class Fish {<br />
public Fish () { sup<strong>er</strong>(); }<br />
public Fish (String n) { sup<strong>er</strong>(n); }<br />
public boolean canWalk () { return false; }<br />
public boolean canFly () { return false; }<br />
}<br />
public boolean canSwim () { return true; }<br />
(b) (i). False. One cannot instantiate an abstract sup<strong>er</strong>class.<br />
(ii). True. It is p<strong>er</strong>fectly acceptable <strong>for</strong> an abstract sup<strong>er</strong>class ref<strong>er</strong>ence to ref<strong>er</strong><br />
to a subclass instance.<br />
(iii). True. Same reasons as above.<br />
(iv). False. Birds and Mammals are at <strong>the</strong> same level of <strong>the</strong> hi<strong>er</strong>archy; one cannot<br />
ref<strong>er</strong> to <strong>the</strong> oth<strong>er</strong>.<br />
5. (a) <strong>The</strong> implementation of levelOrd<strong>er</strong>() is <strong>the</strong> following:<br />
public void printLevelOrd<strong>er</strong> () {<br />
Queue q = new Queue();<br />
q.enqueue(root);<br />
while (! q.isEmpty()) {<br />
TreeNode head = (TreeNode) q.dequeue();<br />
System.out.print(head.getValue() + " ");<br />
if (head.getLeft() != null)<br />
q.enqueue(head.getLeft());<br />
if (head.getRight() != null)<br />
q.enqueue(head.getRight());<br />
}<br />
}<br />
It gen<strong>er</strong>ates [11, 7, 17, 1, 8, 16, 22, 25] on <strong>the</strong> given tree.<br />
(b) It is <strong>the</strong> equivalent of a preord<strong>er</strong> trav<strong>er</strong>sal.<br />
vi