Chapter 5 - Classes and methods II. In this chapter we will: formally introduce the class construct as it is used in the Java language, discuss the use of instance variables to facilitate method communication, demonstrate the use of classes to improve program structure.
Trang 1
Chapter 5
Classes and Methods II
Lecture Slides to Accompany
An Introduction to Computer Science
Using Java (2nd Edition)
by S.N Kamin, D Mickunas, E Reingold
Trang 2
Chapter Preview
In this chapter we will:
• formally introduce the class construct as it is used in the Java language
• discuss the use of instance variables to
facilitate method communication
• demonstrate the use of classes to improve program structure
Trang 3• Using separate methods for each phase can
improve the maintainability of a class or
program
Trang 4
Building Class Definitions
public class classname {
// Author, data, explanation
declarations of instance variables
public void methodName1 (parameter) {
declarations of local variables
executable statements with comments
Trang 5
Instance Variables
• Local variables
– variables declared inside methods
– not accessible to any other method
– cannot be used for communication
Trang 6
Initialization of Instance Variables
• Instance variable declarations can contain
initializers just like local variables
• Unlike local variables, instance variables will
be initialized to default values if no initializers
are found
– integers and doubles are initialized to 0
– characters are initialized to the null
character (ASCII code 0)
– booleans are initialized to false
– object-type variables are initialized to the reference value null
Trang 8
UML Diagram for Hose Class
Trang 9
Variable Scope Rules
1 The scope of an instance variable is the entire class
body unless another identifier is found with the same name
2 The scope of a formal argument in a method header
is the entire method body
3 The scope of a local variable in a method is from the
point of declaration to the end of the method body
4 It is not legal to declare a variable within a method
using the same name as variable in the enclosing block in that method You cannot declare two
instance variables using the same name
Trang 10
Scope Example
Trang 11
Bad Variable Declarations
Trang 12
Class Constructors with
Arguments
• A constructor is a special method that is called
when an object is allocated.
• Writing constructors for programmer defined
classes will be discussed in Chapter 7.
Trang 14
return Statement
• The return statement
– allows a method to return a value to the caller– can appear any where in the method body
– can be conditionally executed
– results in immediate exit from a method when executed
• Form:
return expression;
Trang 15
Clock Class Methods
Example Explanation void setup() Initializes the clock
void getData() Reads and stores the hour and
minute data String toString() Returns string version of time
suitable for printing void setHour(int h) Sets hour to h
void setMinute(int m) Sets minute to m
int getHour() Returns value of hour
int getMinute() Returns value of minute
boolean priorTo(Clock c) Returns true is receiver < c void display
(DrawingBox d,
int x, int y, int r)
Draws the clock with center at (x,y) and radius r, in the DrawingBox referred to by d
Trang 16
Clock Class Outline
Trang 17
Geometry of Clock Drawing
Trang 19
Drawing the Clock Hands
• Assuming (x, y) is the bottom vertex and
recalling that computer graphics coordinates are upside down
Trang 20
TwoClocks Client
public class TwoClocksClient {
public static void main (String[] args){ TwoClock twins = new TwoClocks();
twins.drawClocks();
twins.compareClocks();
}
}
Trang 21
Output from TwoClocks Client
Trang 22
UML Class Diagram for Clock-DrawingBox Composition