1 / 19

Chapter 3: Developing Class Methods

Chapter 3: Developing Class Methods. Object-Oriented Program Development Using Java: A Class-Centered Approach. Objectives. Method and Parameter Declarations Returning a Single Value Method Development: Algorithms Application: Swapping Values static and final Variables

Download Presentation

Chapter 3: Developing Class Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

  2. Objectives • Method and Parameter Declarations • Returning a Single Value • Method Development: Algorithms • Application: Swapping Values • static and final Variables • Common Programming Errors

  3. Method and Parameter Declarations • Arguments are data passed to a method • Calling a method • Invoking a method • Method being invoked is referred to as “called method”

  4. Writing the Method • Methods must be written to accept data • Arguments are defined in the method header: • public void setNewValues(double len, double wid) • Formal parameters: • Are identifier names in the method header • Must be separated by commas • Must have individual data types declared separately

  5. Writing the Method (continued) • When a method is called, parameters passed must agree in: • Number • Order • Data type

  6. Reusing Method Names (Overloading) • Overloading is using the same method name for more than one method • Compiler must be able to determine which method to use • Based on data types of parameters • Particularly useful in writing constructor methods

  7. Reusing Method Names (Overloading) (continued) • Parameter signature is part of the header line containing: • Method name • Parameter list

  8. Passing a Reference Value • A copy of the value in a variable is passed to the called method • Stored in one of the method’s formal parameters • A change to the parameter’s value has no effect on the argument’s value • Except in extremely limited cases

  9. Returning a Single Value • Pass by value • Values are copied into new variable locations • Methods can return at most one value to calling program • Called method provides: • Data type of returned value • Actual value being returned

  10. Returning a Single Value (continued) • Example: • public double calculateArea() • Returning a value syntax: • return expression; • After value is returned, program control reverts to calling method

  11. Returning Multiple Values • Ways of returning multiple values: • Use two methods • Use concatenated string • Use StringBuffer • Parsing • Separating a string into component parts

  12. public class RoomType{ // data declarations section private double length; // declare length as a double variable private double width; // declare width as a double variable // method definitions section public RoomType() // this is a constructor { this.length = 25.0; this.width = 12.0; System.out.println("Created a new room object using the default constructor\n"); } public void showValues() // this is an accessor { System.out.println(" length = " + this.length + "\n width = " + this.width); } public void setNewValues() // this is a mutator { this.length = 12.5; this.width = 9.0; } public void calculateArea() // this performs a calculation { System.out.println(this.length * this.width); }}

  13. public class RoomTypeOne{ // data declarations section private double length; // declare length as a double variable private double width; // declare width as a double variable // method definitions section /* public RoomTypeOne() // this is a constructor { length = 25.0; width = 12.0; System.out.println("Created a new room object using the default constructor\n"); } */ public void showValues() // this is an accessor { System.out.println(" length = " + length + "\n width = " + width); } public void setNewValues(double len, double wid) // this is a mutator { length = len; width = wid; } public void calculateArea() // this performs a calculation { System.out.println(length * width); }}

  14. public class UseRoomTypeOne • { • public static void main(String[] args) { RoomTypeOne roomOne; // declare a variable of type RoomTypeOne roomOne = new RoomTypeOne(); // create and initialize an object of // type RoomTypeOne System.out.println("\nThe values for this room are:"); roomOne.showValues(); // use a class method on this object System.out.print("The floor area of this room is: "); roomOne.calculateArea(); // use another class method on this object roomOne.setNewValues(6, 3.5); // call the mutator System.out.println("\nThe values for this room have been changed to:"); roomOne.showValues(); System.out.print("The floor area of this room is: "); roomOne.calculateArea(); } • }

  15. 2.7 p. 120-121 • # 1, 2, 3, 4

More Related