Module 06 - Methods, Classes and Objects
Module 06 - Methods, Classes and Objects
0 03-June-2020
MODULE OVERVIEW
In this module, introduces the creation of classes, variables, and methods. They will learn
to create a class composed of instance variables and methods. The students will create and
invoke constructor methods to initialize instances of a class. Students will learn to create
methods that accept arguments and return value.
LEARNING OBJECTIVES
LEARNING CONTENTS
Creating Method
• Method
o Program module
o Contains series of statements
o Carries out task
• Execute method
o Invoke or call from another method
• Calling method (client method)
o Makes method call
• Called method
o Invoked by calling method
o
• Method must include:
o Method header
▪ Also called declaration
o Method body
▪ Between a pair of curly braces
▪ Contains the statements that carry out the work
▪ Also called implementation
▪
• Method declaration
o Optional access specifiers
o Return type for method
o Method name
o Set of parentheses
▪ Might contain data to be sent to the method
• Place entire method within class that will use it
o Not within any other method
• Local variable
o Known only within boundaries of method
o Each time method executes
▪ Variable redeclared
▪ New memory location large enough to hold type set up and named
Example: A program that accepts the amount of salary as parameter, then compute and print
its salary increase.
Example: A program that accepts the amount of salary and rate increase as parameters, then
compute and print its salary increase.
import java.util.Scanner;
public class Employee {
public static void predictRaiseUsingRate(double money, double rate) {
double newAmount;
newAmount = money * (1 + rate);
System.out.println("With raise, salary is " + newAmount);
}
public static void main(String[] args) {
int salary;
float increaseRate;
Scanner input = new Scanner(System.in);
System.out.print("How much is your salary?:");
salary = input.nextInt();
System.out.print("What is the increase rate?:");
increaseRate = input.nextFloat()
predictRaiseUsingRate(salary,increaseRate);
}
}
Figure 6.6 The predictRaiseUsingRate() method using scanner that accepts two parameter
arguments are the data you pass into the method's parameters. Parameter is variable in the
declaration of function. Argument is the actual value of this variable that gets passed to function
• return statement
o Causes value to be sent from called method back to calling method
• Return type can be any type used in Java
o Primitive types
o Class types
o void
▪ Returns nothing
• Method’s type
o Method’s return type
Another example program that accepts the computes the area of a rectangle
import java.util.Scanner;
public class Rectangle {
public static int getArea(int length, int width) {
int area;
area = length * width;
return area;
}
public static void main(String[] args) {
int length, width;
Scanner input = new Scanner(System.in);
System.out.print("Enter the length: ");
length = input.nextInt();
System.out.print("Enter the width: ");
width = input.nextInt();
System.out.println("The area of the rectangle is: " + getArea(length,
width));
}
}
Figure 6.10 The getArea () method using scanner
Class Concepts
• Every object is a member of more general class
• Is-a relationships
o Object “is a” member of class
• Instantiation
• Reusability
• Methods often called upon to return piece of information to source of request
• Class client or class user
o Application or class that instantiates objects of another prewritten class
Creating a Class
• Assign name to class
• Determine what data and methods will be part of class
• Class header
o Optional access modifier
o Keyword class
o Any legal identifier for the name of class
• public class
o Accessible by all objects
• Extend
Figure 6.12 The Employee class with one field and two methods
or
• Constructor method
– Method that creates and initializes class objects
– Can write own constructor methods
– Java creates a constructor
• Name of constructor method always same as name of class
• After object instantiated
– Methods accessed using:
• Object’s identifier
• Dot
• Method call
Program output:
The clerk employee id is: 101
The clerk employee service years is: 5
Organizing Classes
• Place data fields in logical order
– At beginning of class
– Fields listed vertically
• May place data fields and methods in any order within a class
– Common to list all data fields first
– Can see names and data types before reading methods that use data fields
Figure 6:16 Start of Employee class with data fields, methods, and comments
LEARNING POINTS
• Method
o Series of statements that carry out a task
o Declaration includes parameter type and local name for parameter
o Can pass multiple arguments to methods
o Has return type
• Class objects
o Have attributes and methods associated with them
• Instantiate objects that are members of class
• Constructor
o Method establishes object and provides specific initial values for object’s data fields
• Everything is an object
o Every object is a member of a more general class
• Implementation hiding, or encapsulation
o private data fields
o public access methods
REFERENCES