Code from Project 1 import javatimeLocalDateTime public clas
Code from Project 1:
import java.time.LocalDateTime;
public class DigitalMedia {
private String name;
private long size;
private LocalDateTime dateCreated;
private LocalDateTime dateModified;
public DigitalMedia(String name, long size, LocalDateTime dateCreated,
LocalDateTime dateModified) {
this.name = name;
this.size = size;
this.dateCreated = dateCreated;
this.dateModified = dateModified;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setSize(long size) {
this.size = size;
}
public long getSize() {
return this.size;
}
public LocalDateTime getDateCreated() {
return this.dateCreated;
}
public void setDateModified(LocalDateTime dateModified) {
this.dateModified = dateModified;
}
public LocalDateTime getDateModified() {
return this.dateModified;
}
@Override
public boolean equals(Object MyBook) {
if (MyBook instanceof DigitalMedia) {
if (((DigitalMedia)MyBook).getDateCreated() == this.getDateCreated() &&
((DigitalMedia)MyBook).getDateModified() == this.getDateModified() &&
((DigitalMedia)MyBook).getName() == this.getName() &&
((DigitalMedia)MyBook).getSize() == this.getSize()) {
return true;
} else return false;
} else return false;
}
/**
* toString()
*
* @return
*/
public String toString() {
return \"Name: \" + this.name +
\"\ Size: \" + this.size +
\"\ Date Created: \" + this.dateCreated.toString() +
\"\ Date Modified: \" + this.dateModified.toString();
}
}
Write the following Java subclasses of the DigitalMedia class from Project 1:
Song to represent a song in a music collection. The Song title is its file name. In addition to the inherited
instance variables from the super class, data members of the Song class are String objects representing the artist’s name, and the album where the song can be found. These instance variables for the class are to have private access modifiers so you will need to write the appropriate methods to allow a client to access and modify the data values. In accordance with good programming practice, you are to override the equals and toString methods inherited from Object. Two Song objects are considered equal if they have the same title, artist, album, and size. In addition, this class is going to be used in a Collection that is to be sorted, so it is to implement the Comparable interface.
Image to represent a digital image file. An image has the additional instance variables width and height, both of type int. As for the Song class, implement getters, setters, equals, toString and the Comparable interface for the Image class. Two images are considered equal if they have the same name, size, width, and height.
A second class is to be written called MediaList that contains a main method that reads in a file name via the command line. The file is a listing of songs and images. In the file, each media item is on a single line.
For Songs, the line is formatted with the letter s, song name, the artist name, the album, and size in which each element is separated by a colon (:) and varying amounts of white space. For example,
S : Mykonos.mp3 : Fleet Foxes :Mykonos - Single: 9,507,904
S : He Doesn\'t Know Why.mp3 : Fleet Foxes :Fleet Foxes : 7,080,711
For Images, the line is formatted with the letter i, the image name, the width, the height, and size in which each element is separated by a colon (:) and varying amounts of white space. For example,
I : Beach.jpg : 2048 : 1536 : 1,163,207
I: IMG_3658.JPG: 2448 : 3264 : 1,384,318
The program is to:
Read in the input file creating Song and Image objects from the data contained in the lines of the file
Store the Songs and Images in a single data structure of type DigitalMedia.
Prompt the user to enter either S or I to see a display of all of the DigitalMedia of that type.
If “I” is entered, display a formatted list of all the images in alphabetic order by image name.
If “S” is entered, display a formatted list of all the songs, grouped by album and in alphabetical order by song
title.
Solution
Dear,
//Song.java
//class Song implements the Comparable interface
public class Song implements Comparable {
private String title; //for song title
private String artist; //song\'s artist name
private String album; //song\'s album name
//no argument constructor initializes the class members to default variables
public Song()
{
title=\"\";
artist=\"\";
album=\"\";
}
// parameterized constructor
public Song(String t,String a,String al)
{
setTitle(t);
setArtist(a);
setAlbum(al);
}
//sets the title of the song
public void setTitle(String t)
{
// it checks wether the title is valid string or not
if(t instanceof String )
title=t;
else
title=\"\";//other wise it sets to null
}
//sets artist of the song
public void setArtist(String a)
{
if(a instanceof String )
artist=a;
else
artist=\"\";
}
//sets alvum of the song
public void setAlbum(String al)
{
if(al instanceof String )
album=al;
else
album=\"\";
}
//gets title of the song
public String getTitle()
{
return title;
}
//gets album of the song
public String getAlbum()
{
return album;
}
//gets artist of the song
public String getArtist()
{
return artist;
}
// overriden toString method that prints Song object on the console
public String toString()
{
return String.format(\"\ %s: %s\ %s: %s\ %s: %s\ \",\"Song Name\",getTitle(),\"Album\",getAlbum(),\"Artist\",getArtist());
}
// overriden equals methode which compares the artist names of the song
public boolean equals(String a)
{
if(this.artist.equals(a))
return true;
else
return false;
}
// this will finds the all the members are equal or not in the song
public boolean equals(Song s)
{
if((this.album.equals(s.album))&&(this.artist.equals(s.artist))&&(this.title.equals(s.title)))
return true;
else
return false;
}
//overriden compareTo method which compares to Song class objects titles
public int compareTo(Object s)
{
String t=((Song) s).getTitle();
if(this.title.compareTo(t)>0)
return 1;
else if(this.title.compareTo(t)<0)
return -1;
else
return 0;
}
}
//SongList.java
import java.util.*;
import java.io.*;
//class SongList
public class SongList {
// main method
public static void main(String[] args)throws FileNotFoundException {
//prints information of the student who done this project
printHeading();
//array of Song class
Song s[]=new Song[70];
int count=0; //counter for array
//opens the file which is specified at command line
Scanner file=new Scanner(new File(args[0].toString()));
//reads data from file and stores in array
while(file.hasNext())
{
StringTokenizer st=new StringTokenizer(file.nextLine(),\":\");
s[count]=new Song(st.nextToken(),st.nextToken(),st.nextToken());
count++;
}
//gets artist name from the user
System.out.print(\"Enter artist name: \");
Scanner input =new Scanner(System.in);
String art=input.nextLine();
//finds the given artist songs in the array and displays
boolean flag=false;
for(int i=0;i<count;i++)
if(s[i].equals(art))
{
System.out.println(s[i]);
flag=true;
}
if(!flag)
System.out.println(\"No song was found with this artist\");
}
private static void printHeading()
{
System.out.println(\"Student Name: John\");
System.out.println(\"Project Number: P405\");
System.out.println(\"Course Identifier: CMSC 246\");
System.out.println(\"Current semester: Spring\");
System.out.println(\"Year: 2011\ \ \");
}
}






