lecture slides online cse.yorku/~burton/teaching/2009W/1030.html
description
Transcript of lecture slides online cse.yorku/~burton/teaching/2009W/1030.html
![Page 1: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/1.jpg)
1
lecture slides onlinehttp://www.cse.yorku.ca/~burton/teaching/2009W/1030.html
![Page 2: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/2.jpg)
Goals for Today
2
learn about preventing class instantiation learn what a utility is in Java learn about implementing methods
static methods pass-by-value
Javadoc
![Page 3: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/3.jpg)
Puzzle 2
3
what does the following program print?
public class Puzzle02 { public static void main(String[] args) {
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);}
}
![Page 4: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/4.jpg)
new DistanceUtility Objects
4
our DistanceUtility API does not expose a constructor but
DistanceUtility u = new DistanceUtility();
is legal
if you do not define any constructors, Java will generate a default no-argument constructor for you
![Page 5: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/5.jpg)
our DistanceUtility API exposes only static constants (and methods later on) its state is constant
there is no benefit in instantiating a DistanceUtility object a client can access the constants (and methods)
without creating a DistanceUtility object
double kmPerMi = DistanceUtility.KILOMETRES_PER_MILE;
can prevent instantiation by declaring a private constructor
Preventing Instantiation
5
![Page 6: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/6.jpg)
Version 2 (prevent instantiation)
6
public class DistanceUtility
{// attributes
public static final double KILOMETRES_PER_MILE = 0.621371192;
// constructors
// suppress default ctor for non-instantiation
private DistanceUtility()
{}
}
[notes 1.2.3]
![Page 7: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/7.jpg)
Version 2.1 (even better)
7
public class DistanceUtility
{// attributes
public static final double KILOMETRES_PER_MILE = 0.621371192;
// constructors
// suppress default ctor for non-instantiation
private DistanceUtility()
{
throw new AssertionError();
}
}
[notes 1.2.3]
![Page 8: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/8.jpg)
private
8
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
![Page 9: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/9.jpg)
Utilities
9
in Java, a utility class is a class having only static attributes and static methods
uses: group related methods on primitive values or
arrays java.lang.Math or java.util.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 more on this when we talk about inheritance
![Page 10: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/10.jpg)
Version 3 (with methods)
10
public class DistanceUtility
{// attributes and constructors; see Version 2 or 2a ...
// methods
public static double kilometresToMiles(double km)
{
// unusual implementation?
km /= KILOMETRES_PER_MILE;
return km;
}
}
![Page 11: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/11.jpg)
Methods
11
public static double kilometresToMiles(double km)
a method is a member that performs an action a method has a signature (name + number and
types of the parameters)
kilometresToMiles(double) all method signatures in a class must be unique
a method returns a typed value or voiddouble
any client of a class can access a public method public methods are part of the class API
name number and types of parameters
![Page 12: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/12.jpg)
Parameters
12
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
![Page 13: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/13.jpg)
static Methods
13
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 = DistanceUtility.kilometresToMiles(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]
![Page 14: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/14.jpg)
Invoking Methods
14
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;double miles = 0.0;miles = DistanceUtility.kilometresToMiles(kilometres);
arguments
84
kilometres 100.0
miles 0.0
![Page 15: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/15.jpg)
Pass-by-value with Primitive Types
15
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
550 DistanceUtility.kilometresToMiles
km
84
kilometres 100.0
miles 0.0
100.0parameter kmgets the value of
argumentkilometres
miles = DistanceUtility.kilometresToMiles(kilometres);
public static double kilometresToMiles(double km)
![Page 16: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/16.jpg)
Pass-by-value with Primitive Types
16
the argument kilometres and the parameter km have the same value but they are distinct variables when DistanceUtility.kilometresToMiles()
changes the value of km the value of kilometres does not change
550 DistanceUtility.kilometresToMiles
km
84
kilometres 100.0
miles 0.0
100.0kilometresdoes notchange
miles = DistanceUtility.kilometresToMiles(kilometres);
public static double kilometresToMiles(double km) { km /= KILOMETRES_PER_MILE; return km;}
160.9...
oops
![Page 17: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/17.jpg)
Pass-by-value with Reference Types
17
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.setWidth(2 * x.getWidth());
x.setHeight(2 * x.getHeight());
}
}
[notes 1.3.1 and 1.3.2]
![Page 18: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/18.jpg)
Pass-by-value with Reference Types
18
r = new Rectangle(3,4);
Doubler.twice(r);
64 client
r
500 Rectangle object
width 3
height 4
600 Doubler.twice
x
500
500
value of r is a reference to the
newRectangle
object
parameter x gets the valueof argument r(a reference)
6
8
see also [AJ 5.2 (p 272-282)]
![Page 19: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/19.jpg)
Exercise for the Student
19
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.getWidth(),
2 * x.getHeight());
x = y;
}
}
draw the memory diagram for the previous slide using this version of Doubler
![Page 20: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/20.jpg)
Pass-by-value
20
Java uses pass-by-value for primitive and reference types an argument of primitive type cannot be changed
by a method an argument of reference type can have its state
changed by a method
![Page 21: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/21.jpg)
Testing
21
a unit test tests the smallest testable unit of code in object-oriented programming unit tests test methods
public class KmToMilesTest {
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 = DistanceUtility.kilometresToMiles(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]
![Page 22: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/22.jpg)
Version 4 (Javadoc) 1
22
/**
* The class <code>DistanceUtility</code> contains constants and
* methods to convert between kilometres and miles.
*
* @author CSE1030Z
*/
public class DistanceUtility
{
/**
* The number of kilometres in a mile.
*/
public static final double KILOMETRES_PER_MILE = 1.609344;
corrected
![Page 23: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/23.jpg)
Version 4 (Javadoc) 2
23
/**
* 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 kilometresToMiles(double km)
{
km /= KILOMETRES_PER_MILE;
return km;
}
![Page 24: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/24.jpg)
Javadoc
24
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 parts1. a description
first sentence of description gets copied to the summary section
only one description block; can use <p> to create separate paragraphs
2. block tags begin with @ (@param, @return, @exception) @pre. is non-standard (custom tag used in CSE1030)
![Page 25: lecture slides online cse.yorku/~burton/teaching/2009W/1030.html](https://reader035.fdocuments.net/reader035/viewer/2022062806/56814efd550346895dbc8c62/html5/thumbnails/25.jpg)
Javadoc Guidelines
25
http://java.sun.com/j2se/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 preconditions ([notes 1.4], [JBA 2.3.3], [AJ p 207]) postconditions ([notes 1.4], [JBA 2.3.3], [AJ p 207])