Java Java Data Structures Efficiency! Add tags to a course

Enyang Mercy
PRO
Enyang Mercy
Pro Student 2,339 Points

fix the addTags methods

What is expected at this stage?

com/example/model/Course.java
package com.example.model;

import java.util.List;
import java.util.HashSet;
import java.util.Set;

public class Course {
  private String mTitle;
  private Set<String> mTags;

  public Course(String title) {
    mTitle = title;
    mTags = new HashSet();
    // TODO: initialize the set mTags
  }

  public void addTag(String tag) {
    mTags.put("add", "Add tags");
    // TODO: add the tag
  }

  public void addTags(List<String> tags) {
    mTags.put("add", "add all the tags in the list");
    // TODO: add all the tags passed in
  }

  public boolean hasTag(String tag) {
    // TODO: Return whether or not the tag has been added
    return false;
  }

  public String getTitle() {
    return mTitle;
  }

}

1 Answer

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,551 Points

Hi there,

For the first method, addTag, it receives a tag as an argument. Use add on mTags using dot notation and pass in the tag the method received.

The second method receives a List of strings. For this, create a for loop to iterate over the list. Inside the method call the first method to add each element of the list to mTags by passing the loop variable value to addTag()..

Let me know how you get on.

Steve.