Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm Programming 1 89-210 Exercise 1 Bar-Ilan University 2007-2008 תשס"ח by Moshe Fresko.

Similar presentations


Presentation on theme: "Algorithm Programming 1 89-210 Exercise 1 Bar-Ilan University 2007-2008 תשס"ח by Moshe Fresko."— Presentation transcript:

1 Algorithm Programming 1 89-210 Exercise 1 Bar-Ilan University 2007-2008 תשס"ח by Moshe Fresko

2 Exercise # 1 CompactInputStream, CompactOutputStream GOAL : To write a general purpose Output Stream class CompactOutputStream in order to write numbers into a stream as compact as possible. To write an Output Stream class CompactInputStream parallel to it in order to read from a stream that was written by CompactOutputStream. Testing application programs for both of these classes in order to see the functioning.

3 Exercise # 1 CompactInputStream, CompactOutputStream  The member variable, “ NumberOfBits ” in both classes will be responsible for keeping the number of bits to be written/read for each byte.  It can be a number between 1 and 32. In our exercises we will use it to be between 1 and 8.  There will be two public member functions for setting and getting this value. void setNumberOfBits(int); int getNumberOfBits(int);  Ctor of both classes will get a number and will set the “ NumberOfBits ”. The default value for it will be 8.  If NumberOfBits is 8, then it will act as regular InputStream/OutputStream.  Otherwise it will write/read as compact as possible.

4 Exercise # 1 CompactInputStream, CompactOutputStream  Example Let ’ s say we set the number of bits to be 3. We can write numbers ranging from 0 to 7 (2^3-1), since these are the numbers that can be written inside 3 bits. If we will write 8 numbers {0,3,5,7,1,2,3,5} then the number of total physical bits to be written to the Output must be 3*8 bits. (In total 3 physical bytes). So that in 3 bytes we can compactly encode 8 numbers. The bits to be written for the previous 8 numbers will look like a sequence of bits: 000 011 101 111 001 010 011 101 And the 3 bytes that will be written physically are: 00001110 11110010 10011101 (The direction of bits does not matter, you can change the direction).

5 Exercise # 1 CompactInputStream, CompactOutputStream COURSE OF ACTION : A.Define a class CompactOutputStream 1.It must be written in CompactOutputStream.java file. 2.It must be a decorator class from the Output Stream class hierarchy. So It must inherit from an appropriate class from page 13 of the IO pages. Please carefully decide which class to extend from. 3.Define the Constructor(s). Put the parameter(s) to the constructor(s) to be as general purpose as possible. (i.e. NO File Name as parameter). Note that, the constructor will not throw any Exception (Think why not?). 4.Define the function write(int i) syntactically the same as the function in OutputStream interface. a)It will write virtually one Number. Physically the bits will be written as we defined them in the previous page. The number in parameter int i may have the values 0..(2^N-1) where N is the number of bits as defined above. If it is any other number you must throw an appropriate exception. b)This function may throw any IOException. c)For physically writing it must call super.write(int i) function.

6 Exercise # 1 CompactInputStream, CompactOutputStream Cont. (define the CompactOutputStream class) 5.Define the functions write(byte[]arr) and write(byte[]arr,int off,int len) syntactically the same as OutputStream.write functions. a)write(byte[]arr) : will get a list of numbers in the form of a byte array. For the example we discussed, the first byte will be 0, second byte 3, third byte 5, etc. b)write(byte[]arr, int off, int len) : will get a list of numbers in the form of byte array, but will write only those numbers written in the bytes arr[off] … arr[off+len-1]. c)They must try to make the writing as optimized as possible. That is: do not use write(int) in a loop but try to use super ’ s write(byte[]) or super ’ s write(byte[],int,int) d)This optimization must be centralized, so that there will be no double coding. (Hint: One of them must call the other) 6.Define the function close() a)It will write the final byte(s) if there is a buffer. (it may call the flush() function ) b)Must close any open resources. Do it in “ Decorator ” way.

7 Exercise # 1 CompactInputStream, CompactOutputStream B.Define a class CompactInputStream (parallel to CompactOutputStream defined previously) 1.It must be written in CompactInputStream.java file. 2.It must be a decorator class from the Input Stream class hierarchy. So It must inherit from an appropriate class from page 13. Please carefully decide which class to extend from. 3.Define the Constructor(s). Put the parameter(s) to the constructor(s) to be as general purpose as possible. (i.e. NO File Name as parameter). Note that, the constructor will not throw any Exception (Think why not?). 4.Define the function read() syntactically the same as the function in InputStream interface. a)It will read virtually one number in the explained range (0..2^N-1). Physically the numbers will be read from the compact form written in CompactOutputStream. If it is the end of our sequence of bits, the return value is -1. b)This function may throw exception. c)For physically reading, it must call super.read()

8 Exercise # 1 CompactInputStream, CompactOutputStream Cont. (define the CompactInputStream class) 5.Define the functions read(byte[]arr) and read(byte[]arr,int off,int len) syntactically the same as InputStream.read functions. a)read(byte[]arr) : will fill a list of numbers in the form of a byte array. For example the first byte will be filled as 0, second byte as 3, third byte as 5, etc. for the previous example we saw. b)read(byte[]arr, int off, int len) : will get a list of bits in the form of byte array, but will fill only those bytes in arr[off] … arr[off+len-1]. c)They must try to make the reading as optimized as possible. That is: do not use read() in a loop but try to use super ’ s read(byte[]) or super ’ s write(read[],int,int) d)This optimization must be centralized, so that there will be no double coding. (Hint: One of them must call the other) 6.Define the function close() if needed … a)May close any open resources. If needed, do it in “ Decorator ” way.

9 Exercise # 1 CompactInputStream, CompactOutputStream C. APPLICATIONS TO TEST 1. Two testing application programs will be written, one for CompactInputStream and one for CompactOutputStream. They can be written in the main() method of these classes. 2. When running them, we will be able to put the following command prompt arguments. java CompactOutputStream TextFileName CompactFileName java CompactInputStream NUM CompactFileName TextFileName 3. The CompactFileName is in the format defined above. 4. The TextFileName is in the format a number per line. Where the first line gives the “ NumberOfBits ”. Each number in the other lines is in the range 0..2^N-1. 5. Example: The file A.txt has the following lines (one number per line, between each number there is a \n). 3 0 3 5 7 1 2 3 5 We run: java CompactOutputStream A.txt A.compact The file A.compact will have 3 bytes only as described above. Now we run: java CompactInputStream 3 A.compact B.txt The file B.txt will have 9 lines. It must look exactly as the A.txt file.

10 Exercise # 1 CompactInputStream, CompactOutputStream EXAMPLE USAGE for these classes: try { CompactOutputStream os = new CompactOutputStream ( new BufferedOutputStream ( new FileOutputStream(“MyFile.compact” ) ), 3 ) ; ### We want to write the numbers 0 3 5 7 1 2 3 5 # We don’t need to call os.setNumberOfBits(3), since we passed this number in CTOR os.write(0) ; os.write(3) ; os.write(5) ; byte[] arr = new byte[] { 7,1,2,3,5 } ; os.write(arr) ; os.close() ; # The file’s length is 3 bytes only. CompactInputStream is = new CompactInputStream ( new BufferedInputStream ( new FileInputStream(“MyFile.compact” ) ), 3 ) ; # We don’t need to call is.setNumberOfBits(3) System.println(is.read()) ; // prints 0 System.println(is.read()) ; // prints 3 byte[] a = new byte[6] ; is.read(a) ;// reads 5 7 1 2 3 5 into a and returns 6 if (is.read()<0) {// returns -1 System.out.println(“EOF”) ; // This line will be written } } catch (......... ) {............ }

11 Exercise # 1 CompactInputStream, CompactOutputStream  Important: You submit via submitex.  Course number: 89-210  Exercise: ex1  Two files will be delivered. CompactInputStream.java and CompactOutputStream.java It must compile/work under Java 1.4 / 1.5 / 1.6. You can try it on the Unix environment (on Sunshine). Do not write debugging information to the console or any other place. Write enough comments. Write wherever you think there is a need for the understanding of the code. Write your code according to the OOP principles.  Deadline: 29 Nov 2006 Thursday


Download ppt "Algorithm Programming 1 89-210 Exercise 1 Bar-Ilan University 2007-2008 תשס"ח by Moshe Fresko."

Similar presentations


Ads by Google