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

Bug in the Quiz: Defaulting Parameters?

Hey there guys, I'll make this short and sweet.

This seemed like a simple enough exercise, and I believe I have done it correctly. I get the correct output in the output.html, but when checking the work it does not detect it as correct. I am either missing something, or there is a bug in the check.

Here is a screenshot of my output and the "Bummer!" message: Error message

Any help is appreciated, thank you. :)

Update: I got it to work. My problem was that I was just simply printing out the statement with 1, which got the correct result but was not what the exercise was looking for - which was not clear to me. I believe this error should be accounted for and a better "Bummer!" message should be given in the future.

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

  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. 
    */
  }
}
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

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

You are expected to call the original method, but push in the value of 1, so you don't repeat yourself

 /* Other code omitted for clarity. Please imagine
       lots and lots of code here. Don't repeat it. 
 */

Yeah, that information in the comment wasn't clear enough (for me at least) it was just too vague. It only took a second for me to do it correctly once I figured out what I had done wrong though.

Thank you for the assistance.