29
Chapter Four Defining Your Own Classes continued

Chapter Four Defining Your Own Classes continued

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter Four Defining Your Own Classes continued

Chapter Four

Defining Your Own Classes

continued

Page 2: Chapter Four Defining Your Own Classes continued

Topics

• Local variables

• Parameter passing

• Memory allocation for parameters– primitives– objects

• More on methods

• Sample development

Page 3: Chapter Four Defining Your Own Classes continued

Local Variables

• A local variable is a variable that is declared within a method declaration.– Local variables are accessible only from the

method in which they are declared. – Memory space for local variables is allocated

only during the execution of the method. When the method execution completes, memory space will be cleared.

– The parameters of a method are local to the method.

Page 4: Chapter Four Defining Your Own Classes continued

Sample method

public fromDollar( double dollar){double amount, fee;fee = exchangeRate - feeRate;amount = dollar * fee;return amount;}

• dollar is a parameter• amount and fee are local variables• none of them can be used outside the method

Page 5: Chapter Four Defining Your Own Classes continued

Local Varaibles

• Memory for local variables is allocated when the method is called– before calling from dollar

– after local variables are declared (in fromDollar)

amt = yenConverter.fromDollar( 200);

double amount, fee;

Page 6: Chapter Four Defining Your Own Classes continued

Local Variables

– after computing values

• Memory for local variables is relaeased when the method returns

amt = yenConverter.fromDollar( 200);

fee = exchangeRate - feeRate;amount = dollar * fee;

Page 7: Chapter Four Defining Your Own Classes continued

Parameter Passing

• When a method is called, the value of the argument is passed to the matching parameter, and separate memory space is allocated to store this value.

• This way of passing the value of arguments is called a pass-by-value, or call-by-value scheme.

Page 8: Chapter Four Defining Your Own Classes continued

Parameter Passing

• The data type of the argument must be assignment-compatible with the data type of the matching parameter.– the same type as the parameter– a type that can be automatically promoted to

that type

Page 9: Chapter Four Defining Your Own Classes continued

Class Diagrams

• The class diagram usually indicates– types for class data – parameter types – return types

Page 10: Chapter Four Defining Your Own Classes continued

Memory Allocation

• Local variables do not exist before the method starts executingx = 20;y = 20;

• Memory for myMethod is allocated and the values of the arguments are copied to the parameterstester.myMethod( x, y);

Page 11: Chapter Four Defining Your Own Classes continued

Memory Deallocation

• Values of the parameters are changedpublic void myMethod(

int one, double two) {one = 25;two = 35.4;}

• Memory for myMethod is deallocated so parameter values are erased– Arguments are unchanged

Page 12: Chapter Four Defining Your Own Classes continued

Accessors and Mutators

• A set method is called a mutator because it changes the property of an object.

• An accessor is a method that returns a property of an object.

Page 13: Chapter Four Defining Your Own Classes continued

Overloaded Methods

• As with constructors, several methods may have the same name as long as the methods have either– A different number of parameters, or– Different data types for the parameters if the number of

parameters is the same.

• The methods with the same name are called overloaded methods.

Page 14: Chapter Four Defining Your Own Classes continued

Calling methods

• Dot notation is required when calling a method of a different object

public class Aclass {

public void myMethod{

Bclass obj =

new Bclass();

obj.doWork();

}

}

• Dot notation is optional when calling a method of the same object

public class Bclass { public void myMethod{

doWork(); }}

• If we want to use dot notation in this case, we use this to refer to the current object

Page 15: Chapter Four Defining Your Own Classes continued

Objects as Parameters

• Passing and returning objects follow the same process as passing and returning primitive data types.– The only difference is that with objects, the value being

passed is the reference (or address) to an object.

• When a variable is an object name, the value of the variable is the address in memory where the object is stored.

• The effect of passing this value, or reference, is to have two variables (object names) referring to the same object.

Page 16: Chapter Four Defining Your Own Classes continued

Kennel Classes

Page 17: Chapter Four Defining Your Own Classes continued

How objects are passed to methods

• From TestKennel programkennel.board( latte);

public class Kennel {

public board( Pet pet) { // sequence of activities

}

}

Page 18: Chapter Four Defining Your Own Classes continued

Parameter Modification

• weight of pet is modified in board

• After board executes– memory for parameter

deallocated– latte has new weight

Page 19: Chapter Four Defining Your Own Classes continued

Returning an Object

• Local variables don't exist before method call

Weight weight;

weight = latte.vanityWeight()

• Memory for local variables is allocated when the method is called

Page 20: Chapter Four Defining Your Own Classes continued

Returning an Object

• vanityWeight is computed and set in the Weight object

vanityWeight.setWeight(1.5*wgt);

return vanityWeight;

• a reference to the object vanityWeight refers to is copied into weight

• memory for vanityWeight is deallocated

Page 21: Chapter Four Defining Your Own Classes continued

Modularizing Functionality

• When a common task is repeated over and over, encapsulate it so it can be reused– within a class, create methods to perform

common actions– for more general tasks, capture the common

task in a class and use an instance of the class to perform the task

• Example InputHandler class in section 4.7

• Java 1.5 has created the Scanner class to do basically the same thing

Page 22: Chapter Four Defining Your Own Classes continued

Sample Development

• Loan and LoanCalculator classes.– revisit the development problem from Chapter 2

• Can we make a better program using instantiable classes?

• Consider problem statement.– Write a loan calculator program that computes both

monthly and total payments for a given loan amount, annual interest rate, and loan period.

Page 23: Chapter Four Defining Your Own Classes continued

Class Design

• Original approach used local variables in the main method of a main class

– still need a main class: LoanCalculatorMain

• Why not use instantiable classes?– The Loan class provides services

• loan computations

– LoanCalculator is a controller class• creates a Loan object and calls the appropriate

methods• manages the objects in the program

Page 24: Chapter Four Defining Your Own Classes continued

Approaches to Development

• Top-down– Develop the top-level classes first– create minimal classes for service objects for

testing

• Bottom-up– develop service classes first– write test classes or main methods in the service

classes for testing

Page 25: Chapter Four Defining Your Own Classes continued

Implementation Steps

1. Start with the main class and a skeleton of the LoanCalculator class.

2. Implement the input routine to accept three input values.

3. Implement the output routine to display the results.

4. Implement the computation routine to compute the monthly and total payments.

5. Finalize the program, implementing any remaining temporary methods and adding necessary methods as appropriate.

Page 26: Chapter Four Defining Your Own Classes continued

Two Approaches

• main method calls all the methods directly

• LoanCalculator has a start method that does all the work

Page 27: Chapter Four Defining Your Own Classes continued

Overall plan

• The same basic tasks as before1. Get three input values: loanAmount, interestRate, and loanPeriod.

2. Compute the monthly and total payments.

3. Output the results.

Page 28: Chapter Four Defining Your Own Classes continued

main in Instantiable Classes

//Instantiable Main Classclass LoanCalculator {

//exactly the same code as before comes here

public static void main (String [ ] args) {

LoanCalculator loanCalculator;loanCalculator = new

LoanCalculator( );loanCalculator.start( );

}}

Page 29: Chapter Four Defining Your Own Classes continued

Javadoc comments

• Javadoc comments begin with /** and end with */. – put one at the top of each class– provide one for each public variable and method in the

class

• Javadoc tags are special markers that begin with @. For example:– @author– @param - provide one for each parameter of each

method– @return - describes the return value

• Tags should be on separate lines at the end of the appropriate javadoc comment