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

solange tuyisenge
solange tuyisenge
3,081 Points

getting an error on java-objects/delivering-the-mvp/defaulting-parameters challenge

Hello am getting this error ';' expected below are my codes

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
    public void addItem(Product item)
     cart.addItem(dispenser, 1);


Thank you for assisting


3 Answers

Right, I'm back at my computer now so I'll add a little more to that answer.

In this challenge you are creating a new method that is called the same as an existing one, addItem.

The difference is that the method you are creating has a different method signature; in this case, the number of parameters are different. The existing addItem method receives a Product and a quantity. It then adds quantity of Product into your ShoppingCart. The new method just receives a Product as it will always just add one of that Product to your cart.

The line of code that needs to work is:


As you can see, this doesn't fit the existing addItem method, which takes two parameters:

  public void addItem(Product item, int quantity) { // two parameters
    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. 

The existing addItem method has lots of code hidden - we don't want to be duplicating any of that. But we need to change something so that it takes only one parameter. The existing method remains a requirement; we can't delete it as it is needed for instance where multiple items are added to the cart.

The new method takes a single parameter, an instance of Product and results in one of that product being added to the cart. The method signature takes one parameter:

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

Inside that method, we call the existing addItem method but default the quantity to 1. That's what you've written already - it was just in the wrong place!

Now, if you've uncommented the line in Example.java your code should work. But remove your method definition from around it first.

I hope that helps.


Ciaran McNally
Ciaran McNally
7,052 Points

Thanks for explaining this, took me some time to get my head round it. I'm hoping this will come more naturally soon.

:+1: :smile:

Hi Solange,

You need to amend the method in the ShoppingCart class, not in Example. The method you have created is pretty much right; it is just in the wrong place.

Note, that once in the correct class, you'll not have an instance to call it from; that's fine. You have the correct concept; call the exisiting method from inside the new one.

Have a go and let me know how you get on.


Rob B
Rob B
11,677 Points

I'm glad that someone is able to explain what is needed. I've taken an Intro course to Java and I am having trouble understanding what is needed in these exercises lately. (It's partially why I stepped away from the track for a while)

Hi Rob,

Give the courses a go and shout in the Community pages if you get stuck. Someone will be able to help - you can @ mention me if you need urgent help.


solange tuyisenge
solange tuyisenge
3,081 Points

Thank you very much Steve. you helped me to understand and get it done.



No problem. Glad to help.