Bài giảng An Introduction to Computer Science Using Java - Chapter 5 Classes and Methods II

Tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 5 Classes and Methods II: Chapter 5 Classes and Methods IILecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:formally introduce the class construct as it is used in the Java languagediscuss the use of instance variables to facilitate method communicationdemonstrate the use of classes to improve program structureBuilding Classes with Multiple Methods Computer programs can be thought of using phasesInputComputationOutputUsing separate methods for each phase can improve the maintainability of a class or programBuilding 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 }}Instance VariablesLocal variablesvariables declared inside methodsno...

ppt22 trang | Chia sẻ: honghanh66 | Lượt xem: 938 | Lượt tải: 0download
Bạn đang xem trước 20 trang mẫu tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 5 Classes and Methods II, để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Chapter 5 Classes and Methods IILecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:formally introduce the class construct as it is used in the Java languagediscuss the use of instance variables to facilitate method communicationdemonstrate the use of classes to improve program structureBuilding Classes with Multiple Methods Computer programs can be thought of using phasesInputComputationOutputUsing separate methods for each phase can improve the maintainability of a class or programBuilding 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 }}Instance VariablesLocal variablesvariables declared inside methodsnot accessible to any other methodcannot be used for communicationInstance variablesdeclared outside the methods, but declared inside the classall class methods have access to the class instance variablescan be used for communication inside classInitialization of Instance VariablesInstance variable declarations can contain initializers just like local variablesUnlike local variables, instance variables will be initialized to default values if no initializers are foundintegers and doubles are initialized to 0characters are initialized to the null character (ASCII code 0)booleans are initialized to falseobject-type variables are initialized to the reference value nullHose Class Methodsvoid 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 ClassVariable Scope RulesThe 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 ExampleBad Variable DeclarationsClass Constructors with ArgumentsA 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.Return TypesIt is possible to have methods that have return types other than voidExample:public class Clock ( int hour; public int getHour (){ return hour; }}return StatementThe return statement allows a method to return a value to the callercan appear any where in the method bodycan be conditionally executedresults in immediate exit from a method when executedForm:return expression;Clock Class MethodsExampleExplanationvoid setup()Initializes the clockvoid getData()Reads and stores the hour and minute dataString toString()Returns string version of time suitable for printingvoid setHour(int h)Sets hour to hvoid setMinute(int m)Sets minute to mint getHour()Returns value of hourint getMinute()Returns value of minuteboolean priorTo(Clock c)Returns true is receiver < cvoid 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 dClock Class OutlineGeometry of Clock DrawingTheta CalculationsFor the hour handtheta = 2*Math.PI*minute/60.0;For the minute handtheta = 2*Math.PI*(hour + minute)/60.0/12.0;Drawing the Clock HandsAssuming (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 rTwoClocks Clientpublic class TwoClocksClient { public static void main (String[] args){ TwoClock twins = new TwoClocks(); twins.drawClocks(); twins.compareClocks(); }}Output from TwoClocks ClientUML Class Diagram for Clock-DrawingBox Composition

Các file đính kèm theo tài liệu này:

  • pptchapter5_1622.ppt
Tài liệu liên quan