You guessed it. Write the 6 standard methods every self-respecting class should
ID: 3903294 • Letter: Y
Question
You guessed it. Write the 6 standard methods every self-respecting class should have for the Song class.
Here’s the driver class:
public class SongTester{
public static voidmain(String args[]){
private Song[] songs;
songs = new Song[3];
songs[0] = new Song(); //defaults to: “Untitled” for name of song, and // “Undetermined” for name of artist...
songs[1] = new Song (“I Can’t Stop Loving You”,“Ray Charles”);
songs[2] = new Song (“Daylight”, “Matt & Kim”);
System.out.println(songs[0]); //toString called via this statement
SortSearchUtil.insertionSort(songs); // Make the Song object Comparable
System.out.println(“After sorting”);
System.out.println(songs[0]); // Is the first element the right value?
if (SortSearchUtil.linearSearch(songs, new Song (“Chain Gang”,“Sam Cooke”))
{
System.out.println(“Already on file.”);
}
else
{
System.out.println(“Not on file.”);
}
}// end method
}//end class Song Tester
Explanation / Answer
public class Song { private String chain_gang; private String sam_cooke; public Song() { } public Song(String s) { sam_cooke = s; } public Song(String chain_gang, String sam_cooke) { this.chain_gang = chain_gang; this.sam_cooke = sam_cooke; } public String getChain_gang() { return chain_gang; } public void setChain_gang(String chain_gang) { this.chain_gang = chain_gang; } public String getSam_cooke() { return sam_cooke; } public void setSam_cooke(String sam_cooke) { this.sam_cooke = sam_cooke; } @Override public String toString() { return "Song{" + "chain_gang='" + chain_gang + ''' + ", sam_cooke='" + sam_cooke + ''' + '}'; } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Song)) return false; Song song = (Song) o; if (chain_gang != null ? !chain_gang.equals(song.chain_gang) : song.chain_gang != null) return false; return sam_cooke != null ? sam_cooke.equals(song.sam_cooke) : song.sam_cooke == null; } public int hashCode() { int result = chain_gang != null ? chain_gang.hashCode() : 0; result = 31 * result + (sam_cooke != null ? sam_cooke.hashCode() : 0); return result; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.