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.
import java.util.Scanner;
class MusicalComposition {
// title, composer, and yearwritten.
private String title;
private String composer;
private int yearWritten;
/**
* Constructor
* @param title
* @param composer
* @param yearWritten
*/
public MusicalComposition (String title,String composer,int yearWritten) {
this.title=title;
this.composer=composer;
this.yearWritten=yearWritten;
}
public void display() {
System.out.println("Title: "+title);
System.out.println("Composer: "+composer);
System.out.println("Year written: "+yearWritten);
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the composer
*/
public String getComposer() {
return composer;
}
/**
* @param composer the composer to set
*/
public void setComposer(String composer) {
this.composer = composer;
}
/**
* @return the yearWritten
*/
public int getYearWritten() {
return yearWritten;
}
/**
* @param yearWritten the yearWritten to set
*/
public void setYearWritten(int yearWritten) {
this.yearWritten = yearWritten;
}
}
class NationalAnthem extends MusicalComposition{
private String nameAnthemNation;
/**
* Constructor
* @param title
* @param composer
* @param yearWritten
* @param nameAnthemNation
*/
public NationalAnthem (String title,String composer,int yearWritten,String nameAnthemNation) {
super(title, composer, yearWritten);
this.nameAnthemNation=nameAnthemNation;
}
public void display() {
super.display();
System.out.println("The name of anthem nation: "+nameAnthemNation);
}
/**
* @return the nameAnthemNation
*/
public String getNameAnthemNation() {
return nameAnthemNation;
}
/**
* @param nameAnthemNation the nameAnthemNation to set
*/
public void setNameAnthemNation(String nameAnthemNation) {
this.nameAnthemNation = nameAnthemNation;
}
}
public class MusicDemo {
public static void main(String[] args) {
MusicalComposition musicalComposition=new MusicalComposition("Symphony in C Minor","Ludwig Van Beethoven",5);
NationalAnthem nationalAnthem =new NationalAnthem("Symphony A","Mary",15,"National Anthem");
musicalComposition.display();
nationalAnthem.display();
}
}
Comments
Leave a comment