110 likes | 250 Views
Serializable Objects. Serializable objects Directly read/write from/to disk with a single statement Convert from/to a sequence of bytes when read/write implements Serializable needs to be on the class signature line
E N D
Serializable Objects • Serializable objects • Directly read/write from/to disk with a single statement • Convert from/to a sequence of bytes when read/write • implements Serializable needs to be on the class signature line • The package java.io.Serializable needs to be available. The statement import java.io.*; suffices • Most, but not all, classes in the java class library are serialized • Sub classes referenced in a serialized class must also be serialized • Transientvariables • Ex: private transient int x; declares the integer x to be transient. • Transient variable are not read or written with the object
Reading and Writing Objects • Before writing objects to sequential disk files, instantiate:FileOutputStream fos = newFileOutputSteam(“pathname”);BufferedOutputStream bos = new BufferedOutputStream(fos);ObjectOutputStream oos = new ObjectOutputStream(bos); • Before reading objects from sequential disk files, instantiate:FileInputStream fis = newFileInputSteam(“pathname”);BufferedInputStream bis = new BufferedInputStream(fis);ObjectInputStream ois = new ObjectInputStream(bis); • To Write the object: oos.writeObject( objectVar ); • To Read the object: objectVar = (objectType)ois.readObject(); • To close the streams: ois.close(); or oos.close(); • Catch exceptions such as IOException, FileNotFoundException
Bitwise Operations • Assume: • x is a byte with its eight bits set to 00010101 • y is a byte with its eight bits set to 11110011 • Then • x & y results in 00010001 (a bit is on [set to 1] if both of the matching bits are on) • x | y results in 11110111 (a bit is set [value of 1] if either of the matching bits are on. • x ^ y results in 11100110 (a bit is set if either but not both of the matching bits are set) • x <<3 results in 10101000 (x bits shift left three positions) • y >>> 3 results in 00011110 (y bits shift right three positions) • y >> 3 results in 11111110 (y bits shift right. The leftmost bit (1) repeats) • x>>3 results in 00000010 (x bits shift right. The leftmost bit (0) repeats) • ~x results in 11101010 (change zeroes to ones and ones to zeroes)
Why bits? • Embedded controllers and device drivers need to communicate with devices using bitwise operations • Significant storage savings are possible with some applications • Human DNA has billions of letters (ACTG). Each letter can store in two bits. A byte for each character would require eight times the storage. • Multimedia applications store data in bits. • Sometimes bit operations are faster than using floating point multiplication and division • Databases and operating systems rely heavily on bit manipulation • Encryption algorithms use bit operations Note:Use bit operations only with integer types (int, short, character, long)
Converting binary to decimal • Assume: • x is a byte with its eight bits set to 0010101 • y is a byte with its eight bits set to 1110011 • What display? • System.out.println(x) displays 21 (20 + 22 + 24 = 1 + 4 + 16) • System.out.println(y) displays -13 (see bullets that follow) • Negative numbers (leftmost bit set to one) • Most computers store negative numbers in twos complement format • Twos complement: flip (1->0 and 0->1) all bits and then add one • Complementing 1110011 gives 0001100. • Add one to 0001100 gives a twos complement value of 0001101 • In decimal, 0001101 is 20 + 22 + 23 = 1 + 4 + 8 = 13. • Because the number was negative, the original value is -13. • Shortcut: flip the binary digits to the left of the least significant one digit. • Remember the carry with binary addition • 1 + 1 is 0 with a carry of 1 to the next digit (there is no 2 digit) • 11111111 + 00000001 = 00000000 ((-1) + (+1) -> 0)
Converting Decimal to Binary • Successively divide by two, using the remainder for the binary digit • Example (49) • 49%2 = 1; 49/2 = 24 • 24%2 = 0; 24/2 = 12 • 12%2 = 0; 12/2 = 6 • 6%2 = 0; 6/2 = 3 • 3%2 = 1; 3/2 = 1 • 1%2 = 1; 1/2 = 0 • Answer: • Copy the binary remainder digits from bottom to top where the bottom is the most significant digit of the answer and the top is least significant digit of the answer. • Above example: 110001
Operations on Bits • We can store M-MDDY-YH-M-M in 32 bits (type int) • Fields • MM (4 bits – bits 0-3) • DD (5 bits – bits 4-8) • YYYY (11 bits – bits 9-19) if YYYY = year – 1976 • HH (5 bits – bits 20-24) • MM (7 bits – bits 25-31) • Bit Mask • an integer with ones in field positions and zeroes elsewhere • int hh = 31<<20 is the mask for HH • int yyyy = 2047<<9 is the mask for YYYY • long field = 3L<<35 is the mask for a 2 bit field starting at bit 35 of a long • Questions • What are the biggest numbers that can fit in each of the above fields? • Why is the 'L' needed in the expression 3L<<35?
Bitwise operations using masks Assume that 'int date' creates the variable with a date • Set the year to 2007 • date &= ~(2047<<9); // clear the field • date |= (2007<<9); // set the new values • Clear the hour field • date &= ~(31<<20); • Toggle a bit (value ^= 1<<5; toggles bit 5) • Multiply using bits • x << 3 multiplies by 23 = 8. • x + x<<1 + x<<2 multiplies by 1 + 21 + 22 = 7 • Divide using bits • y>>2 divides by 4, handling positive and negative numbers • y>>>2 divides by 4, but only handles positive numbers
Software EngineeringSystem: combining software, hardware, and people to solve a problem • Life Cycle: Conceive, Develop, Release, Use and Maintain, Rewrite. The maintenance phase is about 80 percent of the total • The time a system remains in use depends upon: • The effectiveness of the original design • The quality of the documentation • The care taken when the system is upgraded • Great care should be made in the design • Completed in phases with occasional backtracking • High-level functional (architectural) specification • Refined detailed specifications (classes, methods, algorithms, initialization, pseudo-codes, dataflow, user interface) • Evolutionary design refines by focusing on single areas • Throw away prototypes (for feasibility tests)
Quality Assurance • Quality assurance • Design review (walkthroughs), unit tests, integration, test suites, alpha-testing, beta-testing • Unit tests • Black-box testing (check the data) • Glass-box testing (force every path to be taken) • Assertions (internal program checking) • Techniques • Print statements • Breakpoints, watch variables, single stepping • Tracing
Case Study • Automated testing programs are in high demand. They allow tests to be taken at times convenient to the individual. • Some issues to consider: • Guarantee that the correct person is taking the test. • Dynamic group of questions • Where should the test be taken • Types and numbers of questions • Test bank • Availability of results • How general purpose • Throwing out of bad questions • Artificial intelligence • What steps do we need for a sound design? Lab 3: Create prototype to demonstrate automated true/false tests