DOC PREVIEW
UMD CMSC 131 - Lecture 15: Review of Aliasing & Mutability, Floating Point Calculations

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 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 AssignedIt is due Wednesday, 10/31 at 11:00PMThe project is closedYou must complete the project by yourselfAssistance can only be provided by teaching assistants (TAs) and instructorsYou must not look at other students' codeStart now!Read entire assignment from beginning to end before starting to codeCheck out assignment now from CVSFollow the instructions exactlyCMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)2Taking Care of Corner CasesFancyWord example from Friday String of “” was a corner case that we needed to test forWrite new test cases or new asserts in the test cases that already exist to take care of thisWhat 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());}CMSC 131 Fall 2007Jan Plane (adapted from Bonnie Dorr)3What about Strings and Aliasing?String objects are immutable; fields cannot be changed once created Mutable objects: fields (values of instance variables) can be changed (e.g. Cat, Student, etc.) Immutable objects: fields (values of instance variables) cannot be changed See String API: http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.htmlIn 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?CMSC 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

UMD CMSC 131 - Lecture 15: Review of Aliasing & Mutability, Floating Point Calculations

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 15: Review of Aliasing & Mutability, Floating Point Calculations
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 15: Review of Aliasing & Mutability, Floating Point Calculations and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 15: Review of Aliasing & Mutability, Floating Point Calculations 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?