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

Why is it saying I didnt uncomment the last line of code, when I did?

??

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);
    Product dispenser = new Product("Yoda PEZ dispenser");
   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());

  }
  public void addItem(Product item){
    System.out.printf("Adding 1 %s to the cart.", item.getName());
  }
}
Product.java
public class Product {
  private String mName;

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

  public String getName() {
      return mName;
  }
}

1 Answer

The reason it is saying this is because it is expecting a certain result and did not obtain it. When creating the addItem(Object obj) {} method, you do not need to create a system.out.printf() line. You simply need to call the addItem method that was already created by passing through the object and the number of pez dispensers.

The reasoning behind this is because you already have a method that adds an item. The exercise is asking you to create a new method signature without the quantity portion, and then call the addItem with the quantity added. Remember, the code is already done for you, you just have to utilize it. It is going to look something like this

public void addItem(Object obj) { //replace Object obj with the correct syntax for the code you are using
addItem(obj, int); //replacing the int with however many items you want to add
} 

I hope this helps. Please let me know if you have any questions. Happy coding.