CS 33600 Exam 1 Review The exam is on Wednesday, March 6. The exam is over the reading assignments and the code examples listed on the course web site. Here are a few review problems. Problem 1.) (a) A student wants to read integers from a buffered stream and writes the following code. What is wrong? BufferedInputStream bis = new BufferedInputStream( new DataInputStream( new FileInputStream( new File("input.txt")))); (b) A student wants to read integers from a buffered stream and writes the following code. What is wrong? InputStream is = new DataInputStream( new BufferedInputStream( new FileInputStream( new File("input.txt")))); Problem 2.) What do we mean when we say that a BufferedInputStream "is a" InputStream and that a BufferedInputStream "has a" InputStream? (Your answer should make use of proper Object-Oriented terminology.) How is this "is a" relationship used by Java? How is this "has a" relationship used by the BufferedInputStream? Problem 3.) Consider the following program (you can copy and paste it directly into the Java Visualizer). (a) If conditionalCompilation is set to true and the program's input is 1234, then explain in detail the program's output. (b) If conditionalCompilation is set to false and the program's input is 1234, then explain in detail the program's output. (c) If conditionalCompilation is set to true and the program's input is 000, then explain in detail the program's output. import java.util.Scanner; import java.io.DataInputStream; import java.io.IOException; public class IntegerData { public static void main(String[] args) throws IOException { // Set conditionalCompilation to true to use the DataInputStream, or // set conditionalCompilation to false to use the Scanner. final boolean conditionalCompilation = true; final int n; if (conditionalCompilation) { final DataInputStream in = new DataInputStream(System.in); n = in.readInt(); } else { final Scanner in = new Scanner(System.in); n = in.nextInt(); } System.out.println(n); } } Problem 4.) Explain in detail the output from this program. (You can copy and paste it directly into the Java Visualizer.) import java.util.Arrays; import java.io.UnsupportedEncodingException; public class StringToBytes { public static void main(String[] args) throws UnsupportedEncodingException { String s = "abcdefghij"; System.out.println(Arrays.toString( s.getBytes("US-ASCII") )); System.out.println(Arrays.toString( s.getBytes("UTF-16LE") )); System.out.println(Arrays.toString( s.getBytes("UTF-16BE") )); System.out.println(Arrays.toString( s.getBytes("UTF-16") )); } } Problem 5.) This problem is about Unicode's UTF-8 encoding. Given each of the following byte sequences, determine if the sequence is, 1) legal and complete, 2) legal but incomplete, 3) illegal. Briefly explain your answers. Use either of these two pictures of the UTF-8 encoding to help with your explanation. https://higheredbcs.wiley.com/legacy/college/horstmann/1119402972/app/appendices.pdf#page=7 or https://en.wikipedia.org/wiki/UTF-8#Encoding (a) 01000000 10000000 (b) 11000000 01000000 (c) 11000000 10000000 (d) 11110000 10000000 10000000 (e) 11100000 10000000 10000000 (f) 11100000 10000000 01000000 (g) 10000000 01000000 01000000 (h) 10000000 10000000 10000000 Problem 6.) What does an IP address identify? What does a port number identify? Problem 7.) What makes a computer into a "server"? Problem 8.) In the picture below, draw arrows to indicate in which direction data flows through the streams. Socket Socket +-------------+ +-------------+ | | tcp connection | | | IP address |O--------------------O| IP address | | Port number | | Port number | | | | | +-------------+ +-------------+ | | | | | | | | | | | | out in out in stream stream stream stream Problem 9.) The following picture is a picture of a TCP connection, but not yet a picture of a communicating TCP connection. Explain why. Also explain why this connection may stay in this state for a long time. Then draw a new picture so that the new picture describes the communicating TCP connection. ServerSocket Socket +--------------+ +----------------+ | | tcp connection | | | 16.52.9.4:75 |O--------------------O| 10.12.3.2:1234 | | | | | | | | | +--------------+ +----------------+ Problem 10.) Fill in as much detail as you can in this outline of a TCP client/server connection. Server +----------------+ | | | | | | +----------------+ | | | +----------------+ | | | | | | +----------------+ | | Client | +-------------+ +----------------+ | | | | | | | | | | | | +-------------+ +----------------+ | | | | | | +-------------+ +----------------+ | | | | | |--------------------| | | | | | +-------------+ +----------------+ | | | | | | +-------------+ +----------------+ | | | | | |--------------------| | | | | | +-------------+ +----------------+ | | | | | | +-------------+ +----------------+ | | | | | |--------------------| | | | | | +-------------+ +----------------+ | | | | | | +-------------+ +----------------+ | | | | | | | | | | | | +-------------+ +----------------+ Problem 11.) Write five lines of Java that creates a server socket, waits for a connection on that socket, reads one line from the connecting process, writes that line back to the process, and then closes the connection (but not the server socket). Problem 12.) How can one end of a TCP connection know that it is time to stop receiving data from its socket and to begin transmitting data using its socket? Problem 13.) Here is a screenshot of TCPView. How many servers does it show? How many clients does it show? Which clients are connected to which servers? http://cs.pnw.edu/~rlkraft/cs33600/for-class/TCPView_screenshot.png Problem 14.) Suppose we have three processes, P1, P2, and P3 that communicate using TCP. The processes are supposed to form a ring in which P1 can send messages to P2, P2 can send messages to P3, and P3 can send messages to P1, and the processes do not communicate in any other way. More specifically, suppose that when process P1 starts, it prompts a user at a console to type in a message, and then P1 sends that message to P2. P2 receives the message and prints it on its console and then prompts a user at the console to type a new message that P2 sends to P3. P3 receives its message, prints it to its console, prompts a user for a new message, and sends that to P1. P1 receives the message from P3, prints it to its console window, and prompts its user for a message to send to P2, etc. Write a Java pseudo-code outline of the steps that P1 must go through to implement this "ring protocol". How many sockets, and what kind of sockets, will P1 need? What information would P1 need at its startup time? Would the pseudo-code for processes P2 and P3 be the same or different than P1's? Problem 15.) This problem is about the "is a" and "has a" relationships used in Java. Complete the definitions of the two classes, B_is_a_A B_has_a_A so that the resulting program runs and produces the output shown below. class A { private int secret = (int)(10_000*Math.random()); public String method_1(){return "method 1 in A with " + secret;} public String method_2(){return "method 2 in A with " + secret;} } class B_is_a_A extends A { } class B_has_a_A { private A aRef; public B_has_a_A(A aRef){this.aRef = aRef;} } public class IsA_HasA_Tester { public static void main(String[] args) { B_is_a_A b1 = new B_is_a_A(); System.out.println( b1.method_1() ); System.out.println( b1.method_2() ); B_has_a_A b2 = new B_has_a_A(new A()); System.out.println( b2.method_1() ); System.out.println( b2.method_2() ); } } Complete the definitions of B_is_a_A and B_has_a_A so that the output from running the main() method looks like the following (there should always be the same number shown in the first two lines and the same number shown in the last two lines). method 1 in A with 1456 method 2 in A with 1456, and help from B method 1 in A with 3048 method 2 in A with 3048, and help from B