7 - Lec - Classes and Methods Part-2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Passing Values to a Method:

Parameters
• Input values for methods (within the program, not from user)
• passed values or parameters
• More flexibility for methods
• formal parameters
• Part of method definition
• After the method name, within parentheses
• type
• name
• arguments, or actual parameters
• Calling a method with an object within the parentheses
• matching data type
• in the same order
1
Formal vs Actual Parameters
public static void main(String[] args)
{
print(“George Bush”); //“George Bush” as argument
}

public static void print(String name) //name as parameter


{
System.out.print(“The name is: “ + name);
}

2
Parameter Passing Example
//Definition of method to square an integer
public int doubleValue(int numberIn)
{
return 2 * numberIn;
}

//Invocation of the method... somewhere in main...


...
int square = keyboard.nextInt();
System.out.println(someObj.doubleValue(square));
//calling
• What is the formal parameter in the method definition?
• numberIn
• What is the argument (actual parameter) in the method invocation?
• square

3
Arguments to Methods
• An argument in a method invocation can be
• a literal such as 2 or ‘A’
• a variable
• an expression that yields a value

4
Multiple Arguments to Methods
anObject.result(42, 85, 3.59, ‘B’); //call
//definition
public void result(int n1, int n2, double d1, char c1)
{…}

• arguments and formal parameters are matched by position


• Corresponding types need to match

5
public and private
public
• Attribute (instance variable)
• any class can directly access/change

• Method
• any class can invoke

private
• Attribute (instance variable)
• only the same class can access/change

• Method
• only the same class can invoke

6
private or public ?
• Attributes (instance variables)
• should be private, why?

• Methods
• usually public, why?
• sometimes private

• Default is package access in Java


• Convention is to explicitly state public or private

7
Accessors (get) and Mutators (set)
Methods
• Accessor / get methods—public methods that allow attributes (instance
variables) to be read
• Mutator / set methods—public methods that allow attributes (instance
variables) to be modified
• Check to make sure that changes are appropriate.
• Much better than making instance variables public

private attributes (instance variables) with public accessor and


mutator methods
• Use set methods to alter the value
• Use get methods to retrieve the value

8
Set Methods
• Fields in classes are usually private and therefore cannot be accessed from
outside the class.
• To assign values to such fields we need public methods that can be called by
application classes external to the class in which they are defined.
• The practice of assigning a value to a private field is so common that methods
which are specifically intended to do so are called “set methods”.
• For example, a set method in Circle class
public void setRadius(double cRad) {
radius = cRad;
}

9
Get Methods
• Another common requirement is for an application calls to access (get)
the value associated with a private field defined in another class.
• To do this it is common practice to define a public method for each field
which may need to be accessed that returns the value for each field.
• Such methods are generally referred to as "get methods".
• For example, a get method in Circle class
public double getRadius() {
return radius;
}

10
Account Class with an Instance Variable, and
set and get Methods

11
Circle Class Definition

12
CircleTest Class Definition

13
Assignment – Get and Set Methods
• Write a class called Person with the following attributes:
 title (Dr., Mr., Mrs., Ms.)
 first name
 last name
 age in years
 gender (boolean - true/false to indicated either male or female)

• The Person class should have a setter method with public access for each attribute.
• The Person class should have a getter method with public access for each attribute.
• For a Person with the following attributes:
title = "Mr."
first name = "William"
last name = "Gates"
age = 44
gender = true (true is male, false is female, or vice versa)
• The Person class should have the following public access methods that return Strings as follows:
standardName() formalName()
concatenation of the first and last names concatenation of the title, first name, and lastname
(i.e., "William Gates") (i.e., "Mr. William Gates")
14

You might also like