100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

Class notes | Methods | Intro to Computer Programming (COMP150) Introduction to Java Programming and Data Structures, ISBN: 9780134670942

Rating
-
Sold
1
Pages
5
Uploaded on
09-10-2022
Written in
2022/2023

Typed Notes on Java Methods, including standard code forms and sample code blocks










Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
October 9, 2022
Number of pages
5
Written in
2022/2023
Type
Class notes
Professor(s)
Stephen chiong
Contains
Week 5

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
CA$11.33
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
dazyskiies

Get to know the seller

Seller avatar
dazyskiies university of the fraser valley
View profile
Follow You need to be logged in order to follow users or courses
Sold
1
Member since
3 year
Number of followers
0
Documents
27
Last sold
3 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions