COMP 401 Fall 2012 Midterm

hundredcarriageΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 5 μήνες)

78 εμφανίσεις

1


COMP
401
Fall 201
2

Midterm


Wednesday
,
Nov

28
, 201
2
,
11:00
-
12
:
15

Instructions

1.

Please spread out and

try and

sit
in

alternate seats.

2.

This is a closed book exam. You will not be penalized for errors in Java syntax.

3.

Write on the exam itself.

4.

Write on a blank

page

if there is not enough space to solve a problem.

5.

There are:



1
1

numbered pages including this one
,

and

any
marked

blank pages.



4

questions
.



75

regular and 5 bonus

points. Point values appear in brackets next to each question
.

6.

You have
75 minutes
.

7.

You

are not required to comment any code you write, but may get partial credit if you
g
ive

appropriate comments but incorrect code.

8.

If you need to make any assumptions to clarify a problem,
write your assumptions down.
Only
reasonable assumptions get full
credit.

9.

Please
inform the proctor of

anything in the exam that you think is a mistake.

10.

Your code will be evaluated not onl
y for correctness, but also for

time and space efficiency and
style
.

11.

You cannot use any Java capabilities not covered in class.

12.

To ans
wer questions about some piece of code given here, you can mark the code directly.

13.

If you do not understand some English word, do not hesitate to ask the proctor. Naturally, you are
expected to know the computer science terms defined in class.


Name (in
Capitals)


____________________________________

Pledge: I have neither given nor received unauthorized aid on this exam.


(
Signed
)____________________________________


For survey purposes, please indicate the time at which you turned in the exam.


______
_____

Please do not write below

1. _____/
16

2
. _____/
26


3
. _____/
1
5
+5


4
. _____/
1
8
.




Total: _____/ 75
+5

2




1.

[
16


pts
.
]

C
OMPOSITE
P
ATTERN

AND
A
BSTRACT
C
LASSES

Consider the following
interfaces

related to

an
employee in an organization, who may be
a worker
or
a
manag
er
with
two
subordinates
. All of the code related to this problem is located in the same package.


This code is repeatedly used in the exam and

relevant

parts of it
are

reproduced later.
PropertyListenerRegisterer

and
APropertyListenerSu
pport

are types we saw in class and
given

at the end
of the exam.



public

interface

Employee
extends



PropertyListenerRegisterer {


public

static

final

int



START_SALARY

= 50000;


public

int

getSalary();


public

String getName();


public

void

giveRaiseToTeam(


double

aFraction);

}

public

interface

Manager
extends



Employee {


public

static

String
MANAGER_START



=
"(Manager"
;


public

static

String
MANAGER_END

=
")"
;


Employee getSubordinate1();


Employee getSubordinate2();

}


public

interface

Worker
extends

Employee{


public

static

String
WORKER_START

=
"Worker"
;

}

public

class

AnEmployee
implements

Employee{


PropertyListenerSupport
listenerSupport

=
new

APropertyListenerSupport();


int

salary

=
START_SALARY
;


String
name
;


public

AnEmployee (String aName) {
name

= aName; }


public

String getName() {
return

name
; }



public

int

getSalary() {
return

salary
; }


public

void

giveRaiseToTeam(
double

aFraction) {


salary

=
salary

+ (
int
) (
salary
*aFraction);


listenerSupport
.notifyAllListeners(


new

PropertyChangeEvent(
this
,
"Salary"
,
null
,
salary
));


}



public

void

addPropertyChangeListener (PropertyChangeListener aListener) {


lis
tenerSupport
.add(aListener);


}


public

String toString() {
return

name
; }

}

public

class

AWorker
extends

AnEmployee
implements

Worker {


public

AWorker(String aName) {


super
(aName);


}


public

String toString() {


return

WORKER_START

+
" "

+
super
.toString();


}

}

3




a)

[4

pts]
Explain why this code implements the composite pattern
.
You should

identify
the interfaces
that participate in this pattern

and the

relevant

relationships
among

these types.



b)

[2pts] Name a
class in this code that should be abstract:_______________________

c)


[10 pts] Consider the following class

defining the variable
ceo
, which we use repeatedly in this
exam:


Give
the output o
f the following print statement:


System.
out
.println(
ceo
.toString())
;







public

class

AManager
extends

AnEmployee
implements

Manager {


Employee
subordinate1
,
subordinate2
;


public

AManager (String aName, Employee aSubordinate1, Employee aSubordinate2) {


super

(aName);


subordinate1

= aSubordinate1;


subordinate2

= aSubordinate2;


}


public

void

giveRaiseToTeam(
double

aFraction) {


super
.giveRaiseToTeam(aFraction);


subordinate1
.giveRaiseToTeam(aFraction);


subordinate2
.giveRaiseToTeam(aFraction);


}


public

Employee getSubordinate1() {


return

subordinate1
;


}


public

Employee getSubordinate2() {


return

subordinate2
;


}


public

String toString() {


return

MANAGER_START

+
" "

+
super
.toString() +
" "

+
subordinate1
.toString() +
" "

+



subordinate2
.toString() +
" "

+
MANAGER_END
;


}

}

public

class

EmployeeTreeCreator {


static

final

Manager
ceo

=
new

AManager (
"Alison"
,






new

AManager(
"Brian"
,


new

AWorker(
"Catherine"
),


new

AWorker(
"Danny"
)),






new

AWorker(
"Emily"
));


}

4


2.

[
26

pts
.
]
MVC

Consider the class AnEmployee presented before
,

and some additional classes and interfaces.


public

class

AnEmployee
implements

Employee{

// same as in question

1


PropertyListenerSupport
listenerSupport

=
new

APropertyListenerSupport();


int

salary

=
START_SALARY
;


String
name
;


public

AnEmployee (String aName) {
name

= aName; }


public

String getName() {
return

name
; }



public

int

getSalary() {
return

salary
; }


public

void

giveRaiseToTeam(
double

aFraction) {


salary

=
salary

+ (
int
) (
salary
*aFraction);


listenerSupport
.notifyAllListeners(
new

PropertyChangeEvent(
this
,
"Salary"
,
null
,
salary
));


}



public

void

addPropertyChangeListener (PropertyChangeListener aListener) {


listenerSupport
.add(aListener);


}


public

String toSt
ring() {
return

name
; }

}

public

class

ASalaryTextFieldDisplayer
implements

PropertyChangeListener {



Employee
employee
;


JTextField
textField
;


public

ASalaryTextFieldDisplayer (JTextField aTextField, Employee anEmployee) {


employee

= anEmployee
;


textField

= aTextField;


}



public

void

propertyChange(PropertyChangeEvent anEvent) {


textField
.setText(
""

+
employee
.getSalary());


}

}

public

class

EmployeeTextFieldUICreator
extends

EmployeeTreeCreator {


public

static

void

displayTextFieldInFrame(JFrame frame) {


JTextField textField =
new

JTextField(
"Some Salary"
);


PropertyChangeListener listener =
new

ASalaryTextFieldDisplayer(textField,
ceo
);


ceo
.addPropertyChangeListener(listener);


frame.add(textField);


}

public

class

EmployeeTextFieldMain
extends

EmployeeTextFieldUICreator {


public

static

void

createAndDisplayFrame() {


JFrame frame =
new

JFrame(
"Employee"
);


displayTextFieldInFrame
(frame);


frame.setSize(
new

Dimension(300, 200));


frame.setVisible(
true
);



}


public

static

void

main (String[] args) {


createAndDisplayFrame
();


ThreadSupport.
sleep
(1000);



ceo
.giveRaiseToTeam(0.1);


}

}


}



// 2
pts

for some salary



// 2
pts

for initial salary + raise



//
-
2
pts

for showing initial salary




}


}


ceo
.giveRaiseToTeam(0.1);

5


a)

[2 pts
.
]

Is there a model class in the
code

shown on the left
? If

so
,

name it?


b)

[2 pts
.
] I
s there a view class in this code
? If yes,

name it?


c)

[2 pts
.
] I
s there a controller class in thi
s code? If yes, name it?


d)

[
6

pts
.
] The main met
hod creates and displays a JTextField instance

for showing an employ
ee
salary. What values will the

text property of
the text field

take

when the
method is run?




e)

[
14

pts
.
] Suppose that instead of
displaying an employee salary

as text, we wished to display it as a
rectangle whose height is proportional to the salary
, that is,
whose height is

the salary divided by
some constant giving the dollars per pixel
. Write alternatives to
ASalaryTextFieldDisp
layer

and
EmployeeTextFieldUICreator

for creating such a user interface
(using the MVC pattern)
. The outline
for these classes is given below as are some of the constants

you
can
use. You can use the
Graphics
method d
rawRect(int x, int y, int width, int h
eight)

to draw the rectangle.


You do not have to write
the main class that

creates a frame and calls
displayRectangleInFrame
().


public

class

ASalaryRectangleDisplayer
extends

Component



implements

PropertyChangeListener {


public

static

final

int

RECT_WIDTH

= 10;


public

static

final

int

RECT_X

= 100;

// assume RECT_Y = 0


public

static

final

int

DOLLARS_PER_PIXEL

= 1000;






public

void

paint (Graphics g) {





}


public

void

propertyChange(
PropertyChangeEvent arg0) {





}

}

public

class

EmployeeRectangleUICreator
extends

EmployeeTreeCreator {



public

static

void

displayRectangleInFrame(JFrame frame) {








}

}



6


3.

[
1
5

+ 5

pts
.
]
A
NIMATION
,

T
HREADS

AND
C
OMMAND
O
BJECTS

This
question

uses the clearance manager code
presented

in class and used in your assignments. It is
given at the end of the
exam
.

public

class

ARaiseAnimator
implements

RaiseAnimator{


Employee
employee
;


ClearanceManager
clearanceManager
;


public

ARaiseAnimator
(Employee anEmployee) {


employee

= anEmployee;


clearanceManager

=
new

AClearanceManager();


}


void

step(
boolean

wait) {


if

(wait) {


clearanceManager
.waitForProceed();


}
else

{


clearanceManager
.proceed();


ThreadSupport.
sleep
(1000);


}


}


public

void

animateRaise(
double

aFraction,
boolean

aWait) {


System.
out
.println(aWait);
// output


step(aWait);



System.
out
.println(aFraction);

// output


employee
.giveRaiseToTeam(aFraction);


step(aWait);


System.
out
.println
(aFraction);

// output


employee
.giveRaiseToTeam(aFraction);


}


public

void

scroogyAntiScroogyRaises() {


// giving negative raise below and setting wait to true


Runnable runnable1 =
new

ARaiseAnimationRunnable(
this
,
-
0.1,
true
);


(
new

Thre
ad(runnable1)).start();


// giving positive raise below and setting wait parameter to false


Runnable runnable2 =
new

ARaiseAnimationRunnable(
this
, 0.1,
false
);


(
new

Thread(runnable2)).start();


}


}

public

class

ARaiseAnimationRunnable
implements

Runnable {


double

fraction
;


RaiseAnimator
raiseAnimator
;


boolean

wait
;


public

ARaiseAnimationRunnable (RaiseAnimator aRaiseAnimator,
double

aFraction,


boolean

aWait) {


raiseAnimator

= aRaiseAnimator
;


fraction

= aFraction;


wait

= aWait;


}


public

void

run() {


raiseAnimator
.animateRaise(
fraction
,
wait
);




}

}

7



a)


[2 pts
.
]

Is there a command class

in this code? If yes, name it.


b)

[
1
3

pts
.
]
Give the
Console
output of the main method above
.

If you think more than one
sequence of outputs is possible, give
one of them
, ideally the one you think is most probable
.

You can assume that the threads created by this program are the only ones that run on the
computer and that only one processor

is av
ailable to execute them.

You do not have to give the
values of the text property of the text field


only the output of the

four

println() calls



one in
main() and three in animateRaise()
.










c)

[5 Bonus

pts
.
]
What will be the

Console

output if the animation method was synchronized?

public

synchronized

void

animateRaise(
double

aFraction,
boolean

aWait)








public

class

RaiseAnimationMain
extends

EmployeeTreeCreator {


public

static

void

main (String[] args) {


(
new

ARaiseAnimator(
ceo
)).scroogyAntiScroogyRaises();


System.
out
.println(
"Main Ended"
);

// output


}

}

8


4.

[1
8

pts
.
]
R
ECURSIVE
D
ESCENT
P
ARSING

In this problem you must extend the code of problem 1 to allow users to input an employee
tree. Some
of this code is reproduced below, with ellipsis (…) being used for parts you should not have to worry
about in this problem.



The input is assumed to be a sequence of String tokens, where each
token

is
entered

on a separate line.
Three of the tokens are
, the string

constants

MANAGER_START
,


MANAGER_END, and WORKER_START,
w
hich are defined by the Manager and Worker interfaces above, and
have
the values (
"(Manager"
,
")",
and
"Worker"
,
respectively. The fourth token, which we shall refer to as EMPLOYEE_NAME
,

is
an arbitrary

string. You have to write a recursive descent parser that creates a
single

employee tree
from a sequence of these tokens. The grammar for the input is given below.


<Employee>


<Manager> | <Worker>

<Worker>


WORKER_START EMPLOYEE_NAME

<Manager>


MANA
GER_START EMPLOYEE_NAME


<Employee> <Employee>


MANAGER_END

A

user inputs an employee by either inputting a worker or a manager. A worker is input by entering the
token WORKER_START followed by the token EMPLOYEE_NAME (whic
h gives the name of the worker). A
manager is input by entering the token MANAGER_START, followed by the token EMPLOYEE_NAME
(which gives the name of the manager), followed by a sequence of tokens representing an employee
(
who is

the first subordinate o
f the manager), followed

by a sequence of
tokens representing another
employee (who is the second subordinate of the manager), followed by the token MANAGER_END .

The following two examples show user input and the associated Employee
(
tree
)

created the parser







public

interface

Worker
extends



Employee {


public

static

String
WORKER_START




=
"Worker"
;

}

public

interface

Manager
extends



Employee {


public

static

String
MANAGER_START



=
"(Manager"
;


public

static

String
MANAGER_END

=
")"
;




}


public

class

AWorker
extends



AnEmployee
implements

Worker {


public

AWorker(String aName) {





}




}

public

class

AManager

extends

An
Employee

implements

Manager{


public

AManager (String aName,


Employee aSubordinate1,


Employee aSubordinate2) {





}




}

new

AWorker(
"Emily"
));



9






Assume that the main parsing program consists of the following code:

public

static

void

main (String[] args) {


Employee employee =
parseEmployee
();

}

You need to write the the parseEmployee function that parses user input to return the

Employee
instance representing

the employee tree input by the
user
. You will get partial credit if you are able to
parse a worker but not a manager. You can use the Console.readString() function to read the next user
input line as a string.

The first ca
ll to this function

is given to you. Since you are doing recrusive descent
parsing, you will need to write addition
al helper functions called b
y parseEmployee().


public

static

Employee parseEmployee() {


String
worker
OrManagerStart = Console.
readString
()
;




























new

AManager (
"Fan"
,


new

AWorker(
"Grace"
),


new

AManager(
"Hampton"
,




new

AWorker(
"Isaac"
),


new

AWorker(
"Jeanne"
)


)

);



10



Code from class lectures used in the exam questions:




11


Extra blank page