Create a class named MusicalComposition that contains fields for title, composer, and year written. Include a constructor that requires all three values and an appropriate display function. The child class NationalAnthem contains an additional field that holds the name of the anthem’s nation. The child class constructor requires a value for this additional field. The child class also contains a display function. Write a main() function in class MusicDemo that instantiates objects of each class and demonstrates that the functions work correctly.
public class MusicalComposition {
private String title;
private String composer;
private int yearWritten;
public MusicalComposition(String title, String composer, int yearWritten) {
this.title = title;
this.composer = composer;
this.yearWritten = yearWritten;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getComposer() {
return composer;
}
public void setComposer(String composer) {
this.composer = composer;
}
public int getYearWritten() {
return yearWritten;
}
public void setYearWritten(int yearWritten) {
this.yearWritten = yearWritten;
}
@Override
public String toString() {
return "MusicalComposition [title=" + title + ", composer=" + composer + ", yearWritten=" + yearWritten + "]";
}
}
public class NationalAnthem extends MusicalComposition{
private String nameOfTheAnthemsNation;
public NationalAnthem(String title, String composer, int yearWritten, String nameOfTheAnthemsNation) {
super(title, composer, yearWritten);
this.nameOfTheAnthemsNation = nameOfTheAnthemsNation;
}
public String getNameOfTheAnthemsNation() {
return nameOfTheAnthemsNation;
}
public void setNameOfTheAnthemsNation(String nameOfTheAnthemsNation) {
this.nameOfTheAnthemsNation = nameOfTheAnthemsNation;
}
@Override
public String toString() {
return "NationalAnthem [nameOfTheAnthemsNation=" + nameOfTheAnthemsNation + "]";
}
}
public class MusicDemo {
public static void main(String[] args) {
MusicalComposition musicalComposition=new MusicalComposition("The Star-Spangled Banner","Francis Scott Key",1814);
System.out.println(musicalComposition);
NationalAnthem nationalAnthem=new NationalAnthem("The Star-Spangled Banner","Francis Scott Key",1814,"USA");
System.out.println(nationalAnthem);
}
}
Comments
Leave a comment