1 / 29

Chapter Four

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.

sade-brewer
Download Presentation

Chapter Four

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 Four Defining Your Own Classes continued

  2. Topics • Local variables • Parameter passing • Memory allocation for parameters • primitives • objects • More on methods • Sample development

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

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

  5. 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;

  6. Local Variables • after computing values • Memory for local variables is relaeased when the method returns fee = exchangeRate - feeRate; amount = dollar * fee; amt = yenConverter.fromDollar( 200);

  7. 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-valuescheme.

  8. 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

  9. Class Diagrams • The class diagram usually indicates • types for class data • parameter types • return types

  10. Memory Allocation • Local variables do not exist before the method starts executing x = 20; y = 20; • Memory for myMethod is allocated and the values of the arguments are copied to the parameters tester.myMethod( x, y);

  11. Memory Deallocation • Values of the parameters are changed public void myMethod( int one, double two) { one = 25; two = 35.4;} • Memory for myMethod is deallocated so parameter values are erased • Arguments are unchanged

  12. Accessors and Mutators • A set method is called a mutator because it changes the property of an object. • An accessoris a method that returns a property of an object.

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

  14. 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 Calling methods

  15. 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.

  16. Kennel Classes

  17. From TestKennel program kennel.board( latte); public class Kennel { public board( Pet pet) { // sequence of activities } } How objects are passed to methods

  18. weight of pet is modified in board After board executes memory for parameter deallocated latte has new weight Parameter Modification

  19. 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

  20. vanityWeight is computed and set in the Weight object vanityWeight.setWeight(1.5*wgt); return vanityWeight; a reference to the object vanityWeightrefers to is copied into weight memory for vanityWeight is deallocated Returning an Object

  21. 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

  22. 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.

  23. 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

  24. 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

  25. Implementation Steps • Start with the main class and a skeleton of the LoanCalculator class. • Implement the input routine to accept three input values. • Implement the output routine to display the results. • Implement the computation routine to compute the monthly and total payments. • Finalize the program, implementing any remaining temporary methods and adding necessary methods as appropriate.

  26. Two Approaches • main method calls all the methods directly • LoanCalculator has a start method that does all the work

  27. Overall plan • The same basic tasks as before • Get three input values: loanAmount, interestRate, and loanPeriod. • Compute the monthly and total payments. • Output the results.

  28. main in Instantiable Classes //Instantiable Main Class class LoanCalculator { //exactly the same code as before comes here public static void main (String [ ] args) { LoanCalculator loanCalculator; loanCalculator = new LoanCalculator( ); loanCalculator.start( ); } }

  29. 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

More Related