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

Small problem with output.

I tried to have the code output that I am Adding 1 Yoda PEZ dispenser to the cart. When I check preview it says that my output is exactly that but I keep getting the error message.

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());
  }
    /* 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 1 %s to the cart.%n", 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;
  }
}

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi David,

you have to override the first addItem method that takes a single argument "item". Inside that "second" method call the "first" addItem method and pass it the item and the quantity 1.

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

Makes sense?

Grigorij

Thanks for your help. I am still having some trouble. I don't fully understand the basic concept of how we can take a method that requires one input and make it so it excepts two. I still got the following error when I entered your code.

required: Product found: Product,int reason: actual and formal argument lists differ in length

Also I don't know how to call the 1 from addItem(item, 1); into the %d from System.out.printf.

Thank you very much for your help thus far and any more help is greatly appreciated.