Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach."— Presentation transcript:

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

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

3 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 4

5 5

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

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

8 8

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

10 10 Reusing Method Names (Overloading) (continued) Parameter signature - the distinguishing features of a method heading: the combination of a method name with the number and type(s) of its parameters in their given order. Overloading – repeated use of a method name with a different signature.

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

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

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

14 14

15 15 Returning Multiple Values Ways of returning multiple values:  Use two methods  Use concatenated string Parsing  Separating a string into component parts

16 16 public class RoomType { // data declarations section private double length; // instance variable private double width; // instance variable // method definitions section public RoomType() // 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 = " + this.length + "\n width = " + this.width); } public void setNewValues() // this is a mutator { length = 12.5; width = 9.0; } public void calculateArea() // this performs a calculation { System.out.println(length * width); } }

17 17 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); } }

18 18 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(); } }

19 19 Method Development: Algorithms Algorithm  Step-by-step set of instructions  Describes how data are to be processed to produce desired result Must clearly understand difference between algorithmic and intuitive commands  Computers do not understand intuitive commands Coding the algorithm  Writing in programming language

20 20 Algorithms (continued) Pseudocode  English-like phrases used to describe algorithm Formulas  Mathematical equations are used Flowcharts  Diagrams that employ symbols are used

21 21

22 22 Using Pseudocode Most commonly used method for developing algorithms Short English phrases Example:  Input three numbers into computer  Calculate average by adding numbers and dividing the sum by 3  Display average


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

Similar presentations


Ads by Google