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

Im lost with where to start

Can you please help me with the functions in the QuickFix.java over the next few tasks? I've included other code files for your reference only. I'm going to pass in this course to the addForgottenVideo method. For this task, can you please follow the TODO in the comments in the method?

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"

    // TODO(2):  Add 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;
  }

}

3 Answers

Stephen Wall
PLUS
Stephen Wall
Courses Plus Student 27,294 Points
public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
// Just use the Video class that is provided for you
    Video video = new Video("The Beginning Bits"); 

    // TODO(2):  Add the newly created video to the course videos as the second video.
// You can specify the index with the add method, but remember arrays start with 0
//  so second place is 1. 
    course.getVideos().add(1, video); 
  }

Spoiler Alert!

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"
    // Just use the Video class that is provided for you
    Video video = new Video("The Beginning Bits"); 

    // TODO(2):  Add the newly created video to the course videos as the second video.
    // You can specify the index with the add method, but remember arrays start with 0
    //  so second place is 1. 
    course.getVideos().add(1, video); 
  }

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

  }

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

}
Dan Johnson
Dan Johnson
40,533 Points

For the first part of the challenge you just need to:

  1. Create a new instance of Video with "The Beginning Bits" as the title.
  2. Get the list of videos from the course argument using the getVideos method.
  3. Add the new video you created to the list of videos you retrieved at index 1.

I got step 1 of 3, but now I'm stuck on the second part.

Dan Johnson
Dan Johnson
40,533 Points

For step two it'll go something like this:

  1. Create a new map

  2. For every video in the course video list, put a new key value pair in the map where:

    • The key is the video title
    • The value is the video reference
  3. Return the map