Lecture 13 Libraries and Encapsulaton Last time 1 for loops from last lecture notes 2 Project from last lecture notes 3 Static variables and methods Today 1 Parameter passing 2 Libraries 3 Public vs private 9 29 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Parameters and Methods Recall that methods constructors can have parameters Date int newDay int newMonth int newYear day newDay month newMonth year newYear What is printed by the following int newYear 2000 int badDay 0 Date d new Date badDay 1 2 newYear 1 System out println d System out println newYear 2 1 2001 2000 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 How Does Java Evaluate Method Constructor Calls 1 2 3 4 5 int newYear 2000 int badDay 0 Date d new Date badDay 1 2 newYear 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 2001 Stack frame temporary addition to stack created to associate method parameters with values Stack frame put into stack Body of method executed in modified stack Stack frame removed from stack CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 Example Date int newDay int newMonth 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 Stack Heap newYear 2000 badDay 0 d newDay 1 stack frame newMonth 2 newYear Note when two variables in the stack have the same name Java uses the most recent one CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2001 1 day 2 1 month 1900 2001 year separator 3 Libraries 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 beginning import java util Scanner To import class in a package import java util Imports Scanner other classes in package CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Package java lang A special package containing widely used classes String Math etc java lang is automatically imported by every Java program CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Package 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 pack age summary html CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 String API Example String s new String CAT String t new String cat int i s length boolean a s equals t boolean b s equalsIgnoreCase t false true int i s compareTo t int j s compareToIgnoreCase t some negative number 0 char c0 s charAt 0 char c1 s charAt 1 char c2 s charAt 2 C A T String x t toLowerCase String y s toUpperCase Creates a NEW String Ditto CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 Math API Example Math implements lots of mathematical functions Its members are static double double double double double x Math abs 33 7 y Math sqrt 7 pi Math PI c Math cos 2 3 z Math pow 2 0 3 0 double r Math random CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland between 0 and 1 8 Public 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 variables CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Example setMonth allows the month to be set an error is reported if the month is invalid public void setMonth int newMonth if 1 newMonth newMonth 12 month newMonth else System out println Bad argument to setMonth newMonth Goal of setMonth ensure month values are valid But month can still be made invalid Date d new Date d month 13 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 Private 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 methods CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Example public class Date private int day 1 private int month 1 private int year 1900 private String separator private String note CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 What 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 private CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 13
View Full Document