Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 23: Review Mon. 12/11.

Similar presentations


Presentation on theme: "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 23: Review Mon. 12/11."— Presentation transcript:

1 UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 23: Review Mon. 12/11

2 Course Grading ä ä Homework40% ä ä Exam 115% (closed book) ä ä Exam 220% (open book, notes ) ä ä Exam 325% (open book, notes) Results are scaled if necessary.

3 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.

4 Text/Chapter Coverage ä Basic Java (Deitel): ä Chapters: 1-10, 14, 17 ä Advanced Java (Deitel): ä Chapters: 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.

5 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

6 Review: Part I Basic Java

7 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

8 Polymorphism is for Methods A::foo()B::foo()B::foo()a.x=1b.x=2((A)b).x=1 A B x=2foo() x=1foo() AB Thanks Jenn for this example

9 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. C) It can only be instantiated in the enclosing class. D) It must be final, if it is declared in a method scope. E) It can access private instance variables in the enclosing object. From Java 2 Certification An object of a non-static inner class is able to access private variables of the outer class in which it is defined.

10 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. B) If an import statement is included in a source code file, it must appear as the first non-blank* line. C) If a main( ) method is included in a source code file, it must appear as the first non-blank* line. D) If a public interface is declared in a source code file, it must have the same name as the source code file. From Java 2 Certification Package statements must appear as the first “non-blank” line of a source code file if they appear at all. If a public class or interface is declared in a source file, then the source code file must take the name of the public class or interface. * a comment line is considered here as a blank line

11 Typical Short Answer Question ä What is the output of the following program? public class Question{ public static void main(String args[]) { public static void main(String args[]) { double d[] = new double[2]; double d[] = new double[2]; int i[] = new int[3]; int i[] = new int[3]; Object o[] = new Object[1]; Object o[] = new Object[1]; System.out.print(d[0] + i[2]); System.out.print(d[0] + i[2]); System.out.println(o[0] + "ify"); System.out.println(o[0] + "ify"); }} 0.0nullify 0.0nullify The default value of a double is 0.0. The default value of an int is 0. Their sum is 0.0. (This arithmetic expression is evaluated and then the resulting value is cast to a string so it is a valid argument to the print() method. In contrast, if we changed the 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”

12 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[]) { public static void main(String args[]) { String s1 = “ab”; String s1 = “ab”; String s2 = “abcd”; String s2 = “abcd”; String s3 = “cd”; String s3 = “cd”; String s4 = s1 + s3; String s4 = s1 + s3; s1 = s4; s1 = s4; System.out.print(“s1 ”+((s1 == s2)? “==” : “!=”)+“ s2”); System.out.print(“s1 ”+((s1 == s2)? “==” : “!=”)+“ s2”); }} s1 != s2 s1 != s2 Because s1 and s2 refer to different objects, s1 != s2 is true.

13 Typical Short Answer Question ä What is the value of 9 + 8 % 7 + 6 16 16 Order of precedence requires that the expression be evaluated as: (9 + (8 % 7) + 6) From Java 2 Certification

14 Typical Debugging Question public int sum(int i[], Integer start, Integer end){ if(start == end) return i[start.intValue()]; if(start == end) return i[start.intValue()]; else else { Integer mid = new Integer((start.intValue() + end.intValue())/2); Integer mid = new Integer((start.intValue() + end.intValue())/2); Integer mid1 = new Integer((start.intValue() + end.intValue())/2 +1); Integer mid1 = new Integer((start.intValue() + end.intValue())/2 +1); return sum(i, start, mid) + sum(i, mid1, end); return sum(i, start, mid) + sum(i, mid1, end); } } // Note: You can correct the code while still using the Integer wrapper class. // We removed it here only for clarity. If you don’t remove it, you need to // change start == end accordingly. public int sum(int i[], int start, int end) { if(start == end) return i[start]; else return sum(i, start, (start+end)/2) + sum(i, ((start+end)/2) + 1, 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?

15 Typical Fill-in-the-Blanks (page 1) public BufferedReader openInputFile(String fileName) { BufferedReader input = null; BufferedReader input = null; try { try { InputStreamReader inStream = new InputStreamReader(new FileInputStream(fileName)); InputStreamReader inStream = new InputStreamReader(new FileInputStream(fileName)); input = new BufferedReader(inStream); input = new BufferedReader(inStream); } catch (IOException e) { catch (IOException e) { System.out.println("Cannot open "+ fileName); System.out.println("Cannot open "+ fileName); } return input; return input; } This code opens and closes a file whose name is given by the String fileName. public void closeInputFile(BufferedReader input, String fileName) public void closeInputFile(BufferedReader input, String fileName) { try { try { input.close(); input.close(); } catch (IOException e) { catch (IOException e) { System.out.println("Cannot close "+ fileName); System.out.println("Cannot close "+ fileName); } }

16 Typical Fill-in-the-Blanks (page 2) Fill in the blanks so that readFile( ) reads and prints out all the lines in the file. public String readFileLine(BufferedReader inStream) { String newString = null; String newString = null; return newString; } public void readFile(String fileName) public void readFile(String fileName) { BufferedReader inStream = openInputFile(fileName); BufferedReader inStream = openInputFile(fileName); newString = readFileLine(inStream); newString = readFileLine(inStream); closeInputFile(inStream, fileName); closeInputFile(inStream, fileName);} boolean lineNotEmpty = true; boolean lineNotEmpty = true; String newString = null; String newString = null; while (lineNotEmpty){ while (lineNotEmpty){ if (newString == null || newString.length()==0) if (newString == null || newString.length()==0) lineNotEmpty = false; lineNotEmpty = false; else else System.out.println("Just read:"+ newString); System.out.println("Just read:"+ newString); } try{ try{ newString = inStream.readLine(); newString = inStream.readLine(); } catch (IOException e) { catch (IOException e) { System.out.println("Cannot read file line in readFileLine"); System.out.println("Cannot read file line in readFileLine"); }

17 Review: Part II Advanced Java

18 Sampling of Topics ä ä GUI components (awt, Swing) - - JPanel JLabel - - JButton JList - - JFrame JScrollPane - - Container ä ä Layout policy ä ä Event handling ä ä Event listeners ä ä 2D graphics ä ä Advanced data structures - - ArrayList, iterator ä ä Threads - - time synchronization - - creation, scheduling - - mutual exclusion - - states & transitions - - synchronized methods & blocks ä ä Exception handling ä ä RMI: remote interface proxy ä ä One interface, multiple implementations

19 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

20 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. From Java 2 Certification The object thrown by a throw statement must be assignable to the Throwable type. This includes the Error and Exception types.

21 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. From Java 2 Certification Classes have locks, but primitive types do not.

22 Typical Multiple Choice Question ä Which of the following are used by Java RMI? A) stubs B) skeletons C) ORBs D) IIOP Based on Java 2 Certification RMI makes use of stubs and skeletons. ORBs and IIOP are used within CORBA. [Note: Java 2 does not require skeletons]

23 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. From Java 2 Certification The event-delegation model uses event listeners to handle events.

24 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); 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

25 Typical Short Answer Question ä What is the output of the following program? public class Question{ public static void main(String args[]) { public static void main(String args[]) { MyThread t = new MyThread(); MyThread t = new MyThread(); t.displayOutput(“t has been created.”); t.displayOutput(“t has been created.”); t.start(); t.start(); }} class MyThread extends Thread{ public void displayOutput(String s) { public void displayOutput(String s) { System.out.println(s); System.out.println(s); } public void run() { displayOutput(“t is running.”); public void run() { displayOutput(“t is running.”); }} t has been created. t is running. From Java 2 Certification

26 Typical Short Answer Question: What is the output of the following program?

27 Typical Short Answer

28 Typical Short Answer Question: What is the output of the following program?

29 Typical Short Answer

30 Typical Coding Variation Question: Modify this code so it removes Color objects from the list but not Strings continued on next slide...

31 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]

32 Typical Coding Variation Answer continued on next slide...

33 Typical Fill-in-the-Blanks Question ä RMI: making a service into a service+client for transformation from 2-tier to 3-tier. NextNumber.java Remote Interface NextNumberClient.java Client using Service NextNumberImpl.java Service implementing Remote Interface

34 Typical Fill-in-the-Blanks Question (continued) // 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; public int getNextNumber(int n) throws RemoteException;} NextNumber.java Remote Interface

35 Typical Fill-in-the-Blanks Question (continued) NextNumberImpl.java Service implementing Remote Interface

36 Typical Fill-in-the-Blanks Question (continued) NextNumberImpl.java Service implementing Remote Interface

37 Typical Fill-in-the-Blanks Question (continued) NextNumberClient.java Client using Service

38 Typical Fill-in-the-Blanks Question (continued) Client using Service NextNumberClient.java

39 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; public int getSecretNextNumber(int n) throws RemoteException;}

40 Typical Fill-in-the-Blanks Question (continued)

41

42

43 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.

44 Typical Code-from-Scratch Question (continued)

45

46

47

48

49 Review: Part III Jini

50 Sampling of Topics ä ä Basic client, service ä ä HTTP servers ä ä Lookup service ä ä Matching services ä ä Interfaces/proxy ä ä Leasing ä ä Delegation/ 3 rd party delegation ä ä RMI activation ä ä Remote events & event model ä ä Federating communities ä ä Security ä ä Well-behaved service

51 Review Questions ä Typical multiple choice questions ä Typical short answer questions ä Typical fill-in-the-blanks question

52 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

53 Typical Multiple Choice Question ä Which statement is true about RMI Activation? A) It must be used to implement a Jini service whenever the service has a back-end. B) Whenever an object is activated a new JVM is created in which the object will execute. C) An activatable class must directly extend the class UnicastRemoteObject. D) The rmiregistry handles object activation and deactivation. E) A and C F) All of the above G) None of the above A is not true because there are other options for implementing a back-end, such as RMI without activation. B is not true because 2 objects in the same activation group may share the same JVM. C is not true because an activatable class directly extends Activatable, which, in turn, extends UnicastRemoteObject. D is not true. The rmiregistry supports RMI. The RMI activation daemon supports activation.

54 Typical Multiple Choice Question ä Which of the following is a difference between a Jini proxy and an RMI stub? A) A Jini proxy’s code is delivered to the client, but an RMI stub’s code is not delivered to the client. B) A security manager is required in order to remotely use a Jini proxy, but is not required in order to remotely use an RMI stub. C) An RMI stub uses RMI for client-to-service communication, whereas a Jini proxy may use other means of client-to-service communication. D) A Jini proxy can use marshalled objects, but an RMI stub can only use serialized objects. E) All of the above F) None of the above A is not true because both a Jini proxy’s code and RMI stub’s code are delivered to the client. B is false because a security manager is required in both cases to remotely download code. D is false. Marshalled objects need not be automatically reconstituted upon arrival and may be used in both cases.

55 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

56 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.

57 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.

58 Typical Short Answer Question (cont.) ä Given the following sequence of steps, which services does each client discover? ä (Note: Assume that each step in the sequence executes completely before the start of the next. If any conditions are associated with the discovery, state them.) ä LUS1, LUS2 and LUS3 start up, along with their rmids and http servers. ä S1, S2, S3 start up, along with their http servers ä C1, C2, C3 start. S1 registers with LUS1. S1 registers with LUS1. S2 registers with LUS1. S3 registers with LUS1, LUS2. C1 discovers LUS1. If S1 registers with LUS1 before C1 discovers LUS1, then C1 discovers S1. If S3 registers with LUS1 before C1 discovers LUS1, then C1 discovers S3. C2 discovers no services. C3 discovers LUS1, LUS3. C3 discovers S1, S3.

59 Typical Fill-in-the-Blanks ä Suppose you’re given S1, S2, S3, S4 where: ä S1 implements the BankWithdrawal interface ä has serviceID = 1 ä has Currency attribute = US Dollars and Japanese Yen ä has Concurrency attribute = single-user ä 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 = 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

60 Typical Fill-in-the-Blanks (cont.) 3nullnull nullBankWithdrawalCurrency= US Dollars Concurrency= multi-user 3BankWithdrawalnull 3nullCurrency= US Dollars Concurrency= multi-user 3BankWithdrawalCurrency= US Dollars Concurrency= multi-user ä 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: ä serviceIDServiceTypesAttributes

61 Matching Review Client Side: Matching Template Client Template T matches a service X’s ServiceItem if: and and T’s serviceID = X’s serviceID T’s serviceID = null serviceIDs match: or X is instance or subtype of each type in T’s serviceType T’s serviceType = null serviceTypes match: or attributes match: or T’s attributes = null X’s attributes contain >=1 match for each attribute in T


Download ppt "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 23: Review Mon. 12/11."

Similar presentations


Ads by Google