Title: Using Methods, Classes, and Objects
1Using Methods, Classes, and Objects
Java Programming Second Edition
2Methods
A series of statements that carry out some task
- Classes can contain an unlimited number of
methods - May not require any arguments
- May not return any values
- Similar to procedures, functions, subroutines
- Methods are reusable
Wouldnt it be nice to do the work once and
use the method many times?
3A Sample Method
public static void nameAndAddress()
System.out.println(Event Handlers
Incorporated) System.out.println(8900 U.S.
Hwy 14) System.out.println(Crystal Lake, IL
60014)
Declaration
Opening curly brace
Body
Closing curly brace
4Method Access Modifiers
- public
- any class can use it
- private
- friendly
- protected
- static
- method can be used from anywhere in the class
- Any class-wide method required the keyword static
- return type
- type of data returned
public static void nameAndAddress()
5The nameAndAddress() method
call to nameAndAddress method
Place the method within the program, but NOT
within another method
6Using a Method in Another Program
- Notify new class of location of method
- Example
- Both classes must reside in same folder
First.nameAndAddress()
7Methods with Single Arguments
Implementation hiding
- Argument - data passed to methods
- Implementation hiding - conceals the code that
actually performs a task - program doesnt need to know how a method works
- program only needs name of method, type of
information to send, and what type of return to
expect - Include the type and local name of the argument
public void predictRaise(double moneyAmt)
8The predictRaise() method
Number known as moneyAmount
Receive number of type double
9Calling predictRaise()
- Using a constant
- predictRaise(472.25)
- Using a variable
- predictRaise(mySalary)
- 472.25 and mySalary are known as moneyAmount
The variable moneyAmount is a local variable
to the predictRaise() method.
10Using the predictRaise() method
11Methods with Multiple Arguments
- Separate arguments by commas
- List type for each argument
- Pass arguments in order
predictRaiseGivenIncrease(mySalary,promisedRate)
12Methods that Return Values
- Return type any Java type (primitive or class
types) - Refer to return type as methods type
- public static void nameAndAddress() --gt type void
- public boolean overtime() --gt type boolean
- public static double calculateRaise(double
moneyAmount) --gt type double
public double calculateRaise(double
moneyAmount) double newAmount return
newAmount moneyAmount 1.10
13Methods that Return Values
- Use return statement to send back stored value
- Store returned value
-
-
myNewSalary calculateRaise(mySalary)
calculateRaise method returns double
newAmount newAmount stored as myNewSalary
System.out.println(New salary
calculateRaise(mySalary))
14public class SetUpSite3 public static void
main(String args) int currentYear
2003 int age statementOfPhilosophy()
age calculateAge(currentYear)
System.out.println("Serving you for " age "
years") public static void
statementOfPhilosophy()
System.out.println("Event Handlers Incorporated
is") System.out.println
("dedicated to making your event")
System.out.println("a most memorable one.")
public static int calculateAge(int
currDate) int yrs yrs currDate -
1977 return yrs
SetUpSite3.java
15Learning About Class Concepts
- Everything is an object
- Every object is a member of a class
- A Java program using classes is a class
- is-a relationships
- My desk in Schott 508 is a Desk
- My cat named Putty is a Cat
16Classes
Reusability
- An object is an instantiation of a class
- one tangible example of a class
- Objects inherit attributes from classes
- these attributes are predictable as members of
certain classes - reusability
- Every object that is an instance of a class has
the methods associated with the class
17Object-oriented Programming Steps
- 1. Create the classes of objects from which
objects will be instantiated - 2. Write other classes to use the objects
- A program (class) that instantiates objects of
another prewritten class is a class client (class
user).
18Creating a Class
- Create a class header
- optional access modifier (public, final,
abstract) - public classes are accessible by all objects and
may be extended (used as a basis for another
class) - keyword class
- name of the class
- If you dont specify access,
- access is public
- Public classes can be extended
- (used as a basis for other classes)
19Class fields
Greater control!
private data public access
Information hiding
- Include access modifier
- Example -
- private int empNum
- Allowable field modifiers
- private provides the highest level of security
- information hiding (private data/public method)
private public protected transient private
protected static final friendly
20Using Instance Methods
- Instance methods - methods associated with
individual objects (dont use static!) - Instance methods require an instance of an object!
public int getSiteNumber() return
siteNumber
Instance methods require an instantiated
object
21public class EventSite private int
siteNumber public int getSiteNumber() retur
n siteNumber public void setSiteNumber(int
n) siteNumber n
This file CANNOT be run as a program.
Why?
22Declaring Objects
- Declaring a class does not create an actual
object - To create an object that is an instance of a
class - Supply a type and an identifier
- Allocate memory for that object
Employee someEmployee someEmployee new
Employee() OR Employee someEmpe new
Employee()
23Declaring Objects - A Closer Look
The name of the constructor method is always the
same as the name of the class it constructs.
Declare an instance of the Employee class
Employee someEmployee someEmployee new
Employee()
Constructor method that constructs an Employee
object
Allocate necessary memory
24Constructor Methods
- Method that constructs an object
- Write your own OR
- Java writes one for you
- Employee() - a constructor method written by Java
25Accessing an Instantiated Object
- someEmployee.changeSalary(350.00)
- Method (changeSalary) is applied to object
(someEmployee) and the argument (350.00) is
passed to the method
public void changeSalary(double
newAmount) salary newAmount
26Organizing Classes
- Place data fields in some logical order at the
beginning of a class - Store class methods in alphabetical order
- Use comments to document and describe
See Figure 3-28
27Using Constructors
- Employee chauffeur new Employee()
- Constructor methods supplied by Java provide
specific initial values - numeric fields set to 0
- character fields set to Unicode \u0000
- Boolean fields set to false
- object type fields set to null
28Write your own Constructor
- Must have same name as class it constructs
- No return type
- Example
Employee() empSal 300.00