9/29/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 13:Libraries and EncapsulatonLast time:1. for loops (from last lecture notes)2. Project (from last lecture notes)3. Static variables and methodsToday:1. Parameter passing2. Libraries3. Public vs. privateCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Parameters and Methods Recall that methods / constructors can have parametersDate (int newDay, int newMonth, int newYear) {day = newDay;month = newMonth;year = newYear;} What is printed by the following?int newYear = 2000;int badDay = 0Date d = new Date (badDay + 1, 2, newYear + 1);System.out.println (d);System.out.println (newYear); 2/1/20012000CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2How Does Java Evaluate Method / Constructor Calls?int newYear = 2000;int badDay = 0;Date d = new Date (badDay + 1, 2, newYear + 1);1. Arguments are evaluated using stack in effect at call site (place where method called) badDay + 1 evaluates to 1 2 evaluates to 2 newYear + 1 evaluates to 20012. Stack frame (temporary addition to stack) created to associate method parameters with values3. Stack frame put into stack4. Body of method executed in modified stack5. Stack frame removed from stackCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3daymonthyearseparator111900“/”2001Example Date (int newDay, intnewMonth, int newYear) { day = newDay;month = newMonth;year = newYear;} int newYear = 2000;int badDay = 0;Date d = new Date (badDay+ 1, 2, newYear + 1);System.out.println (d);System.out.println(newYear);HeapStacknewYear2000badDay 0dnewDay 1newMonth 2newYear 20012stackframeNote: when two variables in the stack have thesame name, Java uses the “most recent” oneCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Libraries in Java Library: implementation of useful routines that are shared by different programs Java mechanism for creating libraries: packages Package: group of related clases Example: java.util (contains Scanner class) To use a class from a package, you can use a fully qualified name (package name + class name):java.util.Scanner s = new java.util.Scanner(System.in); You can also import the class in the beginningimport java.util.Scanner; To import class in a package:import java.util.*;(Imports Scanner, other classes in package)CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5Package java.lang A special package containing widely used classes: String Math etc. java.lang.* is automatically imported by every Java programCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6Package Management A class can be added to a package by including:package <name of package>;in source file (usually very first line) The variables / methods provided by a class / package are often called its API (= Application Programmers Interface) APIs should be documented java.lang documentation:http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.htmlCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7String API ExampleString s = new String("CAT");String t = new String("cat");int i = s.length();boolean a = s.equals(t); // falseboolean b = s.equalsIgnoreCase(t); // trueint i = s.compareTo(t); // some negative numberint j = s.compareToIgnoreCase(t); // 0char c0 = s.charAt(0); // ‘C’char c1 = s.charAt(1); // ‘A’char c2 = s.charAt(2); // ‘T’String x = t.toLowerCase(); // Creates a NEW StringString y = s.toUpperCase(); // DittoCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Math API Example Math implements lots of mathematical functions Its members are staticdouble x = Math.abs(-33.7);double y = Math.sqrt(7);double pi = Math.PI;double c = Math.cos(2.3);double z = Math.pow(2.0, 3.0);double r = Math.random(); // between 0 and 1CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Public Declarations So far all classes / variables / methods have been public Keyword public used in declaration Every user of an object can access any public element Sometimes access should be restricted! To avoid giving object users unnecessary info (keep API small) To enforce consistency on instance variablesCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Example// "setMonth" allows the month to be set; an error is// reported if the month is invalidpublic void setMonth (int newMonth) {if ((1 <= newMonth) && (newMonth <= 12))month = newMonth;elseSystem.out.println ("Bad argument to setMonth: "+ newMonth);} Goal of setMonth: ensure month values are valid But month can still be made invalidDate d = new Date ();d.month = 13;CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Private Declarations Java also allows variables / methods to be declared private:private int month = 1; Private variables / members cannot be accessed outside the class definition Declaring instance variables private means they can only be modified using public methodsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Examplepublic class Date {private int day = 1;private int month = 1;private int year = 1900;private String separator = “/”;private String note;…}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland13What Should Be Public / Private?Class interface = API = public variables / methods Only make something public if there is a reason to Why? Encapsulation As long as interface is preserved, class can change without breaking other code The more limited the interface, the less there is to maintain Rule of thumb Make instance variables private Implement set / get methods Make auxiliary methods
View Full Document