Professional Documents
Culture Documents
7 - Lec - Classes and Methods Part-2
7 - Lec - Classes and Methods Part-2
7 - Lec - Classes and Methods Part-2
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
}
2
Parameter Passing Example
//Definition of method to square an integer
public int doubleValue(int numberIn)
{
return 2 * numberIn;
}
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)
{…}
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
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
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