Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA. -Pretend you are putting in some initial work for a music library service.

ID: 3721689 • Letter: J

Question

JAVA.

-Pretend you are putting in some initial work for a music library service.

-You are going to create a Song class. This class should have a PopulateSong() method that asks the user (or reads from a file, your choice) for the Title, Artist, ReleaseYear, and Length of the song (in seconds). Please also create a PrintSongDetails() method.

-You can organize it anyway you see fit, but you should use Try-Catch-Throw of Exceptions when you are acquiring data. For instance, release year should not be in the future or a non-integer, the length of a song can't be negative, etc. One or more of the Exceptions used should throw an Exception with a custom message from a method to be handled lower down in the call-stack.

Please explain how this problem is done. I have asked this same question before but I am not sure how to apply the code provided. Thank you.

Explanation / Answer

Song. java // Main Function
---------------------------------------------
import java.util.Date;
import java.util.Scanner;
public class Song {
SongObject s= new SongObject();
@SuppressWarnings("deprecation")
public void PopulateSong() throws ReleaseYearException, SongLengthException
{
Scanner sc= new Scanner(System.in);
System.out.println("Please Enter the Song Details");
System.out.println("Song Title");
s.setTitle(sc.nextLine());
System.out.println("Artist: ");
s.setArtist(sc.nextLine());
System.out.println("Release Year");
String year=sc.nextLine();
Date today=new Date();
if((new Integer(year)<0)||((new Integer(year))>(today.getYear()+1900)))
{
throw new ReleaseYearException("Release Year is future date or it is a negative number");
}
s.setReleaseYear(year);
System.out.println("Length Of Song In Seconds:");
long len=sc.nextLong();
if(len<0)
{
throw new SongLengthException("Length of song is Negative Value");
}
s.setLength(len);
}
public void PrintSongDetails()
{
System.out.println("Song Details:");
System.out.println("-----------------------------------------------------");
System.out.println("Title: :"+s.getTitle());
System.out.println("Artist: :"+s.getArtist());
System.out.println("Release Year: :"+s.getReleaseYear());
System.out.println("Length(sec): :"+s.getLength());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Song sng= new Song();
sng.PopulateSong();
sng.PrintSongDetails();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
------------------------------------------------------------
SongObject.java// Object to stor song releated Data
--------------------------

public class SongObject {
String Title;
String Artist;
String ReleaseYear;
long length;
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getArtist() {
return Artist;
}
public void setArtist(String artist) {
Artist = artist;
}
public String getReleaseYear() {
return ReleaseYear;
}
public void setReleaseYear(String releaseYear) {
ReleaseYear = releaseYear;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
}
---------------------------------------------------------------------------------
ReleaseYearException// Custome Exception for Release Year

--------------------------
public class ReleaseYearException extends Exception {
ReleaseYearException(String S)
{
super(S);
}
}
------------------------------------------------
SongLengthException.java// Custome Exception for Length of Song
------------------------------------
public class SongLengthException extends Exception{
public SongLengthException(String S) {
// TODO Auto-generated constructor stub
super(S);
}
}