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.
Chapter 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 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 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 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 } } 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 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 – 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 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 UML Diagram for Hose Class Variable 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 Scope Example Bad Variable Declarations 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 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; } } 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; 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 Clock Class Outline Geometry of Clock Drawing 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; 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 TwoClocks Client public class TwoClocksClient { public static void main (String[] args){ TwoClock twins = new TwoClocks(); twins.drawClocks(); twins.compareClocks(); } } Output from TwoClocks Client UML Class Diagram for Clock-DrawingBox Composition ... code 0) – booleans are initialized to false – object-type variables are initialized to the reference value null Hose Class Methods void getData() // Reads and stores the height and // weight... methods have access to the class instance variables – can be used for communication inside class Initialization of Instance Variables • Instance variable declarations can contain initializers.. .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