008-002-007
Method Call with Arguments: Execute Methods by Passing Arguments in Recipe Card Class
Medium
Problem Description
Explanation: Calling Methods with Arguments
In this problem, you learned methods that receive arguments and methods without return values.
Key Learning Points
- Methods with Arguments: Format
public void methodName(type argument) - void Type: Use
voidwhen there's no return value - Field Update: Can change field values inside methods
Code Example Explanation
public void addIngredient(String ingredient) {
If (ingredients.isEmpty()) {
Ingredients = ingredient; // First ingredient
} else {
Ingredients += ", " + ingredient; // From second onward
}
}This method:
void: No return valueString ingredient: Receives string argumentingredients +=: Adds to existing string
Next Steps
Next, let's combine with methods that have return values!
.Test Cases
※ Output examples follow programming industry standards
Input:
Expected Output:
Input:
Expected Output:
Input:
Expected Output:
Input:
Expected Output:
Input:
Expected Output:
Input:
Expected Output:
❌ Some tests failed
Your Solution
Current Mode:● My Code
99
1
2
3
4
5
6
7
8
9
10
›
⌄
⌄
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here
sc.close();
}
}
0 B / 5 MB
You have 9 free executions remaining
