Java Java Data Structures Efficiency! Changing Course

Chris Gauthier
PRO
Chris Gauthier
Pro Student 6,434 Points

Passing a List into a class object as the second element.

i believe I have the first of these todo's done, correctly. That wasn't difficult. But Im stuck on the second todo -- passing a video into a class Course as the second element in the list.

Can somebody point me in the right direction, as this will help me finish the chapter.

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 {
  private Video video;

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

    // TODO(2):  Add the newly created video to the course videos as the second video.

  }

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

  }

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

}

2 Answers

Steven Parker
Steven Parker
172,063 Points

You're not quite done with TODO(1). The compiler error that the "Preview" button will show you might help. But I'll also hint that when creating new variables, the declaration must include the type of the variable.

Then for TODO(2), what method does the "course" have that will allow you to access the list of videos in it?
And then, what other method might you chain on that will allow you to add something to a list?

Steven Parker
Steven Parker
172,063 Points

You can disregard my first suggestion, I overlooked the field declaration because I wasn't expecting any new work to be done above the "TODO{1}" line.

But you won't neede "setVideo". Using the list returned by "getVideos", you can call the "add" method which allows you to specify the index where the new item will be inserted.

Chris Gauthier
PRO
Chris Gauthier
Pro Student 6,434 Points

Hello, that didn't quite do it for me, i think. For TODO #1 --
I delcare the variable in the scope of the class:

 private Video video;

And for the second TODO, I can use the getVideos method to get a list of all videos, and then the setVideo method to set the video, but Im confused on how to get the second element in the list.

Can you provide a little more detail here?

Thanks Chris