Answer to Question #196598 in Java | JSP | JSF for Vinit

Question #196598

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.


1
Expert's answer
2021-05-21T11:52:41-0400
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();
		
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS