1CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)Lecture 15:Review of Aliasing & Mutability, Floating Point CalculationsLast time:1.Unit testing and JUnit2.Constructors revisited3.equalsToday:1.Project #4 assigned2.Aliasing and Mutability3.Floating Point calculations4.Example class development: Rational NumbersCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)1Project #4 Is AssignedIt is due Wednesday, 10/31 at 11:00PMThe project is closedYou must complete the project by yourselfAssistance can only be provided by teaching assistants (TAs) and instructorsYou must not look at other students' codeStart now!Read entire assignment from beginning to end before starting to codeCheck out assignment now from CVSFollow the instructions exactlyCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)2Taking Care of Corner CasesFancyWord example from Friday String of “” was a corner case that we needed to test forWrite new test cases or new asserts in the test cases that already exist to take care of thisWhat about null references as corner cases?public void testNullAndEmpty(){FancyWord a = new FancyWord(null);assertEquals(null,a.toString());FancyWord b = new FancyWord("");assertEquals("",b.toString());}2CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)3What about Strings and Aliasing?String objects are immutable; fields cannot be changed once createdMutable objects: fields (values of instance variables) can be changed (e.g. Cat, Student, etc.)Immutable objects: fields (values of instance variables) cannot be changedSee String API: http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.htmlIn this example:y is created as an alias for String x b is created as an alias for Cat a but the results are different.x = Jan Planey = Jan================================Fluffy has been created!Fluffy has been eaten!Fluffy has 8 more lives...a = Fluffy ( 8 lives.)b = Fluffy ( 8 lives.)================================CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)4Which picture represents the current status of memory?StackcataHeapbcat“Fred”Heap HeapStackcatcatab“Fred”CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)5Floating Point CalculationsWhat will this print?public class SimpleMath {public static void main(String[] args) {if (3.9 - 3.8 == 0.1) {System.out.println("I am a very smart computer.");} else {System.out.println("I can't do simple arithmetic.");}}}I can’t do simple arithmetic. Why? Conversion of floating point to binary leads to precision errors! What can we do?3CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)6Floating Point Calculations (cont.)Two important rules:You can never use == to compare floating point values. Instead, check if two numbers are within a certain tolerance of each other.Never use floating point values to represent money, e.g., 3.52 to represent $3.52. Instead, use integer 352 to represent 352
View Full Document