Write the Music class in Java (including the equals method shown on page 3 of Le
ID: 3820441 • Letter: W
Question
Write the Music class in Java (including the equals method shown on page 3 of Lesson 1). The toString method should return a String with the values of each instance variable.
Music
-title: String
-composer: String
-opus: int
+Music()
+Music(ti: String, comp: String, op: int)
+getTitle(): String
+getComposer(): String
+getOpus(): int
+setTitle( newTitle: String )
+setComposer( newComp: String)
+setOpus( newOpus: int )
+equals(other: Object): boolean
+toString(): String
Music
-title: String
-composer: String
-opus: int
+Music()
+Music(ti: String, comp: String, op: int)
+getTitle(): String
+getComposer(): String
+getOpus(): int
+setTitle( newTitle: String )
+setComposer( newComp: String)
+setOpus( newOpus: int )
+equals(other: Object): boolean
+toString(): String
Explanation / Answer
Here is the Music.java class for you:
class Music
{
String title;
String composer;
int opus;
public Music()
{
title = "Not assigned";
composer = "Anonymous";
opus = -1;
}
public Music(String ti, String comp, int op)
{
title = ti;
composer = comp;
opus = op;
}
public String gettitle() { return title; }
public String getComposer() { return composer; }
public int getOpus() { return opus; }
public void setTitle(String newTitle) { title = newTitle; }
public void setComposer(String newComp) { composer = newComp; }
public void setOpus(int newOpus) { opus = newOpus; }
public boolean equals(Object other)
{
Music otherMusic = (Music)other;
return title.equals(otherMusic.title) && composer.equals(otherMusic.composer);
}
public String toString()
{
String output = ("Title: " + title);
output += ("Composer: " + composer);
output +=("Opus: " + opus);
return output;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.