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 trialJuan Santiago
3,766 PointsWhat can I do to improve the code? I did some research and I'm kind of lost D:
I think that the code needs some "for" loop for adding the videos to the list. Help, please.
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;
}
}
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;
}
}
import com.example.model.Course;
import com.example.model.Video;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class QuickFix {
public void addForgottenVideo(Course course) {
// TODO(1): Create a new video called "The Beginning Bits"
Video video = new Video("The Beginning Bits");
// TODO(2): Add to the course videos as the second video.
List<Video> videoList = course.getVideos();
videoList.add(1, video);
}
public void fixVideoTitle(Course course, String oldTitle, String newTitle) {
}
public Map<String, Video> videosByTitle(Course course) {
Map<String,Video> sortedByTitle= new HashMap();
return sortedByTitle;
}
}
1 Answer
Emily Cain
5,850 PointsSo part of the issue seems to be the difference between public and private variables.
In general, it's best to make member variables such as mVideos in the Course class private. Looks like you did that, good job!
The trouble happens when you want to look at or modify those variables from another class. I think this is what you're trying to do here:
List<Video> videoList = course.getVideos();
videoList.add(1, video);
Here's what it seems you are trying to do:
-point the program towards the Course called "course" -modify the mVideos member variable by adding a video to it
However, here's what the above code is ACTUALLY doing:
-creating a new List of Videos called videoList within the QuickFix class. The contents of this list are created by copying the list mVideos from "course." -adding a new video to videoList
Which is great if all you want to do is add a video to this new variable that only exists in your QuickFix object, but a much more logical thing to do would be to actually modify the mVideos list in the course!
Since mVideos is a private member variable in the Course class, you can't modify it directly from another class. Instead, you have to create a method in Course to modify mVideos. I'll write you a basic outline and see if you can figure out the rest:
public void addVideo(Video video) {
//code to add "video" to mVideos. Remember, you are now working within the Course class, so you can add things directly to mVideos using the methods of the List class. (https://docs.oracle.com/javase/7/docs/api/java/util/List.html)
}
Does this answer your question?
EDIT: Of course you'll then have to call your addVideo method on the Course in the code I excerpted above, instead of calling "add" on videoList.