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 Data Structures - Retired Efficiency! Changing Course

Tristan Smith
Tristan Smith
3,171 Points

Solved: Java: Final Challenge Task 1

See answer below. X_X

And I'm stuck again. On TODO(2) it's giving an unknown symbol error for add.

But since course is a list, it should have add. I can't tell if I'm supposed to create a course list, or if one is already created and being passed in. :/ I have the inclination to write a setter to add to mVideos, but that seems like it's going beyond the scope of Task 1.

Like this

setVideos(Integer num, Video video){
    mVideos.add(num, video);
}

// then this

course.setVideos(1, video);
com/example/model/Course.java
package com.example.model;

import java.util.List;

public class Course {
  private String mName;
  private List<Video> mVideos; 

  public Course(String name, List<Video> videos) {
    mName = name;
    mVideos = videos;
  }

  public String getName() {
    return mName;
  }

  public List<Video> getVideos() {
    return mVideos;
  }

}
com/example/model/Video.java
package com.example.model;

public class Video {
  private String mTitle;

  public Video(String title) {
    mTitle = title;
  }

  public String getTitle() {
    return mTitle;
  }

  public void setTitle(String title) {
    mTitle = title;
  }

}
QuickFix.java
import com.example.model.Course;
import com.example.model.Video;

import java.util.Map;

public class QuickFix {

  public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
    Video forgottenVideo = new Video("The Beginning Bits");

    // TODO(2):  Add the newly created video to the course videos as the second video.
    course.add(1, forgottenVideo);

  }

  public void fixVideoTitle(Course course, String oldTitle, String newTitle) {

  }

  public Map<String, Video> videosByTitle(Course course) {
    return null;
  }

}

3 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Tristan - Actually, it's not necessary to add a setter since we have the getVideos() method. You can call it on course and then call add:

course.getVideos().add(1, forgottenVideo);
Tristan Smith
Tristan Smith
3,171 Points

Thanks for the response.

Isn't that just a getter though? I thought the practice was "getter returns(gets) values/info, setters set values/info".

Kourosh Raeen
Kourosh Raeen
23,733 Points

That's true - it is a getter, but what it returns is the member variable mVideos and then you can call add on it.

Tristan Smith
Tristan Smith
3,171 Points

I see, that makes sense. Thank you for your help.

Is that how someone should use getters though? I know it's possible, but doesn't it defeat the purpose of creating getters/setters.

Tristan Smith
Tristan Smith
3,171 Points

Disregard, it passed with adding a setter to course.java

Sometimes I get so confused with these tasks..

  public void setVideos(Integer num, Video video){
    mVideos.add(num, video);
  }
Kourosh Raeen
Kourosh Raeen
23,733 Points

I guess if I wanted to create the method you created I'd call it addVideo(). Typically, a setter accepts a single argument and assigns that to a member variable like:

public void setName(String name) {
    mName = name;
}
Tristan Smith
Tristan Smith
3,171 Points

That would make more sense, "set videos" doesn't sound very OO.

Thanks again for your help! It seems even though my method also works, it's not necessarily what the task was looking for.. I'll be marking your answer as correct.