Title: Chapter 5 Classes and Methods II
1Chapter 5Classes and Methods II
- Lecture Slides to Accompany
- An Introduction to Computer Science Using Java
(2nd Edition) - by
- S.N. Kamin, D. Mickunas, E. Reingold
2Chapter 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
3Building Classes with Multiple Methods
- Computer programs can be thought of using phases
- Input
- Computation
- Output
- Using separate methods for each phase can improve
the maintainability of a class or program
4Building Class Definitions
- public class classname
- // Author, data, explanation
- declarations of instance variables
- public void methodName1 (parameter)
- declarations of local variables
- executable statements with comments
-
- public void methodName2 (parameter)
- declarations of local variables
- executable statements with comments
-
5Instance Variables
- Local variables
- variables declared inside methods
- not accessible to any other method
- cannot be used for communication
- Instance variables
- declared outside the methods, but declared inside
the class - all class methods have access to the class
instance variables - can be used for communication inside class
6Initialization 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
7Hose Class Methods
- void getData()
- // Reads and stores the height and
- // weight data.
- void compute()
- // Computes and stores hose size.
- void display()
- // Displays the results of the
- // computation.
8UML Diagram for Hose Class
9Variable Scope Rules
- The scope of an instance variable is the entire
class body unless another identifier is found
with the same name. - The scope of a formal argument in a method header
is the entire method body. - The scope of a local variable in a method is from
the point of declaration to the end of the method
body. - 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.
10Scope Example
11Bad Variable Declarations
12Class Constructors with Arguments
- A constructor is a special method that is called
when an object is allocated. - We can write
- OutputBox out new OutputBox(A Title)
- Instead of
- OutputBox out new OutputBox()
- Out.setTitle(A Title)
- Writing constructors for programmer defined
classes will be discussed in Chapter 7.
13Return Types
- It is possible to have methods that have return
types other than void - Example
- public class Clock (
- int hour
- public int getHour ()
- return hour
-
14return 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
15Clock 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 lt 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
16Clock Class Outline
17Geometry of Clock Drawing
18Theta Calculations
- For the hour hand
- theta
- 2Math.PIminute/60.0
- For the minute hand
- theta
- 2Math.PI(hour minute)/60.0/12.0
19Drawing the Clock Hands
- Assuming (x, y) is the bottom vertex and
recalling that computer graphics coordinates are
upside down - x1 x (int) (rMath.sin(theta))
- y1 y - (int) (rMath.cos(theta))
- d.drawLine(x, y, x1, y1)
- For the hour hand use r.8 in place of r
20TwoClocks Client
- public class TwoClocksClient
- public static void main (String args)
- TwoClock twins new TwoClocks()
- twins.drawClocks()
- twins.compareClocks()
-
21Output from TwoClocks Client
22UML Class Diagram for Clock-DrawingBox
Composition