lecture slides online http www cse yorku caburtonteaching2009
lecture slides online http: //www. cse. yorku. ca/~burton/teaching/2009 W/1030. html 1
Goals for Today learn about preventing class instantiation learn what a utility is in Java learn about implementing methods static methods pass-by-value Javadoc 2
Puzzle 2 what does the following program print? public class Puzzle 02 { public static void main(String[] args) { final long MICROS_PER_DAY = 24 * 60 * 1000; final long MILLIS_PER_DAY = 24 * 60 * 1000; System. out. println(MICROS_PER_DAY / MILLIS_PER_DAY); } } 3
new Distance. Utility Objects our Distance. Utility API does not expose a constructor but Distance. Utility u = new Distance. Utility(); is legal if you do not define any constructors, Java will generate a default no-argument constructor for you 4
Preventing Instantiation our Distance. Utility API exposes only static constants (and methods later on) its state is constant there is no benefit in instantiating a Distance. Utility object a client can access the constants (and methods) without creating a Distance. Utility object double km. Per. Mi = Distance. Utility. KILOMETRES_PER_MILE; can prevent instantiation by declaring a private constructor 5
Version 2 (prevent instantiation) public class Distance. Utility { // attributes public static final double KILOMETRES_PER_MILE = 0. 621371192; // constructors // suppress default ctor for non-instantiation private Distance. Utility() {} } [notes 1. 2. 3] 6
Version 2. 1 (even better) public class Distance. Utility { // attributes public static final double KILOMETRES_PER_MILE = 0. 621371192; // constructors // suppress default ctor for non-instantiation private Distance. Utility() { throw new Assertion. Error(); } } [notes 1. 2. 3] 7
private attributes, constructors, and methods cannot be accessed by clients they are not part of the class API private attributes, constructors, and methods are accessible only inside the scope of the class a class with only private constructors indicates to clients that they cannot use new to create instances of the class 8
Utilities in Java, a utility class is a class having only static attributes and static methods uses: group related methods on primitive values or arrays group static methods for objects that implement an interface java. util. Collections [notes 1. 6. 1– 1. 6. 3] group static methods on a final class 9 java. lang. Math or java. util. Arrays more on this when we talk about inheritance
Version 3 (with methods) public class Distance. Utility { // attributes and constructors; see Version 2 or 2 a. . . // methods public static double kilometres. To. Miles(double km) { // unusual implementation? km /= KILOMETRES_PER_MILE; return km; } } 10
Methods public static double kilometres. To. Miles(double km) a method is a member that performs an action a method has a signature (name + number and types of the parameters) name number and types of parameters kilometres. To. Miles(double) all method signatures in a class must be unique a method returns a typed value or void double any client of a class can access a public method 11 public methods are part of the class API
Parameters sometimes called formal parameters for a method, the parameter names must be unique but a parameter can have the same name as an attribute (see [notes 1. 3. 3]) the scope of a parameter is the body of the method 12
static Methods a method that is static is a per-class member client does not need an object to invoke the method client uses the class name to access the method double miles = Distance. Utility. kilometres. To. Miles(100. 0); static methods are also called class methods a static method can only use static attributes of the class [notes 1. 2. 4], [AJ 249 -255] 13
Invoking Methods a client invokes a method by passing arguments to the method the types of the arguments must be compatible with the types of parameters in the method signature the values of the arguments must satisfy the preconditions of the method contract [JBA 2. 3. 3] double kilometres = 100. 0; arguments double miles = 0. 0; miles = Distance. Utility. kilometres. To. Miles(kilometres); 84 kilometres miles 14 100. 0
Pass-by-value with Primitive Types an invoked method runs in its own area of memory that contains storage for its parameters each parameter is initialized with the value of its corresponding argument miles = Distance. Utility. kilometres. To. Miles(kilometres); public static double kilometres. To. Miles(double km) 550 84 kilometres miles 15 100. 0 parameter km gets the value of argument kilometres km Distance. Utility. kilometres. To. Miles 100. 0
Pass-by-value with Primitive Types the argument kilometres and the parameter km have the same value but they are distinct variables when Distance. Utility. kilometres. To. Miles() changes the value of km the value of kilometres does not change miles = Distance. Utility. kilometres. To. Miles(kilometres); public static double kilometres. To. Miles(double km) { km /= KILOMETRES_PER_MILE; return km; } 550 84 kilometres miles 16 100. 0 kilometres does not change km Distance. Utility. kilometres. To. Miles 100. 0 160. 9. . . oops
Pass-by-value with Reference Types Java uses pass-by-value for primitive and reference types public class Doubler { // attributes and ctors not shown public static void twice(Rectangle x) { x. set. Width(2 * x. get. Width()); x. set. Height(2 * x. get. Height()); } } [notes 1. 3. 1 and 1. 3. 2] 17
Pass-by-value with Reference Types r = new Rectangle(3, 4); Doubler. twice(r); 64 r 500 3 6 height 4 8 x 18 value of r is a reference to the new Rectangle object width 600 see also [AJ 5. 2 (p 272 -282)] client Doubler. twice 500 parameter x gets the value of argument r (a reference)
Exercise for the Student suppose Doubler was incorrectly implemented like so: public class Doubler { // attributes and ctors not shown public static void twice(Rectangle x) { Rectangle y = new Rectangle(2 * x. get. Width(), 2 * x. get. Height()); x = y; } } draw the memory diagram for the previous slide using this version of Doubler 19
Pass-by-value Java uses pass-by-value for primitive and reference types 20 an argument of primitive type cannot be changed by a method an argument of reference type can have its state changed by a method
Testing a unit tests the smallest testable unit of code in object-oriented programming unit tests test methods public class Km. To. Miles. Test { public static void main(String[] args) { // 100 km == 62. 1371192 miles final double KM = 100. 0; final double MILES = 62. 1371192; final double TOLERANCE = 0. 000001; double test = Distance. Utility. kilometres. To. Miles(KM); if (Math. abs(test - MILES) > TOLERANCE) { System. out. println("test failed"); System. out. print("got " + test + expected " + MILES); } } } see also [notes 1. 2. 5] 21
Version 4 (Javadoc) 1 /** * The class <code>Distance. Utility</code> contains constants and * methods to convert between kilometres and miles. * * @author CSE 1030 Z */ public class Distance. Utility { /** * The number of kilometres in a mile. corrected */ public static final double KILOMETRES_PER_MILE = 1. 609344; 22
Version 4 (Javadoc) 2 /** * Converts distances in kilometres to miles. * * @param km The distance to convert. If <code>km</code> * is negative then the returned distance is * also negative. * @return Distance in miles. */ public static double kilometres. To. Miles(double km) { km /= KILOMETRES_PER_MILE; return km; } 23
Javadoc processes doc comments that immediately precede a class, attribute, constructor or method declaration doc comments delimited by /** and */ doc comment written in HTML and made up of two parts a description 1. block tags 2. 24 first sentence of description gets copied to the summary section only one description block; can use <p> to create separate paragraphs begin with @ (@param, @return, @exception) @pre. is non-standard (custom tag used in CSE 1030)
Javadoc Guidelines http: //java. sun. com/j 2 se/javadoc/writingdoccomments/index. html [notes 1. 5. 1, 1. 5. 2] [AJ p 318 -320] precede every exported class, interface, constructor, method, and attribute with a doc comment for methods the doc comment should describe the contract between the method and the client 25 preconditions ([notes 1. 4], [JBA 2. 3. 3], [AJ p 207]) postconditions ([notes 1. 4], [JBA 2. 3. 3], [AJ p 207])
- Slides: 25