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

No output from my code in the exercise is displayed, not even original call to addItem().

I cannot find the reason for my code not working. The error message states that 1 Yoda Pez dispenser doesn't appear in the output.

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 */
  }//addItem(Product, quantity)

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

Dan Johnson
Dan Johnson
40,533 Points

The output in your single argument version is missing an "of" in the format string.

However, instead of changing the format string in the second version what you can do is call the two argument version from within the single argument version. Just pass in the product and set the quantity to 1.

This way you can more easily avoid the hard to spot formatting issues and use/repeat less code in the process.

Thank you. So it boils down to me omitting the word "of" in the output string. That slipped right by me. LOL Too bad Java doesn't support default values for method parameters. Perhaps in a future version this very common functionality will make an appearance.

I gather from your explanation that when overriding methods, one can, where possible, call other versions of the method first and then add any little changes required for the current method.

Thank you.

Dan Johnson
Dan Johnson
40,533 Points

Yep. It's about as close as you can get to default argument values in Java. It's a bit more clunky than in other languages but it gets the job done:

public String hexString(int value, String prefix) {
    return String.format("%s%06X", prefix, value);
}

public String hexString(int value) {
    return hexString(value, "0x");
}

With constructors:

public Vector3D(float x, float y, float z) {
    this.x = x;
    this.y = y;
    this.z = z;
}

public Vector3D() {
    this(0.0F, 0.0F, 0.0F);
}