Back

Activity 3.2 Methods with Parameters and Return Values

divider

Introduction

Methods with Parameters and Return Values

Sending and Receiving Data in Java Methods

Why Use Parameters and Returns?

So far, we've created methods that perform actions, but they couldn't accept input or return results.

Parameters and return statements allow data to flow into and out of a method.

Parameters

A parameter is a variable that receives data when a method is called. It lets us reuse one method for different inputs.

public static void greetUser(String name)
{
System.out.println("Hello, " + name + "!");
System.out.println("Welcome to the Math Utility Program.");
}

Return Values

A method can send data back to the caller using a return statement. The method's return type tells Java what kind of data to return.

public static int square(int n)
{
return n * n;
}

Parameters + Returns Together

Methods can use both parameters and return values to create flexible and reusable tools.

public static double average(double a, double b)
{
return (a + b) / 2.0;
}

Data Flow

  1. Parameters are inputs
  2. The method body is the process
  3. The return value is the output

Key Terms

Parameter
A variable declared inside a method's header that receives input when the method is called.
Argument
The actual value or expression passed into a method when it's called.
Return Type
Specifies the kind of data a method sends back to the caller. If no data is returned, the return type is void.

Key Terms

Return Statement
The command that exits a method and sends a value back to the caller.
Void Method
A method that performs an action but does not return data.
Scope
The part of a program where a variable can be accessed. A parameter has scope only inside its method.

Next Lesson Preview

Method Overloading

In the next lesson, we'll explore how to define multiple methods with the same name but different parameters.

'F' → Fullscreen

Objectives

  • icon Define and call methods that take parameters.
  • icon Define and call methods that return values.
  • icon Use both to make methods reusable and flexible.
  • icon Differentiate between parameters and arguments.
divider

Activity Tasks

  • icon Create a new Java file named 3-2-ParametersReturn.
  • icon Complete each task and test your program output after every method is added.

Task 1: Passing Data into a Method

  • icon Create a method named greetUser(String name) that prints a personalized greeting.
  • icon Prompt the user for their name in main() and call the method.
Program.java
import java.util.Scanner;
public class Program
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
// Task 1
System.out.println("--- Math Utilities ---");
System.out.print("Enter your name: ");
String userName = input.nextLine();
greetUser(userName);
}
// Task 1 Method
static void greetUser(String name)
{
System.out.println("Hello, " + name + "!");
System.out.println("Welcome to the Math Utility Program.\n");
}
}

Task 2: Returning a Value from a Method

  • icon Define a method square(int n) that returns the square of a number.
  • icon In main(), ask the user for a number, call square(num), and print the result.
Program.java
import java.util.Scanner;
public class Program
{
public static void main(String[] args)
{
9 collapsed lines
Scanner input = new Scanner(System.in);
// Task 1
System.out.println("--- Math Utilities ---");
System.out.print("Enter your name: ");
String userName = input.nextLine();
greetUser(userName);
// Task 2
System.out.print("Enter a number to square: ");
int num = input.nextInt();
int result = square(num);
System.out.println("The square is " + result + "\n");
}
6 collapsed lines
// Task 1 Method
static void greetUser(String name)
{
System.out.println("Hello, " + name + "!");
System.out.println("Welcome to the Math Utility Program.\n");
}
// Task 2 Method
static int square(int n)
{
return n * n;
}
}

Task 3: Combining Parameters and Return Values

  • icon Create a method average(double a, double b) that returns the average of two numbers.
  • icon Add another method randomInRange(int min, int max) to return a random integer between two values.
  • icon Test both methods by prompting the user for input and displaying the results.
Program.java
import java.util.Scanner;
public class Program
{
public static void main(String[] args)
{
17 collapsed lines
Scanner input = new Scanner(System.in);
// Task 1
System.out.println("--- Math Utilities ---");
System.out.print("Enter your name: ");
String userName = input.nextLine();
greetUser(userName);
// Task 2
System.out.print("Enter a number to square: ");
int num = input.nextInt();
int result = square(num);
System.out.println("The square is " + result + "\n");
// Task 3
System.out.print("Enter first number: ");
double a = input.nextDouble();
System.out.print("Enter second number: ");
double b = input.nextDouble();
double avg = average(a, b);
System.out.println("The average is " + avg + "\n");
System.out.print("Enter minimum value: ");
int min = input.nextInt();
System.out.print("Enter maximum value: ");
int max = input.nextInt();
int random = randomInRange(min, max);
System.out.println("Random number between " + min + " and " + max + ": " + random + "\n");
}
12 collapsed lines
// Task 1 Method
static void greetUser(String name)
{
System.out.println("Hello, " + name + "!");
System.out.println("Welcome to the Math Utility Program.\n");
}
// Task 2 Method
static int square(int n)
{
return n * n;
}
// Task 3 Methods
static double average(double a, double b)
{
return (a + b) / 2.0;
}
static int randomInRange(int min, int max)
{
return (int)((Math.random() * (max - min + 1)) + min);
}
}
divider

Sample Output

Your program output should look similar to the example below.

Sample Output
Enter your name: Anthony
Hello, Anthony!
Welcome to the Math Utility Program.
Enter a number to square: 6
The square is 36
Enter first number: 8
Enter second number: 10
The average is 9.0
Enter minimum value: 1
Enter maximum value: 10
Random number between 1 and 10: 4
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. What is the difference between a parameter and an argument?
  2. Why must a method that returns a value specify a non-void return type?
  3. How could one method's return value be used as input to another method?
  4. Why is using parameters and returns more flexible than relying on global variables?
divider

Submission

Submit your completed .java file and reflection answers to the appropriate dropbox.

Activity Complete