Download free for 30 days
Sign in
Upload
Language (EN)
Support
Business
Mobile
Social Media
Marketing
Technology
Art & Photos
Career
Design
Education
Presentations & Public Speaking
Government & Nonprofit
Healthcare
Internet
Law
Leadership & Management
Automotive
Engineering
Software
Recruiting & HR
Retail
Sales
Services
Science
Small Business & Entrepreneurship
Food
Environment
Economy & Finance
Data & Analytics
Investor Relations
Sports
Spiritual
News & Politics
Travel
Self Improvement
Real Estate
Entertainment & Humor
Health & Medicine
Devices & Hardware
Lifestyle
Change Language
Language
English
Español
Português
Français
Deutsche
Cancel
Save
Submit search
EN
Uploaded by
xhan28m
PPT, PDF
7 views
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony GaddisCSO_Gaddis_Java_Chapter05.ppt
Object Oriented Programming
Technology
◦
Read more
0
Save
Share
Embed
Embed presentation
Download
Download to read offline
1
/ 28
2
/ 28
3
/ 28
4
/ 28
5
/ 28
6
/ 28
7
/ 28
8
/ 28
9
/ 28
10
/ 28
11
/ 28
12
/ 28
13
/ 28
14
/ 28
15
/ 28
16
/ 28
17
/ 28
18
/ 28
19
/ 28
20
/ 28
21
/ 28
22
/ 28
23
/ 28
24
/ 28
25
/ 28
26
/ 28
27
/ 28
28
/ 28
More Related Content
PPT
Cso gaddis java_chapter5
by
mlrbrown
PPTX
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
by
vekariyakashyap
DOCX
PSYC 3663 SAFMEDS TERMS – Assignment 1Provide a definition for e.docx
by
amrit47
PPTX
Chapter 5:Understanding Variable Scope and Class Construction
by
It Academy
PPTX
Chap4java5th
by
Asfand Hassan
PPTX
Chapter 4 Absolute Java
by
Shariq Alee
PPTX
java java java java java-unit-ii-batu-.pptx
by
ssuser7b9bda1
PPTX
IntroductionJava Programming - Math Class
by
sandhyakiran10
Cso gaddis java_chapter5
by
mlrbrown
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
by
vekariyakashyap
PSYC 3663 SAFMEDS TERMS – Assignment 1Provide a definition for e.docx
by
amrit47
Chapter 5:Understanding Variable Scope and Class Construction
by
It Academy
Chap4java5th
by
Asfand Hassan
Chapter 4 Absolute Java
by
Shariq Alee
java java java java java-unit-ii-batu-.pptx
by
ssuser7b9bda1
IntroductionJava Programming - Math Class
by
sandhyakiran10
Similar to Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony GaddisCSO_Gaddis_Java_Chapter05.ppt
PPT
Chapter 4 - Classes in Java
by
Khirulnizam Abd Rahman
PPTX
W4. Member Methods Object oriented programming.pptx
by
farhanghafoor5
PPT
Lec4
by
Hemlathadhevi Annadhurai
PDF
CIS 1403 lab 3 functions and methods in Java
by
Hamad Odhabi
DOCX
1 INVALID & VALID PARTY & FAVOR CHOICES P.docx
by
mercysuttle
PPTX
Class 10
by
SIVASHANKARIRAJAN
PPTX
Chapter 6 Methods.pptx
by
ssusere3b1a2
PPTX
5. Abstract Methods in Java Programing .pptx
by
ambrosenjeru1
PDF
Week 7 Java Programming Methods For I.T students.pdf
by
JaypeeGPolancos
PPTX
Chap5java5th
by
Asfand Hassan
PPT
Cso gaddis java_chapter9ppt
by
mlrbrown
PPTX
Java Foundations: Methods
by
Svetlin Nakov
PPTX
basics of java lanaguge you can learn about classes and objects in java .pptx
by
vaishnavizokdar
PPT
Chap07
by
Terry Yoast
PPT
Explain Classes and methods in java (ch04).ppt
by
ayaankim007
PPT
Java căn bản - Chapter7
by
Vince Vo
PPT
9781439035665 ppt ch07
by
Terry Yoast
PPTX
Chap2 class,objects contd
by
raksharao
PPT
Methods intro-1.0
by
BG Java EE Course
PPT
9781111530532 ppt ch07
by
Terry Yoast
Chapter 4 - Classes in Java
by
Khirulnizam Abd Rahman
W4. Member Methods Object oriented programming.pptx
by
farhanghafoor5
Lec4
by
Hemlathadhevi Annadhurai
CIS 1403 lab 3 functions and methods in Java
by
Hamad Odhabi
1 INVALID & VALID PARTY & FAVOR CHOICES P.docx
by
mercysuttle
Class 10
by
SIVASHANKARIRAJAN
Chapter 6 Methods.pptx
by
ssusere3b1a2
5. Abstract Methods in Java Programing .pptx
by
ambrosenjeru1
Week 7 Java Programming Methods For I.T students.pdf
by
JaypeeGPolancos
Chap5java5th
by
Asfand Hassan
Cso gaddis java_chapter9ppt
by
mlrbrown
Java Foundations: Methods
by
Svetlin Nakov
basics of java lanaguge you can learn about classes and objects in java .pptx
by
vaishnavizokdar
Chap07
by
Terry Yoast
Explain Classes and methods in java (ch04).ppt
by
ayaankim007
Java căn bản - Chapter7
by
Vince Vo
9781439035665 ppt ch07
by
Terry Yoast
Chap2 class,objects contd
by
raksharao
Methods intro-1.0
by
BG Java EE Course
9781111530532 ppt ch07
by
Terry Yoast
Recently uploaded
PPTX
The Abomination of AI – keynote at ICoSCI 2026
by
Alan Dix
PPTX
Robot Vacuums are Cleaning Up. The global robot vacuum segment has high marke...
by
Robert March
PPTX
Storage-and-HCI-Positioning-update-sales.pptx
by
hungungphu123
PPTX
Becoming Frontier for Dynamics 365 Finance PPT
by
jonbravetti
PPTX
Design Flaws and Known Attacks in Agentic AI - ITI Business Session
by
Information Technology Institute
PDF
Special Reeling Cable Specification _ Anhui Feichun Special Cable Co., Ltd_.pdf
by
Anhui Feichun Special Cable Co., Ltd.
PDF
Unlocking Gen AI Capabilities Within UiPath Document Understanding
by
DianaGray10
PPTX
Spacewalk 2026 - Presented at the IMAX in Raleigh, NC
by
All Things Open
PPTX
computer science class 11 ( phishing and fraud mails) .pptx
by
balajichess314
PPT
connectives-linking words in English.ppt
by
AmrMohammed82
PPTX
Fortify Your Digital Defenses with ServiceNow Security Operations by Suma Soft
by
Gavin Ellis
PDF
Top 10 Dedicated Server Providers in Stockholm (Updated Edition).pdf
by
Atal Networks
PDF
6 Insights from the Patron Saint of AI Failure
by
Scott M. Graffius
PDF
How Automation Powers Data Quality and Governance at Scale
by
Safe Software
PDF
Adding Copilot+ PCs with Snapdragon to your HP fleet won’t require IT deploym...
by
Principled Technologies
PPTX
WIFI Enabled 8 by 32 Matrix.pptx for final year project
by
mdsabbirhossain524856
PPTX
UNIT 1- Introduction to Basic of Computer Fundamentals
by
pawarbhaktiit
PPTX
Webinar: EVerest for Production: Accelerating EV Charger Development w/ Indus...
by
DanBrown980551
PDF
Best 10 Dedicated Servers in Amsterdam, Netherlands (2026 Enterprise Edition)...
by
Atal Networks
PDF
Presentation of Cybersecurity and data protection
by
tarikaityahya2
The Abomination of AI – keynote at ICoSCI 2026
by
Alan Dix
Robot Vacuums are Cleaning Up. The global robot vacuum segment has high marke...
by
Robert March
Storage-and-HCI-Positioning-update-sales.pptx
by
hungungphu123
Becoming Frontier for Dynamics 365 Finance PPT
by
jonbravetti
Design Flaws and Known Attacks in Agentic AI - ITI Business Session
by
Information Technology Institute
Special Reeling Cable Specification _ Anhui Feichun Special Cable Co., Ltd_.pdf
by
Anhui Feichun Special Cable Co., Ltd.
Unlocking Gen AI Capabilities Within UiPath Document Understanding
by
DianaGray10
Spacewalk 2026 - Presented at the IMAX in Raleigh, NC
by
All Things Open
computer science class 11 ( phishing and fraud mails) .pptx
by
balajichess314
connectives-linking words in English.ppt
by
AmrMohammed82
Fortify Your Digital Defenses with ServiceNow Security Operations by Suma Soft
by
Gavin Ellis
Top 10 Dedicated Server Providers in Stockholm (Updated Edition).pdf
by
Atal Networks
6 Insights from the Patron Saint of AI Failure
by
Scott M. Graffius
How Automation Powers Data Quality and Governance at Scale
by
Safe Software
Adding Copilot+ PCs with Snapdragon to your HP fleet won’t require IT deploym...
by
Principled Technologies
WIFI Enabled 8 by 32 Matrix.pptx for final year project
by
mdsabbirhossain524856
UNIT 1- Introduction to Basic of Computer Fundamentals
by
pawarbhaktiit
Webinar: EVerest for Production: Accelerating EV Charger Development w/ Indus...
by
DanBrown980551
Best 10 Dedicated Servers in Amsterdam, Netherlands (2026 Enterprise Edition)...
by
Atal Networks
Presentation of Cybersecurity and data protection
by
tarikaityahya2
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony GaddisCSO_Gaddis_Java_Chapter05.ppt
1.
Starting Out with
Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods
2.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-2 Chapter Topics Chapter 5 discusses the following main topics: – Introduction to Methods – Passing Arguments to a Method – More About Local Variables – Returning a Value from a Method – Problem Solving with Methods
3.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-3 Why Write Methods? • Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer. • Methods simplify programs. If a specific task is performed in several places in the program, a method can be written once to perform that task, and then be executed anytime it is needed. This is known as code reuse.
4.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-4 void Methods and Value-Returning Methods • A void method is one that simply performs a task and then terminates. System.out.println("Hi!"); • A value-returning method not only performs a task, but also sends a value back to the code that called it. int number = Integer.parseInt("700");
5.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-5 Defining a void Method • To create a method, you must write a definition, which consists of a header and a body. • The method header, which appears at the beginning of a method definition, lists several important things about the method, including the method’s name. • The method body is a collection of statements that are performed when the method is executed.
6.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-6 Two Parts of Method Declaration public static void displayMesssage() { System.out.println("Hello"); } Header Body
7.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-7 Parts of a Method Header public static void displayMessage () { System.out.println("Hello"); } Method Modifiers Return Type Method Name Parentheses
8.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-8 Parts of a Method Header • Method modifiers – public—method is publicly available to code outside the class – static—method belongs to a class, not a specific object. • Return type—void or the data type from a value- returning method • Method name—name that is descriptive of what the method does • Parentheses—contain nothing or a list of one or more variable declarations if the method is capable of receiving arguments.
9.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-9 Calling a Method • A method executes when it is called. • The main method is automatically called when a program starts, but other methods are executed by method call statements. displayMessage(); • Notice that the method modifiers and the void return type are not written in the method call statement. Those are only written in the method header. • Examples: SimpleMethod.java, LoopCall.java, CreditCard.java, DeepAndDeeper.java
10.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-10 Documenting Methods • A method should always be documented by writing comments that appear just before the method’s definition. • The comments should provide a brief explanation of the method’s purpose. • The documentation comments begin with /** and end with */.
11.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-11 Passing Arguments to a Method • Values that are sent into a method are called arguments. System.out.println("Hello"); number = Integer.parseInt(str); • The data type of an argument in a method call must correspond to the variable declaration in the parentheses of the method declaration. The parameter is the variable that holds the value being passed into a method. • By using parameter variables in your method declarations, you can design your own methods that accept data this way. See example: PassArg.java
12.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-12 Passing 5 to the displayValue Method displayValue(5); public static void displayValue(int num) { System.out.println("The value is " + num); } The argument 5 is copied into the parameter variable num. The method will display The value is 5
13.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-13 Argument and Parameter Data Type Compatibility • When you pass an argument to a method, be sure that the argument’s data type is compatible with the parameter variable’s data type. • Java will automatically perform widening conversions, but narrowing conversions will cause a compiler error. double d = 1.0; displayValue(d); Error! Can’t convert double to int
14.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-14 Passing Multiple Arguments showSum(5, 10); public static void showSum(double num1, double num2) { double sum; //to hold the sum sum = num1 + num2; System.out.println("The sum is " + sum); } The argument 5 is copied into the num1 parameter. The argument 10 is copied into the num2 parameter. NOTE: Order matters!
15.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-15 Arguments are Passed by Value • In Java, all arguments of the primitive data types are passed by value, which means that only a copy of an argument’s value is passed into a parameter variable. • A method’s parameter variables are separate and distinct from the arguments that are listed inside the parentheses of a method call. • If a parameter variable is changed inside a method, it has no affect on the original argument. • See example: PassByValue.java
16.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-16 Passing Object References to a Method • Recall that a class type variable does not hold the actual data item that is associated with it, but holds the memory address of the object. A variable associated with an object is called a reference variable. • When an object such as a String is passed as an argument, it is actually a reference to the object that is passed.
17.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-17 Passing a Reference as an Argument showLength(name); public static void showLength(String str) { System.out.println(str + " is " + str.length() + " characters long."); str = "Joe" // see next slide } address address “Warren” Both variables reference the same object The address of the object is copied into the str parameter.
18.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-18 Strings are Immutable Objects • Strings are immutable objects, which means that they cannot be changed. When the line str = "Joe"; is executed, it cannot change an immutable object, so creates a new object. • See example: PassString.java address address “Warren” “Joe” The name variable holds the address of a String object The str variable holds the address of a different String object
19.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-19 @param Tag in Documentation Comments • You can provide a description of each parameter in your documentation comments by using the @param tag. • General format @param parameterName Description • See example: TwoArgs2.java • All @param tags in a method’s documentation comment must appear after the general description.The description can span several lines.
20.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-20 More About Local Variables • A local variable is declared inside a method and is not accessible to statements outside the method. • Different methods can have local variables with the same names because the methods cannot see each other’s local variables. • A method’s local variables exist only while the method is executing. When the method ends, the local variables and parameter variables are destroyed and any values stored are lost. • Local variables are not automatically initialized with a default value and must be given a value before they can be used. • See example: LocalVars.java
21.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-21 Returning a Value from a Method • Data can be passed into a method by way of the parameter variables. Data may also be returned from a method, back to the statement that called it. int num = Integer.parseInt("700"); • The string “700” is passed into the parseInt method. • The int value 700 is returned from the method and assigned to the num variable.
22.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-22 Defining a Value-Returning Method public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; } Return type This expression must be of the same data type as the return type The return statement causes the method to end execution and it returns a value back to the statement that called the method.
23.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-23 Calling a Value-Returning Method total = sum(value1, value2); public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; } 20 40 60
24.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-24 @return Tag in Documentation Comments • You can provide a description of the return value in your documentation comments by using the @return tag. • General format @return Description • See example: ValueReturn.java • The @return tag in a method’s documentation comment must appear after the general description. The description can span several lines.
25.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-25 Returning a booleanValue • Sometimes we need to write methods to test arguments for validity and return true or false public static boolean isValid(int number) { boolean status; if(number >= 1 && number <= 100) status = true; else status = false; return status; } Calling code: int value = 20; If(isValid(value)) System.out.println("The value is within range"); else System.out.println("The value is out of range");
26.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-26 Returning a Reference to a String Object customerName = fullName("John", "Martin"); public static String fullName(String first, String last) { String name; name = first + " " + last; return name; } See example: ReturnString.java address “John Martin” Local variable name holds the reference to the object. The return statement sends a copy of the reference back to the call statement and it is stored in customerName.
27.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-27 Problem Solving with Methods • A large, complex problem can be solved a piece at a time by methods. • The process of breaking a problem down into smaller pieces is called functional decomposition. • See example: SalesReport.java • If a method calls another method that has a throws clause in its header, then the calling method should have the same throws clause.
28.
©2013 Pearson Education,
Inc. Upper Saddle River, NJ. All Rights Reserved. 5-28 Calling Methods that Throw Exceptions • Note that the main and getTotalSales methods in SalesReport.java have a throws IOException clause. • All methods that use a Scanner object to open a file must throw or handle IOException. • You will learn how to handle exceptions in Chapter 12. • For now, understand that Java required any method that interacts with an external entity, such as the file system to either throw an exception to be handles elsewhere in your application or to handle the exception locally.
Download