Design and implement a musical instrument class along with 4 subclasses using th
ID: 3647942 • Letter: D
Question
Design and implement a musical instrument class along with 4 subclasses using the following guidelines:Create a class named MusicalInstrument and its four subclasses named Woodwind, Brass, Strings and Percussion.
The MusicalInstrument has a name, isPlaying, and isTuned data fields. It also has methods for setting and getting the values of each of the fields along with a method to display how the instrument is played. A constructor that sets the isTuned and isPlaying fields to false is also included in the MusicalInstrument class. You are welcome to add additional data fields and methods if you like.
create a Java test class that simulates using your MusicalInstrument class. In your test class you should at a minimum: a) Construct 4 instances of your instrument, b) tune your instruments, c) print the name of your instrument d) print how you play your instrument
When you create your Woodwind, Brass, Strings and Percussion classes set the name and how to play the instrument as follows
Class Name Method to Play
Woodwind Woodwind Reeds
Brass Brass Mouth piece
Strings Strings Pluck and bow
Percussion Percussion Hit
Explanation / Answer
public class CMusicalInstrument { private String name; private boolean isPlaying; private boolean isTuned; public CMusicalInstrument(){ isPlaying = false; isTuned = false; } public CMusicalInstrument(String name){ this.name = name; } public void setName(String value){ name = value; } public String getName(){ return name; } public void play(){ isPlaying = true; } public void pause(){ isPlaying = false; } public void tune(){ isTuned = !isTuned; } } public class CWoodwind extends CMusicalInstrument { private String playSound; public CWoodwind(String name,String play){ super(name); playSound = play; } public void howToPlay(){ System.out.println("" + super.getName() + " is playing " +playSound ); } } public class CBrass extends CMusicalInstrument { private String playSound; public CBrass(String name,String play){ super(name); playSound = play; } public void howToPlay(){ System.out.println("" + super.getName() + " is playing " +playSound ); } } public class CString extends CMusicalInstrument { private String playSound; public CString(String name,String play){ super(name); playSound = play; } public void howToPlay(){ System.out.println("" + super.getName() + " is playing " +playSound ); } } public class CPercurssion extends CMusicalInstrument { private String playSound; public CPercurssion(String name,String play){ super(name); playSound = play; } public void howToPlay(){ System.out.println("" + super.getName() + " is playing " +playSound ); } } public class Main { public static void main(String args[]){ CWoodwind wood = new CWoodwind("Woodwind","Reeds"); CBrass brass = new CBrass("Brass","Piece"); CString str = new CString("String","Pluck and Bow"); CPercurssion per = new CPercurssion("Percurssion","Percursion Hit"); wood.howToPlay(); brass.howToPlay(); str.howToPlay(); per.howToPlay(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.