Need help with this exercise. I must create a class Song array. The duration fie
ID: 3911532 • Letter: N
Question
Need help with this exercise. I must create a class Song array. The duration field is a float number giving the length of the song in minutes. The Song constructor will create a song with the fields set to default values (String to “”, float to 0.0, int to 0). The setSong method will assignment the parameter values to the corresponding fields if the parameter value is a valid value. The toStringmethod will return a String representation of the song information (including all the fields) in a formatted way.
- title: String
- writer: String
- singer: String
- duration: float
- genre: String
- year: int
+Song()
+setSong(String, String, String, float, Sting, int)
+getTitle(): String
+getWriter(): String
+getSinger(): String
+getDuration(): float
+getGenre(): String
+getYear(): int
+toString():String
Explanation / Answer
//Test java program that tests the Song
//class and print the data on console.
//TestSong.java
public class TestSong
{
public static void main(String[] args) {
//Create an instance of Son class
Song song=new Song();
//print song object
System.out.println("Default song object:");
System.out.println(song.toString());
//Set values of song object using setSong
song.setSong("ABC", "Milan", "Micheal", 300, "Solo", 2018);
//print song object
System.out.println("Song object:");
System.out.println(song.toString());
}
}
---------------------------------------------------------------------------------------------------
/**
* The java class Song.java that has private
* date members to store title, writer, singer,
* duration, genre and year.
* The class has default constructor
* and has method to set values for the
* variables of the class.
* The class also contains methods
* to get the values of the private data
* members and toString method to return
* the string description of song class object.
* */
//Song.java
public class Song
{
//private data members
private String title;
private String writer;
private String singer;
private float duration;
private String genre;
private int year;
/*Default constructor*/
public Song()
{
this.title="";
this.writer="";
this.singer="";
this.duration=0.0f;
this.genre="";
this.year=0;
}
/**Method to set values*/
public void setSong(String title, String writer, String singer,
float duration, String genre, int year)
{
this.title=title;
this.writer=writer;
this.singer=singer;
this.duration=duration;
this.genre=genre;
this.year=year;
}
/**Method that returns title of song*/
public String getTitle()
{
return title;
}
/**Method that returns writer of song*/
public String getWriter()
{
return writer;
}
/**Method that returns singer of song*/
public String getSinger()
{
return singer;
}
/**Method that returns duration of song*/
public float getDuration()
{
return duration;
}
/**Method that returns genre of song*/
public String getGenre()
{
return genre;
}
/**Method that returns year of song*/
public int getYear()
{
return year;
}
//Override the toString method that returns
//song description
public String toString()
{
return String.format("%-15s %-15s %-15s %-5.2f %-15s %-5d",
title,
writer,
singer,
duration,
genre,
year
);
}
}
---------------------------------------------------------------------------------------------------
Sample Output:
Default song object:
0.00 0
Song object:
ABC Milan Micheal 300.00 Solo 2018