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

Charlie Gallentine
Charlie Gallentine
934 Points

What is the difference between these? Java objects defaulting parameters

I think I understand this challenge but I am having difficulty understanding the difference between these two lines of code.

What is the difference between what I have in (correct as far as I know):

public void addItem(Product item) { System.out.printf("Adding %d of %s to the cart.\n", 1, item.getName()); }

and saying this (incorrect due to repeating?):

public void addItem(Product item) { System.out.printf("Adding 1 of %s to the cart.\n", item.getName()); }

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) {
    System.out.printf("Adding %d of %s to the cart.\n", 1, item.getName());
  }
}
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;
  }
}

3 Answers

Matthew Hardy
Matthew Hardy
4,063 Points

The reason it states "(incorrect due to repeating?)" is because in the line

System.out.printf("Adding %d of %s to the cart.\n", 1, item.getName());

you are assigning in the 1 to the %d no matter what. In this sense you're repeating the 1 because you could simply replace the %d with 1 and not have to type both of these things out.

Charlie Gallentine
Charlie Gallentine
934 Points

The code you quoted is what the program counts as correct. When I substitute the %d for the 1 it gives me the repetition error, so I'm not sure that that's it unless I am misreading you.

Matthew Hardy
Matthew Hardy
4,063 Points

Oh that's odd. I can't think of a reason they would differ besides for teaching purposes. Since you get no error either way I'm just assuming that's the way Craig wanted you to do it for teaching purposes. Sorry for the confusion, I misunderstood which one was giving you the error

Raquel Smith
Raquel Smith
10,683 Points

The code above worked for me, also. I rated the challenge with a 1 star because I didn't really feel like it achieved the goal of exercising our method signature muscles very well - just our forum searching muscles ;) Fortunately, the quizzes can be updated much more easily than the videos, so hopefully Craig can take another look at the quiz and improve it a bit.