Question: Homework help! I have some classes that relate to one another through inheritance. FIrst theres the LibraryMain code: 1 import java.util.Scanner; 2 /* This application

Homework help! I have some classes that relate to one another through inheritance.

FIrst theres the LibraryMain code:

1 import java.util.Scanner; 2 /* This application manages a collection of media items for a user. The items 3 * are representeed by MediaItem objects. The items are managed by a MediaList object. 4 * Their are currently three types of MediaItems: books (electronic format), movies, and songs. 5 * Each type is modeled as a subclass of MediaItem. 6 * A user has several options that are displayed in a menu format. 7 * This class runs a console interface between a user and the mediaList 8 */ 9 import java.io.*; 10 11 public class LibraryMain { 12 13 public static void main(String[] args) throws IOException{ 14 System.out.println("My Media Library"); 15 Scanner scan = new Scanner(System.in); 16 MediaList mediaList = new MediaList(5); 17 boolean keepGoing = true; 18 String userStr = ""; 19 int position; 20 21 while(keepGoing) { 22 System.out.println("Main Menu:"); 23 System.out.println("Enter A to add a new media item."); 24 System.out.println("Enter F to find media items."); 25 System.out.println("Enter P to view all media items."); 26 System.out.println("Enter X to quit."); 27 System.out.println(""); 28 userStr = scan.nextLine(); 29 30 if (userStr.equalsIgnoreCase("A")){ 31 MediaItem item = null; 32 System.out.println("Enter the title: "); 33 String title = scan.nextLine(); 34 System.out.println("Enter the author/artist/director: "); 35 String author = scan.nextLine(); 36 System.out.println("Enter the genre: "); 37 String genre = scan.nextLine(); 38 System.out.println("What type of media? Enter B for book, M for movie, S for song: "); 39 String type = scan.nextLine(); 40 if(type.equalsIgnoreCase("B")){ 41 System.out.println("Enter the number of pages: "); 42 String pages = scan.nextLine(); 43 System.out.println("Enter the preferred font size: "); 44 String font = scan.nextLine(); 45 item = new Book(title, author, genre, Integer.parseInt(pages), Double.parseDouble(font)); 46 } 47 else if(type.equalsIgnoreCase("M")){ 48 System.out.println("Enter the playing time (minutes): "); 49 String playTime = scan.nextLine(); 50 System.out.println("Enter the lead actor: "); 51 String actor = scan.nextLine(); 52 System.out.println("Enter the release year YYYY: "); 53 String year = scan.nextLine(); 54 item = new Movie(title, author, genre, Integer.parseInt(playTime), 55 actor, year); 56 } 57 else if(type.equalsIgnoreCase("S")){ 58 System.out.println("Enter the playing time (minutes): "); 59 String playTime = scan.nextLine(); 60 System.out.println("Enter the album: "); 61 String album = scan.nextLine(); 62 System.out.println("Enter the label: "); 63 String label = scan.nextLine(); 64 item = new Song(title, author, genre, Double.parseDouble(playTime), 65 album, label); 66 } 67 else 68 System.out.println("Unrecognized type."); 69 mediaList.addItem(item); 70 } 71 else if (userStr.equalsIgnoreCase("F")){ 72 System.out.println("Enter T to search by title, G to search by genre:"); 73 userStr = scan.nextLine(); 74 if(userStr.equalsIgnoreCase("T")){ 75 System.out.println("Enter the title:"); 76 String title = scan.nextLine(); 77 MediaItem[] items = mediaList.getItemsByTitle(title); 78 if(items.length>0){ 79 String listStr = getItemListAsString(items); 80 System.out.println(listStr); 81 } 82 else 83 System.out.println("Could not find "+title+" in the list."); 84 } 85 else if(userStr.equalsIgnoreCase("G")){ 86 System.out.println("Enter the genre:"); 87 String genre = scan.nextLine(); 88 MediaItem[] items = mediaList.getItemsByGenre(genre); 89 if(items.length>0){ 90 String listStr = getItemListAsString(items); 91 System.out.println(listStr); 92 } 93 else 94 System.out.println("Could not find "+genre+" in the list."); 95 } 96 else 97 System.out.println("Unrecognized search type."); 98 } 99 else if (userStr.equalsIgnoreCase("P")){ 100 System.out.println("Your media: "); 101 MediaItem[] items = mediaList.getItemList(); 102 String listStr = getItemListAsString(items); 103 System.out.println(listStr); 104 } 105 else if(userStr.equalsIgnoreCase("X")) 106 keepGoing = false; 107 else 108 System.out.println("Unrecognized input."); 109 } 110 System.out.println("Bye for now."); 111 scan.close(); 112 } 113 114 /* This method returns a String which consists of the String 115 * representations of all MediaItems in the array passed in. 116 * Assume the itemList does not contain NULL values. 117 * Assume the itemList is not null, and length >=0. 118 */ 119 public static String getItemListAsString(MediaItem[] itemList){ 120 StringBuilder sb = new StringBuilder(); 121 if(itemList.length>0){ 122 for(int i=0; i

Then theres the MediaList code:

1 2 /* This class encapsulates a list of media items in a user's collection. 3 * The list is implemented as an array of type MediaItem. 4 * Media items are either a book (electronic format), movie, or song. 5 * Each type of media item is represented by an instance of the Book, Movie, or Song class. 6 * These three classes are subclasses of MediaItem. The array stores media items as 7 * references of type MediaItem. 8 */ 9 public class MediaList { 10 //Class member variable declarations: 11 12 13 /* Constructor that initializes the member variables. The array is 14 * created using the initial length passed in to the constructor. 15 * The initialLength is assigned to the initial length passed in to the constructor. 16 * The numItems is initialized to 0. 17 * Any other member variables are initialized as well. 18 */ 19 public MediaList(int initialLen){ 20 } 21 22 /* Add the newItem passed in to the next available cell in the itemList. 23 * Available cells have the vaue NULL. The numItems variable may be used 24 * to keep track of the index of the next available cell. 25 * For example, if the list contained: item1, item2, NULL, NULL, 26 * the next available cell is at index 2. 27 */ 28 public void addItem(MediaItem newItem){ 29 } 30 31 /* This method returns an array that contains only MediaItem objects whose 32 * title matches the targetTitle passed in. 33 * The array returned does not contain any NULL values. 34 * This method returns an array of length 0 if there are no matches. 35 * This method may call the getOnlyItems method. 36 */ 37 public MediaItem[] getItemsByTitle(String targetTitle){ 38 return null; 39 } 40 41 /* This method returns an array that contains only MediaItem objects whose 42 * genre matches the targetGenre passed in. 43 * The array returned does not contain any NULL values. 44 * This method returns an array of length 0 if there are no matches. 45 * This method may call the getOnlyItems method. 46 */ 47 public MediaItem[] getItemsByGenre(String targetGenre){ 48 return null; 49 } 50 51 /* This method returns an array of all of the MediaItem objects that are 52 * stored in the itemList. The array returned does not contain any NULL 53 * values. This method returns an array of length 0 if the itemList is empty. 54 * This method may call the getOnlyItems metho 55 */ 56 public MediaItem[] getItemList(){ 57 return null; 58 } 59 60 /* Returns the number of items stored in the itemList. 61 */ 62 public int getNumItems(){ 63 return -5; 64 } 65 66 /* Returns true if the itemList contains no MediaItems, false otherwise. 67 */ 68 public boolean isEmpty(){ 69 return false; 70 } 71 72 /****** Private, "helper" method section ******/ 73 74 /* Creates a new array that is double the size of the array passed in, copies the data 75 * from that array to the new array, and returns the new array. 76 * Note that the new array will contain the items from the previous array followed by NULL values. 77 */ 78 private MediaItem[] expandList(MediaItem[] inputList){ 79 return null; 80 } 81 82 /* A full item list is an array where all cells contain an item. That 83 * means there is no cell that contains NULL. 84 * This method returns true if all cells in the array contain a String 85 * object, false otherwise. 86 */ 87 private boolean isFull(){ 88 return true; 89 } 90 91 /* 92 * This method takes an array of MediaItems as an input as well as 93 * the number of MediaItems on that array. The input array may contain 94 * some NULL values. 95 * This method returns an array that contains only the MediaItems in 96 * the input array and no NULL values. 97 * It returns an array of length 0 if there are no MediaItems in the input array. 98 */ 99 private MediaItem[] getOnlyItems(MediaItem[] inputArray, int size){ 100 return null; 101 } 102 }

Then there's the MediaItem code:

1 /** 2 * This class encapsulates the data required to represent a MediaItem. 3 * The attributes common to all MediaItems are: title, author and genre. 4 * Subclasses should override the toString method. 5 **/ 6 public class MediaItem { 7 8 private String title; 9 private String author; 10 private String genre; 11 12 /* Subclasses may add specific parameters to their constructor's 13 * parameter lists. 14 */ 15 public MediaItem(String title, String author, String genre){ 16 this.title = title; 17 this.author = author; 18 this.genre = genre; 19 } 20 // get method for the title 21 public String getTitle(){ 22 return title; 23 } 24 // get method for the author 25 public String getAuthor(){ 26 return author; 27 } 28 // get method for the genre 29 public String getGenre(){ 30 return genre; 31 } 32 33 // Subclasses should override. 34 public String toString(){ 35 return title+", "+author+", "+genre; 36 } 37 }

Then the Book, Song, Movie code respectively all Extend MediaItem:

Book code:

1 /** 2 * This class encapsulates the data required to represent a book in electronic format 3 * in a collection of MediaItems. It derives from MediaItem. 4 * In addition to its superclass attributes, the attributes of a book are: 5 * number of pages and font size. 6 **/ 7 public class Book extends MediaItem { 8 9 private int numPages; 10 private double fontSize; 11 12 /* Constructor. 13 */ 14 15 // get method for the number of pages 16 17 // get method for the font size 18 19 /* Override the superclass toString method. Use a call to the superclass toString method 20 * to get the superclass attributes. 21 * The return should have the format: 22 * Book: [title], [author], [genre], [numPages], [fontSize] 23 * For example: 24 * "Book: Snow Crash, Neil Stephenson, sci fi, 480, 3.5" 25 */ 26 27 }

Song code:

1 /** 2 * This class encapsulates the data required to represent a song in a collection 3 * of MediaItems. It derives from MediaItem. 4 * In addition to its superclass attributes, the attributes of a song are: 5 * playing time, album, and label. 6 **/ 7 public class Song extends MediaItem { 8 9 private double playTime;//minutes 10 private String album; 11 private String label; 12 13 /* Constructor 14 */ 15 16 // get method for the play time 17 18 // get method for the album 19 20 // get method for the label 21 22 /* Override the superclass toString method. Use a call to the superclass toString method 23 * to get the superclass attributes. 24 * The return should have the format: 25 * Song: [title], [author], [genre], [playTime], [album], [label] 26 * For example: 27 * "Song: Humble, Kendrick Lamar, hip hop, 2.57, Damn, Top Dawg" 28 */ 29 30 }

Movie code:

1 /** 2 * This class encapsulates the data required to represent a movie in a collection 3 * of MediaItems. It derives from MediaItem. 4 * In addition to its superclass attributes, the attributes of a movie are: 5 * playing time, lead actor, and year of release in the form YYYY (i.e. 2018). 6 **/ 7 public class Movie extends MediaItem { 8 9 private int playTime;// in minutes 10 private String leadActor; 11 private String releaseYear; //in the form YYYY 12 13 /* Constructor 14 */ 15 16 17 // get method for the playing time 18 19 // get method for the lead actor 20 21 // get method for the release year 22 23 /* Override the superclass toString method. Use a call to the superclass toString method 24 * to get the superclass attributes. 25 * The return should have the format: 26 * Movie: [title], [author], [genre], [playTime], [leadActor], [releaseYear] 27 * For example: 28 * "Movie: Black Panther, Coogler, fantasy, 134, Chadwick Boseman, 2018" 29 */ 30 31 }

And here is the test code:

1 import org.junit.Assert; 2 import static org.junit.Assert.*; 3 import org.junit.Before; 4 import org.junit.Test; 5 import java.lang.reflect.*; 6 7 public class MediaListTest { 8 9 private MediaList mediaList; 10 11 /** Fixture initialization (common initialization 12 * for all tests). **/ 13 @Before public void setUp() { 14 mediaList = new MediaList(6); 15 } 16 17 /** Test for required instance variable declarations. **/ 18 @Test 19 public void initialLengthNameTest() { 20 Field[] fields = MediaList.class.getDeclaredFields(); 21 boolean wasDeclared = false; 22 for(int i=0;i

Basically all that needs to be done is fill in the blanks with the methods and returns. Any help I can have with any of the classes is great help thanks!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!