Chapter Four Defining Your Own Classes continued.

Slides:



Advertisements
Similar presentations
DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Road Map Introduction to object oriented programming. Classes
Chapter 41 Defining Classes and Methods Chapter 4.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Development Software Life Cycle UML Diagrams.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Sample Development Loan Calculator.
Chapter Four Defining Your Own Classes. Topics Instantiable classes Components of a class –constructors –accessors –mutators Visibility modifiers Class.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 More on Defining Classes Overloading.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: *Sample Development Loan Calculator.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 3b Standard Input and Output Sample Development.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
CS1101X: Programming Methodology Recitation 2 Classes.
IT PUTS THE ++ IN C++ Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Why Programmer-Defined Classes Using just the String,
© 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development  Problem-solving approach  Breaking a task down into smaller subtasks  First level.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 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.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 7- Defining Your Own Classes Part 2 : Objectives After you have read and studied this chapter, you should be able to –Describe how objects are.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Java Programming: Guided Learning with Early Objects
Chapter 7 Top-Down Development
Chapter 3: Using Methods, Classes, and Objects
User-Defined Functions
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 4 Defining Instantiable Classes
Defining Your Own Classes
Classes & Objects: Examples
Introduction to Object-Oriented Programming with Java--Wu
Defining Classes and Methods
Object Oriented Programming in java
Classes CS 21a: Introduction to Computing I
Presentation transcript:

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

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

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 program kennel.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 before 1.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 Class class 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 For example: - provide one for each parameter of each method - describes the return value Tags should be on separate lines at the end of the appropriate javadoc comment