Java Java Data Structures Efficiency! Add tags to a course

Chris Gauthier
PRO
Chris Gauthier
Pro Student 6,434 Points

Initializing a set in a constructor

package com.example.model;

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

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

  public Course(String title, Set<String> newTags) {
    mTitle = title;
    // TODO: initialize the set mTags
    mTags = newTags;
  }

  public void addTag(String tag) {
    // TODO: add the tag
  }

  public void addTags(List<String> tags) {
    // 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;
  }

}

Why doesn't this work?

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

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

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

  public Course(String title, Set<String> newTags) {
    mTitle = title;
    // TODO: initialize the set mTags
    mTags = newTags;
  }

  public void addTag(String tag) {
    // TODO: add the tag
  }

  public void addTags(List<String> tags) {
    // 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;
  }

}

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,352 Points

Hi Chris,

for the first challenge you were asked to initialize mTags in the constructor. For this, You don't need to pass Set<String> newTags into the constructor. Just initialize mTags variable within the constructor. For that, use HashSet. And also make sure that HashSet is included as an import at the top.

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

Does it make sense?

Grigorij Schleifer
Grigorij Schleifer
10,352 Points

NICE!!!! Glad to hear that, Chris ...