190 likes | 263 Views
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
E N D
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 • Common Programming Errors
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”
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
Writing the Method (continued) • When a method is called, parameters passed must agree in: • Number • Order • Data type
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
Reusing Method Names (Overloading) (continued) • Parameter signature is part of the header line containing: • Method name • Parameter list
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
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
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
Returning Multiple Values • Ways of returning multiple values: • Use two methods • Use concatenated string • Use StringBuffer • Parsing • Separating a string into component parts
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); }}
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); }}
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(); } • }
2.7 p. 120-121 • # 1, 2, 3, 4