Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Delivering the MVP Defaulting Parameters

Checking my understanding of method signatures...

Thanks to other answers I was able to pass the challenge, but I wanted to know how to think about it properly. So in a void method with the same name, it's perfectly legal to just throw in the previous method as the body of your new method with no type or return or any prefixes?

Example.java
public class Example {

  public static void main(String[] args) {
    ShoppingCart cart = new ShoppingCart();
    Product pez = new Product("Cherry PEZ refill (12 pieces)");
    cart.addItem(pez, 5);
    /* Since a quantity of 1 is such a common argument when adding a product to the cart,
     * your fellow developers have asked you to make the following code work, as well as keeping
     * the ability to add a product and a quantity.
     */
    Product dispenser = new Product("Yoda PEZ dispenser");
    /* Uncomment the line following this comment,
       after adding a new method using method signatures,
       to solve their request in ShoppingCart.java
    */
    cart.addItem(dispenser);
  }

}
ShoppingCart.java
public class ShoppingCart {

  public void addItem(Product item, int quantity) {
    System.out.printf("Adding %d of %s to the cart.%n", quantity, item.getName());
    /* Other code omitted for clarity. Please imagine
       lots and lots of code here. Don't repeat it. 
    */
  }

    public void addItem(Product item) {
      addItem(item, 1);
  }

}
Product.java
public class Product {
  /* Other code omitted for clarity, but you could imagine
     it would store price, options like size and color
  */
  private String mName;

  public Product(String name) {
      mName = name;
  }

  public String getName() {
      return mName;
  }
}

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Hey Kathleen,

In regards to your main question in whether or not it's legal, the answer is yes. If you look at the second void, it takes what was passed in and applies it to the first void method. Since the first void method has a System.out.printf the second void method is legal. Whether that print statement was in the first or second method, as long as something is being printed when a void method is called, it will always be legal.

In case you wanted some more clarity in what is happening, I can help you with that. Our main void method accepts a Product object and a number and then when called, it will print out how much of that Product object we added. In our second void method, we use the first method because we want it to the same thing except that when no number is passed in as a parameter, it will default to 1. We then use the first void method and put our Product object and the number 1 as the parameter. And then it will print out that 1 of the Product was added. You probably already know most of this but I just wanted to make sure.

Happy coding and all the best,

Kevin

That makes it much clearer now :) thanks again!

Kevin Faust
Kevin Faust
15,353 Points

Glad to have been of help :)

Make sure to mark as best answer so others with the same question can see!

Kevin