100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Class notes | Methods | Intro to Computer Programming (COMP150) Introduction to Java Programming and Data Structures, ISBN: 9780134670942 CA$11.60   Add to cart

Class notes

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

 25 views  1 purchase

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

Preview 2 out of 5  pages

  • October 9, 2022
  • 5
  • 2022/2023
  • Class notes
  • Stephen chiong
  • Week 5
book image

Book Title:

Author(s):

  • Edition:
  • ISBN:
  • Edition:
All documents for this subject (2)
avatar-seller
dazyskiies
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

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

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

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 CA$11.60. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

79223 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
CA$11.60  1x  sold
  • (0)
  Add to cart