Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.

Similar presentations


Presentation on theme: "COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable."— Presentation transcript:

1 COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable classes *Components of a class *Visibility modifiers *Class diagrams revisited

2 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Template for Instantiable Class

3 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Class Diagrams *A complete class diagram usually indicates types for class data parameter types return types

4 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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

5 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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.

6 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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.

7 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Sample Development *Loan and LoanCalculator classes. revisit the development problem from Chapter 3 *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.

8 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Class Design *Original approach used local variables in the main method of a main class still need a main class: LoanCalculatorMain LoanCalculator is a controller class creates a Loan object and calls the appropriate methods manages the objects in the program *Why not use instantiable classes? You might sometimes need to consider more than one loan at a time The Loan class provides services loan computations

9 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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

10 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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.

11 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Two Approaches *main method calls all the methods directly *LoanCalculator has a start method that does all the work

12 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Overall plan *The same basic tasks as before 1. Get three input values: loanAmount, interestRate, and loanPeriod. 2.Compute the monthly and total payments. 3.Output the results.

13 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Putting a main method in an Instantiable Class //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( ); }


Download ppt "COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable."

Similar presentations


Ads by Google