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

  1. Methods with Arguments: Format public void methodName(type argument)
  2. void Type: Use void when there's no return value
  3. 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 value
  • String ingredient: Receives string argument
  • ingredients +=: 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