JDC_Lecture23 - Computer Science

Download Report

Transcript JDC_Lecture23 - Computer Science

UMass Lowell Computer Science 91.460
Java and Distributed Computing
Prof. Karen Daniels
Fall, 2000
Lecture 23: Review
Mon. 12/11
Course Grading
Homework
 Exam 1
 Exam 2
 Exam 3

40%
15% (closed book)
20% (open book, notes )
25% (open book, notes)
Results are scaled if necessary.
Final Exam: Logistics
Thursday, 12/21
 Olsen 410 8:00-10:00 a.m.
 Open book, open notes
 Closed computers, neighbors
 Cumulative
 Worth 25% of grade

We’re trying to
change this to
OS 311. It
looks like OS
311 is
available.
Text/Chapter Coverage

Basic Java (Deitel):
 Chapters:

Advanced Java (Deitel):
 Chapters:

1-10, 14, 17
11-13, 15, 20, 22-24
Jini (Core Jini):
 Chapters:
1, 2, 3, 5 & (attribute matching)
 Appendix A
All questions refer to Java 2 unless otherwise specified.
Format and Emphasis
This exam will have a mixture of questions of the
following types:
1) Multiple Choice
2) Short Answer
3) Debugging
4) Fill-in-the-blanks
5) Write a variation on code
6) Code from scratch
Review: Part I
Basic Java
Sampling of Topics
Applets
 Applications
 Data Types
 Operators
 Expressions
 Interfaces
 Control Structures
 Recursion

Packages
 Object-Oriented

 Classes
(and inner)
 Methods
 Polymorphism

Files & Streams
 Chaining
 Object
serialization
Polymorphism is for Methods
A
A::foo()
B::foo()
B::foo()
B
a.x=1
b.x=2
((A)b).x=1
B
x=2
foo()
A
x=1
foo()
Thanks Jenn for this example
Typical Multiple Choice Question

Which statement is true about a non-static inner
class?
A) It must implement an interface.
B) It is accessible from any other class.
An object of a non-static inner class is able to
C) It canaccess
only beprivate
instantiated
in of
thethe
enclosing
class.
variables
outer class
in
it is defined.
D) It must be final, ifwhich
it is declared
in a method scope.
E) It can access private instance variables in the
enclosing object.
From Java 2 Certification
Typical Multiple Choice Question

Which of the following are true?
A) If a package statement is included in a source code
file, it must appear as the first non-blank* line.
Package
must appear
as the first
line
B) If statements
an import statement
is included
in “non-blank”
a source code
of a source
if they
at non-blank*
all. If a public
class or
file, it code
mustfile
appear
asappear
the first
line.
interface is declared in a source file, then the source code file
C) If a main( ) method is included in a source code file,
must
takeappear
the name
of the
class orline.
interface.
it must
as the
firstpublic
non-blank*
D) If a public interface is declared in a source code file,
it must have the same name as the source code file.
* a comment line is considered here as a blank line
From Java 2 Certification
Typical Short Answer Question
What is the output of the following program?
public class Question{
public static void main(String args[]) {
double d[] = new double[2];
int i[] = new int[3]; 0.0nullify
The default value of a double is 0.0.
Object
o[] = new Object[1];
The default value of an int is 0. Their sum is 0.0. (This arithmetic
System.out.print(d[0]
+ the
i[2]);
expression is evaluated and then
resulting value is cast to a string so
it is a valid argument to the print() method. In contrast, if we changed the
System.out.println(o[0]
+
"ify");
expression to the following, it would cease to be an arithmetic expression
} and would become a string expression: System.out.print(“ “ + d[0] + i[2]).
This would produce a different result.)
}
The default value of an Object is null. Adding “ify” gives “nullify”

Typical Short Answer Question
What is the output of the following program?
public class Question{
public static void main(String args[]) {

String s1 = “ab”;
String s2 = “abcd”;
String s3 = “cd”;
String s4 = s1 + s3;
s1 = s4;
System.out.print(“s1 ”+((s1 == s2)? “==” : “!=”)+“ s2”);
}
}
s1 != s2
From
Javato2different
Certification
Because s1 and
s2 refer
objects, s1 != s2 is true.
Typical Short Answer Question

What is the value of
9+8%7+6
16
Order of precedence requires that the expression be
evaluated as:
(9 + (8 % 7) + 6)
From Java 2 Certification
Typical Debugging Question
public int sum(int i[], Integer start, Integer end){
// Note:
can correct
code while still using the Integer wrapper class.
if(start
==Youend)
returnthei[start.intValue()];
// We removed it here only for clarity. If you don’t remove it, you need to
else// change start == end accordingly.
{ public int sum(int i[], int start, int end)
{
Integer
mid = new Integer((start.intValue() + end.intValue())/2);
if(start == end) return i[start];
elsemid1
return=sum(i,
(start+end)/2) + sum(i, ((start+end)/2)
+ 1, end);+1);
Integer
new start,
Integer((start.intValue()
+ end.intValue())/2
}
return
sum(i, start, mid) + sum(i, mid1, end);
}
}
This code is supposed to recursively sum the values of the
elements of integer array i. For each recursive call, the starting
and ending indices of the part of the array to be summed should
be in start and end, respectively. What is wrong with the code?
Typical Fill-in-the-Blanks (page 1)
public BufferedReader openInputFile(String public void
closeInputFile(BufferedReader
fileName)
input, String fileName)
{
{
BufferedReader input = null;
try {
try {
input.close();
InputStreamReader inStream = new
}
InputStreamReader(new
FileInputStream(fileName));
catch (IOException e) {
input = new BufferedReader(inStream);
System.out.println("Cannot close
"+ fileName);
}
}
catch (IOException e) {
}
System.out.println("Cannot open "+
fileName);
}
return input;
}
This code opens and closes a file whose name is given by the String fileName.
Typical Fill-in-the-Blanks (page 2)
public void readFile(String fileName)
{
BufferedReader inStream =
openInputFile(fileName);
boolean lineNotEmpty = true;
String newString = null;
public String
readFileLine(BufferedReader
inStream)
{
String newString = null;
try{
newString = inStream.readLine();
while (lineNotEmpty){
}
newString = readFileLine(inStream);
catch (IOException e) {
if (newString == null || newString.length()==0)
System.out.println("Cannot read
lineNotEmpty = false;
file line in readFileLine");
else
}
System.out.println("Just read:"+ newString);
return newString;
}
}
closeInputFile(inStream, fileName);
}
Fill in the blanks so that readFile( ) reads and prints out all the lines in the file.
Review: Part II
Advanced Java
Sampling of Topics

GUI components
(awt, Swing)
JPanel
JLabel
JButton JList
JFrame JScrollPane
Container

Threads
time
synchronization
creation, scheduling
mutual exclusion
states & transitions
synchronized methods &
blocks
Layout policy
 Exception handling
 Event handling
 RMI: remote interface proxy
 Event listeners
 One interface, multiple
 2D graphics
implementations
 Advanced data structures

ArrayList,
iterator
Review Questions

Typical multiple choice questions
 exception handling
 mutual exclusion
 RMI
 event handling

Typical short answer questions
 event handling
 threads
 graphics

Typical write-a-variation questions
 ArrayList
Typical fill-in-the-blanks question
 Typical code-from-scratch question

Typical Multiple Choice Question

Which of the following must be true of the object
thrown by a throw statement?
A) It must be assignable to the Throwable type.
B) It must be assignable to the Error type.
C) It must be assignable to the Exception type.
D) It must be assignable to the String type.
The object thrown by a throw statement must
be assignable to the Throwable type. This
includes the Error and Exception types.
From Java 2 Certification
Typical Multiple Choice Question

Which of the following are true?
A) Only threads have locks.
B) Classes have locks.
C) Primitive types have locks.
D) Only Runnable objects have locks.
Classes have locks, but primitive types do not.
From Java 2 Certification
Typical Multiple Choice Question

Which of the following are used by Java RMI?
A) stubs
B) skeletons
C) ORBs
D) IIOP
RMI makes use of stubs and skeletons.
ORBs and IIOP are used within CORBA.
[Note: Java 2 does not require skeletons]
Based on Java 2 Certification
Typical Multiple Choice Question

What is the preferred way to handle an object’s
events in Java 2?
A) Override the object’s handleEvent() method.
B) Add one or more event listeners to handle the events.
C) Have the object override its processEvent() methods.
D) Have the object override its dispatchEvent()
methods.
The event-delegation model uses event
listeners to handle events.
From Java 2 Certification
Typical Short Answer Question

Supposed you want to have an object eh handle
the TextEvent of a TextArea object t. How should
you add eh as the event handler for it?
t.addTextListener(eh);
You must invoke the TextArea object’s
addTextListener( ) method and pass it a reference to
the event handler.
From Java 2 Certification
Typical Short Answer Question
 What is the output of the following program?
public class Question{
public static void main(String args[]) {
MyThread t = new MyThread();
t.displayOutput(“t has been created.”);
t.start();
}
}
class MyThread extends Thread{
public void displayOutput(String s) {
t has been created.
System.out.println(s);
}
t is running.
public void run() {
displayOutput(“t is running.”);
}
From Java 2 Certification
}
Typical Short Answer Question:
What is the output of the following program?
Typical Short Answer
Typical Short Answer Question:
What is the output of the following program?
Typical Short Answer
Typical Coding Variation Question:
Modify this code so it removes Color objects
from the list but not Strings
continued on next slide...
Typical Coding Variation Question:
Modify this code so it removes Color objects
from the list but not Strings
ArrayList:
java.awt.Color[r=255,g=0,b=255] red white blue
java.awt.Color[r=0,g=255,b=255]
ArrayList after calling removeStrings:
java.awt.Color[r=255,g=0,b=255] java.awt.Color[r=0,g=255,b=255]
Typical Coding Variation Answer
continued on next slide...
Typical Fill-in-the-Blanks Question

RMI: making a service into a service+client
for transformation from 2-tier to 3-tier.
Remote Interface
NextNumber.java
Client using Service
NextNumberClient.java
NextNumberImpl.java
Service implementing Remote Interface
Typical Fill-in-the-Blanks Question
(continued)
Remote Interface
NextNumber.java
// A remote interface for getting the next number
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface NextNumber extends Remote {
public int getNextNumber(int n) throws RemoteException;
}
Typical Fill-in-the-Blanks Question
NextNumberImpl.java
Service implementing Remote Interface
(continued)
Typical Fill-in-the-Blanks Question
(continued)
NextNumberImpl.java
Service implementing Remote Interface
Typical Fill-in-the-Blanks Question
Client using Service
NextNumberClient.java
(continued)
Typical Fill-in-the-Blanks Question
Client using Service
NextNumberClient.java
(continued)
Typical Fill-in-the-Blanks Question
(continued)

Fill-in-the-blanks so that the next number
service obtains the next number from a secret
service instead of just adding one to the number.
You may assume that the interface of the secret
service is:
// A remote secret interface for getting the next number
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Secret extends Remote {
public int getSecretNextNumber(int n) throws RemoteException;
}
Typical Fill-in-the-Blanks Question
(continued)
Typical Fill-in-the-Blanks Question
(continued)
Typical Fill-in-the-Blanks Question
(continued)
Typical Code-from-Scratch Question

Write code to simulate n bank patrons using
safe deposit boxes.
 Assume
bank patron i already is assigned safe
deposit box i.
 Only 1 box in the bank is accessible at a time.
 To access a box, patron needs his/her key and
the bank's key for their box.
 Assume patron already has his/her key, so we
don't model that.
 However, they need to get lock on bank key
and box.
Typical Code-from-Scratch Question
(continued)
Typical Code-from-Scratch Question
(continued)
Typical Code-from-Scratch Question
(continued)
Typical Code-from-Scratch Question
(continued)
Typical Code-from-Scratch Question
(continued)
Review: Part III
Jini
Sampling of Topics
Basic client, service
 HTTP servers
 Lookup service
 Matching services
 Interfaces/proxy
 Leasing
 Delegation/ 3rd party
delegation

RMI activation
 Remote events & event
model
 Federating communities
 Security
 Well-behaved service

Review Questions
Typical multiple choice questions
 Typical short answer questions
 Typical fill-in-the-blanks question

Typical Multiple Choice Question

Which statement is true about a well-behaved Jini
service? (assume that each request for a serviceID returns a unique serviceID)
A) Each time it renews its lease with a Lookup Service it
must use the same serviceID.
B) Each time it executes in the same JVM it uses the same
serviceID.
C) Each time it executes in a different JVM it may use a
different serviceID.
D) All of the above
E) None of the above
F) A and B
G) B and C
H) A and C
Typical Multiple Choice Question

Which statement is true about RMI Activation?
A) ItA must
be used to implement a Jini service whenever
is not true because there are other options for
the
service has aa back-end,
back-end.such as RMI without
implementing
activation. an object is activated a new JVM is
B) Whenever
created
which
the object
will in
execute.
B is notintrue
because
2 objects
the same
activation
groupclass
may must
sharedirectly
the same
JVM. the class
C) An
activatable
extend
UnicastRemoteObject.
C is not true because an activatable class directly
extends
Activatable,
which,
in turn,
extends and
D) The
rmiregistry
handles
object
activation
UnicastRemoteObject.
deactivation.
is not
E) ADand
C true. The rmiregistry supports RMI. The
RMI
F) All
ofactivation
the abovedaemon supports activation.
G) None of the above
Typical Multiple Choice Question

Which of the following is a difference between a
Jini proxy and an RMI stub?
A is notcode
true because
bothto
a the
Jini client,
proxy’sbut
code
A) A Jini proxy’s
is delivered
an
andcode
RMI is
stub’s
code are delivered
to the
RMI stub’s
not delivered
to the client.
client.
B) A security
manager is required in order to remotely use
a Jini proxy,
but is not required in order to remotely use
B is false because a security manager is
an RMI stub.
required in both cases to remotely download
C) An RMIcode.
stub uses RMI for client-to-service
communication, whereas a Jini proxy may use other
means ofDclient-to-service
communication
. be
is false. Marshalled
objects need not
D) A Jini proxy
can use reconstituted
marshalled objects,
but an
RMI
automatically
upon arrival
and
stub can may
onlybe
useused
serialized
in bothobjects.
cases.
E) All of the above
F) None of the above
Typical Short Answer Question(s)

Suppose you’re given this Jini configuration:
 Subnet A contains:
LUS1, S1, S2, C1
 Subnet B contains: LUS2, LUS3, S3, C2, C3
 URLs of LUSs:
 LUS1: jini://host.testA.org
 LUS2: jini://host1.testB.org
 LUS3: jini://host2.testB.org
 Guaranteed message delivery
no network failures
 UDP multicast messages reach entire subnet

 Infinite
leases
Typical Short Answer Question (cont.)





S1, S2, each use multicast discovery to find lookup
services and join their communities. Each has a
DiscoveryListener.
S3 uses unicast discovery to jini://host1.testB.org and
jini://host.testA.org without a DiscoveryListener.
C1, C2, each use multicast discovery to find lookup
services and look for services. Each has a
DiscoveryListener.
C3 uses unicast discovery to jini://host2.testB.org and
jini://host.testA.org without a DiscoveryListener.
C3 registers for remote service discovery events from the
lookup services that it discovers.
Typical Short Answer Question (cont.)





All matching is done based only on interfaces.
S1, S3 each implement the BankWithdrawal interface.
S2 implements the BankDeposit interface.
C1, C3’s service matching templates each match based
on the BankWithdrawal interface.
C2’s service matching template matches based on the
BankDeposit interface.
Typical Short Answer Question (cont.)

S1 registers with LUS1.
Given the following sequence of steps, which services
S2 registers with LUS1.
does
each client discover?
 (Note: Assume
that each LUS2.
step in the sequence executes completely
S3 registers
with LUS1,
before the start of the next. If any conditions are associated with the
C1 discovers
If S1 registers with LUS1 before
discovery, LUS1.
state them.)
C1
discovers LUS1, then C1 discovers S1. If S3
 LUS1, LUS2 and LUS3 start up, along with their rmids and
registers
with LUS1 before C1 discovers LUS1, then
http servers.
C1 discovers S3.
 S1, S2, S3 start up, along with their http servers
C2
discovers
no services.
 C1,
C2, C3 start.
C3 discovers LUS1, LUS3. C3 discovers S1, S3.
Typical Fill-in-the-Blanks

Suppose you’re given S1, S2, S3, S4 where:

S1 implements the BankWithdrawal interface




S2 implements the BankDeposit interface




has serviceID = 2
has Currency attribute = US Dollars and Canadian Dollars
has Concurrency attribute = single-user
S3 implements the BankWithdrawal interface




has serviceID = 1
has Currency attribute = US Dollars and Japanese Yen
has Concurrency attribute = single-user
has serviceID = 3
has Currency attribute = US Dollars and Canadian Dollars
has Concurrency attribute = multi-user
S4 implements the BankDeposit interface



has serviceID = 4
has Currency attribute = US Dollars and European Euro
has Concurrency attribute = multi-user
Typical Fill-in-the-Blanks (cont.)


If a client wants to lookup a multi-user BankWithdrawal
service for US dollars, fill in the blanks for all the
different ServiceTemplates that will match the service:
serviceID
ServiceTypes
Attributes
3
null
null
BankWithdrawal
3
3
BankWithdrawal
null
3
BankWithdrawal
null
Currency= US Dollars
Concurrency= multi-user
null
Currency= US Dollars
Concurrency= multi-user
Currency= US Dollars
Concurrency= multi-user
Matching Review
Client Side: Matching Template
Client Template T matches a service X’s ServiceItem if:
serviceIDs match:
T’s serviceID = null
T’s serviceID = X’s serviceID
or
and
serviceTypes match:
T’s serviceType = null
or
X is instance or
subtype of each type
in T’s serviceType
and
attributes match:
T’s attributes = null
or
X’s attributes
contain >=1 match for
each attribute in T