Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

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

3 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

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 } public void methodName2 (parameter) { declarations of local variables executable statements with comments }

5 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

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

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

8 UML Diagram for Hose Class

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.

10 Scope Example

11 Bad Variable Declarations

12 Class 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.

13 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; }

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 ;

15 Clock Class Methods ExampleExplanation 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

16 Clock Class Outline

17 Geometry of Clock Drawing

18 Theta Calculations For the hour hand theta = 2*Math.PI*minute/60.0; For the minute hand theta = 2*Math.PI*(hour + minute)/60.0/12.0;

19 Drawing the Clock Hands Assuming (x, y) is the bottom vertex and recalling that computer graphics coordinates are upside down x1 = 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

20 TwoClocks Client public class TwoClocksClient { public static void main (String[] args){ TwoClock twins = new TwoClocks(); twins.drawClocks(); twins.compareClocks(); }

21 Output from TwoClocks Client

22 UML Class Diagram for Clock-DrawingBox Composition


Download ppt "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."

Similar presentations


Ads by Google