Date post: | 22-Dec-2015 |
Category: |
Documents |
View: | 218 times |
Download: | 0 times |
Chapter Four
Defining Your Own Classes
continued
Topics
• Local variables
• Parameter passing
• Memory allocation for parameters– primitives– objects
• More on methods
• Sample development
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.
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
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;
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;
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.
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
Class Diagrams
• The class diagram usually indicates– types for class data – parameter types – return types
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);
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
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.
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.
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
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.
Kennel Classes
How objects are passed to methods
• From TestKennel programkennel.board( latte);
public class Kennel {
public board( Pet pet) { // sequence of activities
}
}
Parameter Modification
• weight of pet is modified in board
• After board executes– memory for parameter
deallocated– latte has new weight
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
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
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
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.
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
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
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.
Two Approaches
• main method calls all the methods directly
• LoanCalculator has a start method that does all the work
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.
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( );
}}
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