008-002-007
Method Call with Arguments: Execute Methods by Passing Arguments in Recipe Card Class
Medium
Problem Description
In this problem, you will implement the Recipe class that manages object state through fields such as ingredients.
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!
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up