0% found this document useful (0 votes)
81 views

Java Methods (Subgoal-Labeled Worked Examples)

The document provides instructions for writing methods in Java that can either have no return value or return a value. It explains how to define a method that sums two integers and prints their sum, as well as a method that sums two integers and returns their sum, storing the returned value in a variable. The key steps are to define the method signature with the correct return type and parameters, write the method body, and call the method from another method or store its return value.

Uploaded by

Kelly Lougheed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Java Methods (Subgoal-Labeled Worked Examples)

The document provides instructions for writing methods in Java that can either have no return value or return a value. It explains how to define a method that sums two integers and prints their sum, as well as a method that sums two integers and returns their sum, storing the returned value in a variable. The key steps are to define the method signature with the correct return type and parameters, write the method body, and call the method from another method or store its return value.

Uploaded by

Kelly Lougheed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

How to Write a Method (Parameters, No Return Value)

Problem​​: Write a method that sums two integers and prints their sum. Call the method with two integers.

SUBGOAL: Set up Java environment.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {

}
}

SUBGOAL: Write the method definition.

1. After the main method ends, write “public static void” followed by the name of your new method.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {

​public​ ​static​ ​void​ sum


}

2. Write the comma-separated parameters for the method, including the data types and the parameter
names.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {

​public​ ​static​ ​void​ sum​(​int​ x, ​int​ y)

}
3. Add in the curly braces.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {

​public​ ​static​ ​void​ sum(​int​ x, ​int​ y) ​{

4. Write the body of the method.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {

​public​ ​static​ ​void​ sum(​int​ x, ​int​ y) {


​System.out.println(x + y);
}

}
SUBGOAL: Call the method.

1. Write the name of the method inside main.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {
​sum
}

​public​ ​static​ ​void​ sum(​int​ x, ​int​ y) {


System.out.println(x + y);
}

2. Add parentheses after the method name to call it. Inside the parentheses, include the two arguments.
End with a semicolon.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {
sum​(​2​, ​3​);
}

​public​ ​static​ ​void​ sum(​int​ x, ​int​ y) {


System.out.println(x + y);
}

3. Run your program and test it!


How to Write a Method (Parameters & Return Value)

Problem​​: Write a method that sums two integers and returns their sum. Call the method with two integers
and store the result in a variable.

SUBGOAL: Set up Java environment.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {

}
}

SUBGOAL: Write the method definition.

1. Write “public static” followed by the data type of the return variable and the name of the method.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {

​public​ ​static​ ​int​ sum


}

2. Write the comma-separated parameters for the method, including the data types and the parameter
names.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {

​public​ ​static​ ​int​ sum​(​int​ x, ​int​ y)

}
3. Add in the curly braces.
class​ Main {
​public​ ​static​ ​void​ main(String[] args) {

​public​ ​static​ ​int​ sum(​int​ x, ​int​ y) ​{

4. Write the body of the method, ending with a return statement followed by a value.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {

​public​ ​static​ ​int​ sum(​int​ x, ​int​ y) {


​return x + y;
}

SUBGOAL: Store the method’s return value in a variable.

1. In main, declare a variable to store the return value.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {
​int​ number
}

​public​ ​static​ ​int​ sum(​int​ x, ​int​ y) {


​return​ x + y;
}

2. Call the method with arguments and assign it to the variable.

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {
​int​ number​ = sum(​2​, ​3​);
}

​public​ ​static​ ​int​ sum(​int​ x, ​int​ y) {


​return​ x + y;
}

3. Optional: you can print the variable to see what’s inside it!

class​ Main {
​public​ ​static​ ​void​ main(String[] args) {
​int​ number = sum(​2​, ​3​);
​System.out.println(number);
}

​public​ ​static​ ​int​ sum(​int​ x, ​int​ y) {


​return​ x + y;
}

You might also like