chapter 5 classes and methods ii lecture slides to accompany an introduction to computer science...

22
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

Upload: martin-cunningham

Post on 20-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

Chapter 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

Page 2: 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

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

Page 3: 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

Building 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

Page 4: 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

Building Class Definitionspublic 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

}

}

Page 5: 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

Instance 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

Page 6: 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

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

Page 7: 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

Hose 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.

Page 8: 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

UML Diagram for Hose Class

Page 9: 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

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.

Page 10: 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

Scope Example

Page 11: 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

Bad Variable Declarations

Page 12: 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

Class Constructors with Arguments

• A constructor is a special method that is called when an object is allocated.

• We can writeOutputBox out = new OutputBox(“A Title”);

• Instead ofOutputBox out = new OutputBox();Out.setTitle(“A Title”);

• Writing constructors for programmer defined classes will be discussed in Chapter 7.

Page 13: 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

Return 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;

}

}

Page 14: 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

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;

Page 15: 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

Clock Class MethodsExample 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

Page 16: 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

Clock Class Outline

Page 17: 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

Geometry of Clock Drawing

Page 18: 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

Theta Calculations

• For the hour handtheta =

2*Math.PI*minute/60.0;

• For the minute handtheta =

2*Math.PI*(hour + minute)/60.0/12.0;

Page 19: 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

Drawing the Clock Hands

• Assuming (x, y) is the bottom vertex and recalling that computer graphics coordinates are upside downx1 = x + (int) (r*Math.sin(theta));y1 = y - (int) (r*Math.cos(theta));d.drawLine(x, y, x1, y1);

• For the hour hand use r*.8 in place of r

Page 20: 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

TwoClocks Client

public class TwoClocksClient {

public static void main (String[] args){

TwoClock twins = new TwoClocks();

twins.drawClocks();

twins.compareClocks();

}

}

Page 21: 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

Output from TwoClocks Client

Page 22: 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

UML Class Diagram for Clock-DrawingBox Composition