SlideShare a Scribd company logo
Java Methods
Week 7
Objective: To apply predefined and user defined methods in Java
programming.
What is a Method in Java?
• Is a block of code or collection of statements or a set of code
grouped together to perform a certain task or operation.
• Used to achieve a reusability of code. This means that a
method can written once and use it many times.
What is a Method in Java?
• Provides easy modification and readability of code by adding
or removing a chunk of code.
• It is executed only when we call or invoke it.
• The most important method in java is the main() method.
Types of Method
• Predefined Method
• User-defined Method
Predefined Method
What is a Method in Java?
Predefined Method
Methods already written
and provided by Java
Organized as a collection
of classes (class libraries)
To use: it needs an
import package
Method Type: data type
of value returned by
method
Predefined Method
Are the method
that is already
defined in Java
class libraries.
Also known as
standard library
method or built-in
method.
Example of
Predefined
Classes
User-Defined Method
User-Defined Methods
The method
written by the user
or programmer
This method is
modified according
to the requirement
Types of User-Defined Method
Ø Non – value returning method
Ø Value returning method
Ø Methods Overloading
• When the method returns nothing, the return
keyword at the end of the method is optional.
• It uses void keyword.
• Similar in structure to value-returning methods
Non - Value Returning Method
• Similar in structure to value-returning methods
• Call to method is always stand-alone statement
• Can use return statement to exit method early
Void Method
Syntax of Creating a method
without a return value
public static type name()
{
// method boy or method signature
}
Access Specifier
Return
Type
Method
Name
Method Header
Example of a
User-Defined
Method
public class Method {
public static void display()
{
System.out.println("Hi Java");
}
public static void main(String[] args) {
// call or invoke the Method
display();
} // end of main
}//end of class
public class Method {
public static void display()
{
System.out.println("Hi Java");
}
public static void main(String[] args) {
// call or invoke the Method
display();
} // end of main
}//end of class
Week 7 Java Programming Methods For I.T students.pdf
• Calculate and return a value
• Used to save value for later calculation or print value
• Uses a return keyword.
Value Returning Method
Week 7 Java Programming Methods For I.T students.pdf
public class Method {
public static int sum(int x, int y)
{
int sum = x + y;
System.out.println("Sum = "+sum);
return sum;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a, b;
System.out.print("Enter value for a:");
a = input.nextInt();
System.out.print("Enter value for b:");
b = input.nextInt();
sum(a,b);
}
}
Variable x, y are arguments or known as the
actual parameters;
Variable a ,b are known as format parameters;
Naming a Method
• remember that the method name must be a verb and start
with a lowercase letter.
• If the method name has more than two words, the first
name must be a verb followed by adjective or noun.
• In the multi-word method name, the first letter of each
word must be in uppercase except the first word.
Example:
• Single-word method name: sum(), area()
• Multi-word method name: computeCircle(),
calculateArea()
Naming a Method
• It is also possible that a method has the same
name as another method name in the same
class, it is known as method overloading
Primitive Type Wrapper Classes as
Parameters
• If a formal parameter is of the primitive data type and the corresponding actual
parameter is a variable, then the formal parameter cannot change the value of the
actual parameter
• Only reference variables can pass values outside the method (except, of course, for
the return value)
• Corresponding to each primitive data type, Java provides a class so that the values of
primitive data types can be wrapped in objects
• The class Integer does not provide a method to change the value of an existing
Integer object
• The same is true of other wrapper classes
Reference Variables as Parameters
• If a formal parameter is a reference variable:
• Copies value of corresponding actual parameter
• Value of actual parameter is address of the object where
actual data is stored
• Both formal and actual parameter refer to same object
Use Reference Variables as Parameters
• Can return more than one value from a method
• Can change the value of the actual object
• When passing address, would save memory space and time,
relative to copying large amount of data
Method Overloading
• Method Overloading: is creating several methods within a class,
with the same name.
• The signature of the method consists of the method name and its
formal parameter list
• Two methods have different signatures if they have either different
names or different format parameter lists
• Note that the signature of a method does not include the return type of
the method
Method Overloading
- by changing the number of arguments
Method Overloading
- by changing the data types
Debugging: Using Drivers and Stubs
• A program may contain a number of methods. In a complex program,
usually, when a method is written, it is tested and debugged alone.
• You can write a separate program to test the method. The program that
tests a method is called a driver program.
• Before writing the complete program, you could write separate driver
programs to make sure that each method is working properly.
Summary
• Sometimes the results calculated by one method are needed in another
method.
• In that case, the method that depends on another method cannot be tested
alone.
• A method stub is a method that is not fully coded.
• For a void method, a method stub might consist of only a method header
and a set of empty braces, {}.
• For a value-returning method it might contain only a return statement with
a plausible return value.
• If the problem is large and complex, it must be broken into subproblems, and if a
subproblem is still complex, it must further be divided into subproblems.
• The subdivision of a problem should continue to the point where the solution is clear and
obvious.
• Once a subproblem is solved, we can continue with the solution of another subproblem
and if all the subproblems of a problem are solved, we can continue with the next level.
• Eventually, the overall solution of the problem must be assembled and tested to ensure that
the programming code accomplishes the required task.
• A Java program is a collection of classes, and a class is a collection of data members
and methods.
• Each class and each method must work properly.
• To accomplish this, as explained in the previous section, once a method is written, it
can be tested using stubs and drivers.
• Since a method can be tested in isolation, it is not necessary to code all the methods in
order.
• Once all the methods are written, the overall program must be tested.
• The technique to solve a problem by subdividing into smaller problems is known as divide
and conquer and top-down design approach.
• These techniques are suitable and work for many kinds of problems, including most of the
problems given in this book and the problems you will encounter as a beginning
programmer.
• To simplify the overall solution of a problem that consists of many subproblems, we write
and test the code one piece at a time.
• Typically, once a subproblem is solved and the code is tested, it is saved as the first
version or a version of the program.
• We continue to add and save the program one piece at a time. Keep in mind that a working
program with fewer features is better than a nonworking one with many features.
Ad

More Related Content

Similar to Week 7 Java Programming Methods For I.T students.pdf (20)

OOPSCA1.pptx
OOPSCA1.pptxOOPSCA1.pptx
OOPSCA1.pptx
Soumyadipchanda2
 
Ifi7184 lesson5
Ifi7184 lesson5Ifi7184 lesson5
Ifi7184 lesson5
Sónia
 
Constructors and Method Overloading
Constructors and Method OverloadingConstructors and Method Overloading
Constructors and Method Overloading
Ferdin Joe John Joseph PhD
 
METHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptxMETHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptx
ArjunKhanal8
 
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
123 JAVA CLASSES, OBJECTS AND METHODS.ppt123 JAVA CLASSES, OBJECTS AND METHODS.ppt
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
mcjaya2024
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#
Muhammad Younis
 
Java method
Java methodJava method
Java method
sunilchute1
 
chap7 user function-1_240902_0940346.pdf
chap7 user function-1_240902_0940346.pdfchap7 user function-1_240902_0940346.pdf
chap7 user function-1_240902_0940346.pdf
d84nvrnh4f
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
CharthaGaglani
 
UNIT1-JAVA.pptx
UNIT1-JAVA.pptxUNIT1-JAVA.pptx
UNIT1-JAVA.pptx
ssuser99ca78
 
01-class-and-objects java code in the .pdf
01-class-and-objects  java code  in the .pdf01-class-and-objects  java code  in the .pdf
01-class-and-objects java code in the .pdf
sithumMarasighe
 
java.pptx
java.pptxjava.pptx
java.pptx
PRASHANTKULKARNI133
 
New operator and methods.15
New operator and methods.15New operator and methods.15
New operator and methods.15
myrajendra
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
Amr Abd El Latief
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
Michael Heron
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
Epsiba1
 
Methods in Java its a presentation that .pptx
Methods in Java its a presentation that .pptxMethods in Java its a presentation that .pptx
Methods in Java its a presentation that .pptx
anasraufmoh
 
OOPJ.pptx
OOPJ.pptxOOPJ.pptx
OOPJ.pptx
ssuser99ca78
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
Jan Niño Acierto
 
Ifi7184 lesson5
Ifi7184 lesson5Ifi7184 lesson5
Ifi7184 lesson5
Sónia
 
METHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptxMETHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptx
ArjunKhanal8
 
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
123 JAVA CLASSES, OBJECTS AND METHODS.ppt123 JAVA CLASSES, OBJECTS AND METHODS.ppt
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
mcjaya2024
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#
Muhammad Younis
 
chap7 user function-1_240902_0940346.pdf
chap7 user function-1_240902_0940346.pdfchap7 user function-1_240902_0940346.pdf
chap7 user function-1_240902_0940346.pdf
d84nvrnh4f
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
CharthaGaglani
 
01-class-and-objects java code in the .pdf
01-class-and-objects  java code  in the .pdf01-class-and-objects  java code  in the .pdf
01-class-and-objects java code in the .pdf
sithumMarasighe
 
New operator and methods.15
New operator and methods.15New operator and methods.15
New operator and methods.15
myrajendra
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
Michael Heron
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
Epsiba1
 
Methods in Java its a presentation that .pptx
Methods in Java its a presentation that .pptxMethods in Java its a presentation that .pptx
Methods in Java its a presentation that .pptx
anasraufmoh
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
Jan Niño Acierto
 

Recently uploaded (20)

Top 10 IT Help Desk Outsourcing Services
Top 10 IT Help Desk Outsourcing ServicesTop 10 IT Help Desk Outsourcing Services
Top 10 IT Help Desk Outsourcing Services
Infrassist Technologies Pvt. Ltd.
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Mastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdfMastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdf
Spiral Mantra
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Mastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdfMastering Advance Window Functions in SQL.pdf
Mastering Advance Window Functions in SQL.pdf
Spiral Mantra
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Ad

Week 7 Java Programming Methods For I.T students.pdf

  • 1. Java Methods Week 7 Objective: To apply predefined and user defined methods in Java programming.
  • 2. What is a Method in Java? • Is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation. • Used to achieve a reusability of code. This means that a method can written once and use it many times.
  • 3. What is a Method in Java? • Provides easy modification and readability of code by adding or removing a chunk of code. • It is executed only when we call or invoke it. • The most important method in java is the main() method.
  • 4. Types of Method • Predefined Method • User-defined Method
  • 6. What is a Method in Java? Predefined Method Methods already written and provided by Java Organized as a collection of classes (class libraries) To use: it needs an import package Method Type: data type of value returned by method
  • 7. Predefined Method Are the method that is already defined in Java class libraries. Also known as standard library method or built-in method.
  • 10. User-Defined Methods The method written by the user or programmer This method is modified according to the requirement
  • 11. Types of User-Defined Method Ø Non – value returning method Ø Value returning method Ø Methods Overloading
  • 12. • When the method returns nothing, the return keyword at the end of the method is optional. • It uses void keyword. • Similar in structure to value-returning methods Non - Value Returning Method
  • 13. • Similar in structure to value-returning methods • Call to method is always stand-alone statement • Can use return statement to exit method early Void Method
  • 14. Syntax of Creating a method without a return value public static type name() { // method boy or method signature } Access Specifier Return Type Method Name Method Header
  • 15. Example of a User-Defined Method public class Method { public static void display() { System.out.println("Hi Java"); } public static void main(String[] args) { // call or invoke the Method display(); } // end of main }//end of class
  • 16. public class Method { public static void display() { System.out.println("Hi Java"); } public static void main(String[] args) { // call or invoke the Method display(); } // end of main }//end of class
  • 18. • Calculate and return a value • Used to save value for later calculation or print value • Uses a return keyword. Value Returning Method
  • 20. public class Method { public static int sum(int x, int y) { int sum = x + y; System.out.println("Sum = "+sum); return sum; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int a, b; System.out.print("Enter value for a:"); a = input.nextInt(); System.out.print("Enter value for b:"); b = input.nextInt(); sum(a,b); } }
  • 21. Variable x, y are arguments or known as the actual parameters; Variable a ,b are known as format parameters;
  • 22. Naming a Method • remember that the method name must be a verb and start with a lowercase letter. • If the method name has more than two words, the first name must be a verb followed by adjective or noun. • In the multi-word method name, the first letter of each word must be in uppercase except the first word. Example: • Single-word method name: sum(), area() • Multi-word method name: computeCircle(), calculateArea()
  • 23. Naming a Method • It is also possible that a method has the same name as another method name in the same class, it is known as method overloading
  • 24. Primitive Type Wrapper Classes as Parameters • If a formal parameter is of the primitive data type and the corresponding actual parameter is a variable, then the formal parameter cannot change the value of the actual parameter • Only reference variables can pass values outside the method (except, of course, for the return value) • Corresponding to each primitive data type, Java provides a class so that the values of primitive data types can be wrapped in objects • The class Integer does not provide a method to change the value of an existing Integer object • The same is true of other wrapper classes
  • 25. Reference Variables as Parameters • If a formal parameter is a reference variable: • Copies value of corresponding actual parameter • Value of actual parameter is address of the object where actual data is stored • Both formal and actual parameter refer to same object
  • 26. Use Reference Variables as Parameters • Can return more than one value from a method • Can change the value of the actual object • When passing address, would save memory space and time, relative to copying large amount of data
  • 27. Method Overloading • Method Overloading: is creating several methods within a class, with the same name. • The signature of the method consists of the method name and its formal parameter list • Two methods have different signatures if they have either different names or different format parameter lists • Note that the signature of a method does not include the return type of the method
  • 28. Method Overloading - by changing the number of arguments
  • 29. Method Overloading - by changing the data types
  • 30. Debugging: Using Drivers and Stubs • A program may contain a number of methods. In a complex program, usually, when a method is written, it is tested and debugged alone. • You can write a separate program to test the method. The program that tests a method is called a driver program. • Before writing the complete program, you could write separate driver programs to make sure that each method is working properly.
  • 31. Summary • Sometimes the results calculated by one method are needed in another method. • In that case, the method that depends on another method cannot be tested alone. • A method stub is a method that is not fully coded. • For a void method, a method stub might consist of only a method header and a set of empty braces, {}. • For a value-returning method it might contain only a return statement with a plausible return value.
  • 32. • If the problem is large and complex, it must be broken into subproblems, and if a subproblem is still complex, it must further be divided into subproblems. • The subdivision of a problem should continue to the point where the solution is clear and obvious. • Once a subproblem is solved, we can continue with the solution of another subproblem and if all the subproblems of a problem are solved, we can continue with the next level. • Eventually, the overall solution of the problem must be assembled and tested to ensure that the programming code accomplishes the required task.
  • 33. • A Java program is a collection of classes, and a class is a collection of data members and methods. • Each class and each method must work properly. • To accomplish this, as explained in the previous section, once a method is written, it can be tested using stubs and drivers. • Since a method can be tested in isolation, it is not necessary to code all the methods in order. • Once all the methods are written, the overall program must be tested.
  • 34. • The technique to solve a problem by subdividing into smaller problems is known as divide and conquer and top-down design approach. • These techniques are suitable and work for many kinds of problems, including most of the problems given in this book and the problems you will encounter as a beginning programmer. • To simplify the overall solution of a problem that consists of many subproblems, we write and test the code one piece at a time. • Typically, once a subproblem is solved and the code is tested, it is saved as the first version or a version of the program. • We continue to add and save the program one piece at a time. Keep in mind that a working program with fewer features is better than a nonworking one with many features.