Class notes | Loops COMP 150 (COMP150) Introduction to Java Programming and Data Structures, ISBN: 9780134670942
COMPLETE TEST BANK FOR INTRODUCTION TO JAVA PROGRAMMING AND DATA STRUCTURES COMPREHENSIVE VERSION, 12TH EDITION, Y. DANIEL LIANG.
COMPLETE TEST BANK FOR INTRODUCTION TO JAVA PROGRAMMING AND DATA STRUCTURES COMPREHENSIVE VERSION, 12TH EDITION, Y. DANIEL LIANG.
All for this textbook (4)
Written for
University of the Fraser Valley (UFV)
Chemistry
COMP 150 (COMP150)
All documents for this subject (2)
Seller
Follow
dazyskiies
Content preview
Methods
Date @October 7, 2022
Introduction to Java Programming and Data Structures, Comprehensive
Text Version: Chapter 6
Defining Methods
A method is a collection of statements that are grouped together to perform an
operation
// define the method
public static int max(int num1, int num2) {
int result;
if (num1>num2) {
result = num1;
} else {
result = num2
}
return result;
}
// invoke the method
int z = max(x, y);
public static int max(int num1, int num2) - method header
public static - modifier
int - returnValueType
the data type of the value that is returned by the method
if the method does not return a value, the returnValueType is the keyword void
max - method name
(int num1, int num2) - method signature
combination of the method name and the parameter list
int num1, int num2 - parameter list
Methods 1
, num1 & num2 - formal parameters
variables defined in the method header
return result; - return value
x & y - actual parameters (arguments)
the value passed on to the parameter when a method is invoked
Calling Methods
// define the method
public static int max(int num1, int num2) {
int result;
if (num1>num2) {
result = num1;
} else {
result = num2
}
return result;
}
public static void main (String[] args) {
int i = 5;
int j = 2;
int k = max(i, j)
System.out.println("The maximum between " + i + " and " + j + " is " + k);
}
this program takes the integer values of i and j and puts them through the max
method,
caution:
// section a
public static int sign(int n) {
if (n > 0) {
return 1;
} else if (n == 0) {
return 0;
} else if (n < 0) {
return -1;
}
}
Methods 2
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller dazyskiies. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.99. You're not tied to anything after your purchase.