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

Michael LaCroix
Michael LaCroix
5,828 Points

Says I'm missing what I'm not...

No idea what the problem is.

The editor is asking me if I uncommented the line that was commented. I did just that and it's still giving the same error message. What else am I missing that it's not telling me?

Also, could someone tell me why the %s is referencing item.getName - I don't understand where the item class came from (that is a class, right?). I thought it would have to be product.getName since the getName method is under the Product class. Thoughts?

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 this line after 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 */
  }
   public void addItem(Product item) {
   System.out.printf("Adding one %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;
  }
}

4 Answers

Raymond Wach
Raymond Wach
7,961 Points

Hi Michael LaCroix,

If you could paste in the error message that you are getting, I can try to explain what is going on. The code you have there looks like it should work to me.

In regards to your other question:

Also, could someone tell me why the %s is referencing item.getName - I don't
understand where the item class came from (that is a class, right?). I thought it would have to be product.getName since the getName method is under the Product class. Thoughts?

%s inside the first string argument to System.out.printf is just a formatting token that will be replaced by the corresponding String argument passed to printf. Your code passes in the return value of item.getName() (note the () at the end). To find out where item came from, you need to find its declaration, which is in the parameter list of the current method. The type of item is Product so you know that you can access any public member or function of the Product class because item is an instance of the Product class. getName is a public instance method of the Product class that returns a String object (i.e. an object with the type of String) so in order to call getName you need to have an instance of Product. When addItem is called, it is guaranteed to be passed an instance of Product, which addItem will refer to in its body as item (this is what the declaration tells you). item is just what the current code happens to declare its Product argument will be called inside the current scope (the body of addItem). If the declaration of addItem was

    public void addItem(Product product)

Then you would call need to say product.getName() in order for printf to translate %s into the same thing. Remember that Java is case sensitive so Product and product are not referring to the same thing.

Michael LaCroix
Michael LaCroix
5,828 Points

There's no indication in the Preview of where the error is occurring. Just the following message in the red bar that appears when there's an error:

"Bummer! Did you uncomment the line in Example.java? The output from System.out does not show the 1 Yoda PEZ dispenser."

Also, how are you getting those screen shot type images to appear when you quote me and then when you indicated the declaration of add item?

Raymond Wach
Raymond Wach
7,961 Points

I'm using code blocks. You can see how to do that by clicking the "Markdown Cheatsheet" reference under the textbox for posting an answer or comment (note, I'm also using a comment instead of an answer).

To include code in your answer, wrap it in triple backticks(`)(the tilde key on US keyboards) and optionally include the language the code is written in. E.g.:

```Java
// This is a Java single-line comment.
```

// This is a Java single-line comment.
Raymond Wach
Raymond Wach
7,961 Points

Also, the challenges can be pretty picky about the answer format they are expecting. Hopefully there is something to indicate what output is expected so that you can make sure your format string is correct.

Michael LaCroix
Michael LaCroix
5,828 Points

LOL. Dude, that's precisely why I posted a question. It's giving me an error message with no indication of where the error is occurring. I thought I fixed what it's asking me to fix. Thus my confusion and post.

Raymond Wach
Raymond Wach
7,961 Points

What is the difference between the ShoppingCart method that was provided and the new code that you are asked to implement? Is there a case where the outputs of the two functions should match and do they match given your implementation?

A tiny change, and you will complete the challenge!!!

Where you have

"Adding one %s to the cart.%n"

change it to

"Adding 1 of %s to the cart.%n"