I am trying to get to the videos by their names, i want you use the videosByTitl
ID: 3851725 • Letter: I
Question
I am trying to get to the videos by their names, i want you use the videosByTitle() method to generate the proper map of title to video.. below is the code i have written and i have attched the other classes so you can understand it better. Please let me know what I am doing wrong and if you can help me, please comment so I understand . Thanks
import java.util.Map;
import java.util.HashMap;
import java.util.List;
public class QuickFix {
public void addForgottenVideo(Course course) {
// TODO(1): Create a new video called "The Beginning Bits"
List<Video> video = course.getVideos();
Video newVideo = new Video ("The Beginning Bits");
video.add(1, newVideo);
}
public void fixVideoTitle(Course course, String oldTitle, String newTitle) {
}
public Map<String, Video> videosByTitle(Course course) {
Map<String, Video> myHashMap = new HashMap <String, Video>();
List<Video> video = course.getVideos();
String key = course.getName();
Video value = new Video(null);
for ( int i = 0; i < course.getVideos().size(); i++){
for (Video newVideo : video){
video.add(i, newVideo);
}
}
myHashMap.put(key, value);
myHashMap.get(key);
return myHashMap;
}
}
============================================================================================
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 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;
}
}
Explanation / Answer
Here is the fixed code with comments inline. Hope it helps. Let me know if you have any doubts, by posting a comment. If happy with the answer , please don't forget to give a thumbs up :-). Thank you very much.
import java.util.Map;
import java.util.HashMap;
import java.util.List;
public class QuickFix {
public void addForgottenVideo(Course course) {
// TODO(1): Create a new video called "The Beginning Bits"
List<Video> videos = course.getVideos();
Video newVideo = new Video ("The Beginning Bits");
videos.add(1, newVideo);
}
public boolean fixVideoTitle(Course course, String oldTitle, String newTitle) {
Map<String, Video> map = videosByTitle(course); // get the map of title to video
Video video = map.get(oldTitle); //get the video corresponding to oldTitle
if(video == null) // the oldTitle is not existing
return false;
else
{
video.setTitle(newTitle); //set the new title
return true;
}
}
public Map<String, Video> videosByTitle(Course course) {
Map<String, Video> myHashMap = new HashMap <String, Video>();
//get the list of videos in this course object
List<Video> videos = course.getVideos();
Video video;
//iterate as many times as the size of the list of videos
for(int i = 0 ; i < videos.size(); i++)
{
video = videos.get(i); //get the ith video in videos list
myHashMap.put(video.getTitle(), video); //put the video object in map using its title as key
}
return myHashMap; //return the hashmap created
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.