IN JAVA: Write a program that will compile a list of instruments from different
ID: 3867605 • Letter: I
Question
IN JAVA:
Write a program that will compile a list of instruments from different instrument families. A family of musical Instruments is a grouping of several different but related sizes or types of instruments.
Ref: https://en.wikipedia.org/wiki/Family_(musical_instruments)
Some commonly recognized families are:
Brass Family - A brass instrument is a musical instrument that produces sound by sympatheticvibration of air in a tubular resonator in sympathy with the vibration of the player's lips.
Ref: https://en.wikipedia.org/wiki/Brass_instrument
Strings Family - String instruments, stringed instruments, or chordophones are musicalinstruments that produce sound from vibrating strings.
Ref: https://en.wikipedia.org/wiki/String_instrument
Woodwind Family - Woodwind instruments are a family of musical instruments within the moregeneral category of wind instruments. There are two main types of woodwind instruments:flutes and reed instruments (otherwise called reed pipes). What differentiates theseinstruments from other wind instruments is the way in which they produce their sound.
Ref: https://en.wikipedia.org/wiki/Woodwind_instrument
Percussion Family - A percussion instrument is a musical instrument that is sounded by beingstruck or scraped by a beater (including attached or enclosed beaters or rattles); struck, scrapedor rubbed by hand; or struck against another similar instrument.
Ref: https://en.wikipedia.org/wiki/Percussion_instrument
Constraints
Limit the number of families to the list above
Use one separate (external to the main class) abstract super class: Instrument
Use three separate (external to the main class) abstract subclasses that extend Instrument:oBlownInstrumentoFingeredoStruck
Use four separate (external to the main class) subclasses (families) that extend the appropriate three aboveoBrassoStringsoWoodwindoPercussion
Requirements
Display the menu shown in the example output below
Request the number of instruments to be entered
All instruments should be placed into an array (you can use one array per family or one array for all entered) so they may be displayed
Use set and get methods where appropriate
Implement a loop that will allow the user to enter more instruments
Use exception handling and validation where appropriate
Hints
Make sure you use Java coding conventions
Deliverables
Select one of the following instrument families:
1.Brass
2.String
3.Woodwind
4.Percussion
5.Display all instruments
6.Exit
->: 1
How many brass instruments would you like to enter? ->: 3
Enter instrument #1->: bugle
Enter instrument #2->: trumpet
Enter instrument #3->: tuba
Display instruments?
(Y for Yes, N for No)->: n
Enter more instruments?
(Y for Yes, N for No)->: y
1.Brass
2.String
3.Woodwind
4.Percussion
5.Display all instruments
6.Exit
->: 2
How many string instruments would you like to enter? ->: 2
Enter instrument #1->: guitar
Enter instrument #2->: harp
Display instruments?
(Y for Yes, N for No)->: n
Enter more instruments?
(Y for Yes, N for No)->: y
1.Brass
2.String
3.Woodwind
4.Percussion
5.Display all instruments
6.Exit
->: 3
How many wood wind instruments would you like to enter? ->: 2
Enter instrument #1->: clarinet
Enter instrument #2->: oboe
Display instruments?
(Y for Yes, N for No)->: n
Enter more instruments?
(Y for Yes, N for No)->: y
1.Brass
2.String
3.Woodwind
4.Percussion
5.Display all instruments
6.Exit
->: 4
How many percussion instruments would you like to enter? ->: 2
Enter instrument #1->: drum
Enter instrument #2->: marimba
Display instruments?
(Y for Yes, N for No)->: n
Enter more instruments?
(Y for Yes, N for No)->: y
1.Brass
2.String
3.Woodwind
4.Percussion
5.Display all instruments
6.Exit
->: 5
The bugle is a brass instrument and is played by mouth.
The trumpet is a brass instrument and is played by mouth.
The tuba is a brass instrument and is played by mouth.
The clarinet is a woodwind instrument and is played by mouth.
The oboe is a woodwind instrument and is played by mouth.
The guitar is a string instrument and is played with fingers.
The harp is a string instrument and is played with fingers.
The drum is a percussion instrument and is struck.
The marimba is a percussion instrument and is struck.
Enter more instruments?
(Y for Yes, N for No)->: n
Goodbye
Explanation / Answer
Main
package com.mi;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
boolean flag=true;
int caseid=0;
int cont=0;
String cflag="N";
String instName=null;
Scanner sc=new Scanner(System.in);
HashMap<String,Instrument> hm= new HashMap<String,Instrument>();
System.out.println("Select one of the following instrument families:");
while(flag)
{
if(cflag.equalsIgnoreCase("N"))
{
System.out.println("1.Brass");
System.out.println("2.String");
System.out.println("3.Woodwind");
System.out.println("4.Percussion");
System.out.println("5.Display all instruments");
System.out.println("6.Exit");
System.out.print("->:");
caseid=sc.nextInt();
}
switch(caseid)
{
case 1: {
System.out.println("How many brass instruments would you like to enter? ->");
cont=sc.nextInt();
for(int i=1;i<=cont;i++)
{
System.out.println("Enter instrument #"+i+"->:");
instName=sc.next();
hm.put(instName, new BrassFamily());
}
System.out.println("Display instruments?");
System.out.print("(Y for Yes, N for No)->:");
cflag=sc.next();
if(cflag.equalsIgnoreCase("Y"))
{
caseid=5;
break;
}
else
{
System.out.println("Enter more instruments?");
System.out.println("(Y for Yes, N for No)->:");
cflag=sc.next();
if(cflag.equalsIgnoreCase("Y"))
{
cflag="N";
break;
}
else
{
caseid=6;
break;
}
}
}
case 2: {
System.out.println("How many string instruments would you like to enter? ->");
cont=sc.nextInt();
for(int i=1;i<=cont;i++)
{
System.out.println("Enter instrument #"+i+"->:");
instName=sc.next();
hm.put(instName, new StringsFamily());
}
System.out.println("Display instruments?");
System.out.print("(Y for Yes, N for No)->:");
cflag=sc.next();
if(cflag.equalsIgnoreCase("Y"))
{
caseid=5;
break;
}
else
{
System.out.println("Enter more instruments?");
System.out.println("(Y for Yes, N for No)->:");
cflag=sc.next();
if(cflag.equalsIgnoreCase("Y"))
{
cflag="N";
break;
}
else
{
caseid=6;
break;
}
}
}
case 3: {
System.out.println("How many wood wind instruments would you like to enter? ->");
cont=sc.nextInt();
for(int i=1;i<=cont;i++)
{
System.out.println("Enter instrument #"+i+"->:");
instName=sc.next();
hm.put(instName, new WoodwindFamily());
}
System.out.println("Display instruments?");
System.out.print("(Y for Yes, N for No)->:");
cflag=sc.next();
if(cflag.equalsIgnoreCase("Y"))
{
caseid=5;
break;
}
else
{
System.out.println("Enter more instruments?");
System.out.println("(Y for Yes, N for No)->:");
cflag=sc.next();
if(cflag.equalsIgnoreCase("Y"))
{
cflag="N";
break;
}
else
{
caseid=6;
break;
}
}
}
case 4: {
System.out.println("How many percussion instruments would you like to enter? ->");
cont=sc.nextInt();
for(int i=1;i<=cont;i++)
{
System.out.println("Enter instrument #"+i+"->:");
instName=sc.next();
hm.put(instName, new PercussionFamily());
}
System.out.println("Display instruments?");
System.out.print("(Y for Yes, N for No)->:");
cflag=sc.next();
if(cflag.equalsIgnoreCase("Y"))
{
caseid=5;
break;
}
else
{
System.out.println("Enter more instruments?");
System.out.println("(Y for Yes, N for No)->:");
cflag=sc.next();
if(cflag.equalsIgnoreCase("Y"))
{
cflag="N";
break;
}
else
{
caseid=6;
break;
}
}
}
case 5:
{
for(Map.Entry<String,Instrument> m:hm.entrySet())
{
System.out.println("The "+m.getKey()+" "+m.getValue().statement());
}
System.out.println("Enter more instruments?");
System.out.println("(Y for Yes, N for No)->:");
cflag=sc.next();
if(cflag.equalsIgnoreCase("Y"))
{
cflag="N";
break;
}
else
{
caseid=6;
break;
}
}
case 6:
{
System.out.println("GoodBye");
flag=false;
break;
}
}
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------
BlownInstrument
package com.mi;
public abstract class BlownInstrument extends Instrument {
public String play()
{
return "and is played by mouth.";
}
public abstract String SuggestType();
public abstract String statement();
}
-------------------------------------------------------------------------------------------------------------------------------------------------
BrassFamily
package com.mi;
public class BrassFamily extends BlownInstrument {
@Override
public String SuggestType() {
// TODO Auto-generated method stub
return "is a brass instrument ";
}
@Override
public String statement() {
String op=this.SuggestType()+super.play();
return op;
}
}
---------------------------------------------------------------------------------------------------------------------------------------
FingeredInstrument
package com.mi;
public abstract class FingeredInstrument extends Instrument {
public String play()
{
return "and is played with fingers.";
}
public abstract String SuggestType();
public abstract String statement();
}
-------------------------------------------------------------------------------------------------------------------------------------
Instrument
package com.mi;
public abstract class Instrument {
public abstract String play();
public abstract String SuggestType();
public abstract String statement();
}
------------------------------------------------------------------------------------------------
PercussionFamily
package com.mi;
public class PercussionFamily extends StruckInstrument {
@Override
public String SuggestType() {
// TODO Auto-generated method stub
return "is a percussion instrument ";
}
@Override
public String statement() {
String op=this.SuggestType()+super.play();
return op;
}
}
---------------------------------------------------------------------------------------------------------------------------
StringsFamily
package com.mi;
public class StringsFamily extends FingeredInstrument {
@Override
public String SuggestType() {
// TODO Auto-generated method stub
return "is a string instrument ";
}
@Override
public String statement() {
String op=this.SuggestType()+super.play();
return op;
}
}
-----------------------------------------------------------------------------------------------------------------------------------
StruckInstrument
package com.mi;
public abstract class StruckInstrument extends Instrument {
public String play()
{
return "and is struck.";
}
public abstract String SuggestType();
public abstract String statement();
}
----------------------------------------------------------------------------------------------------------------------------------------
WoodwindFamily
package com.mi;
public class WoodwindFamily extends BlownInstrument {
@Override
public String SuggestType() {
// TODO Auto-generated method stub
return "is a woodwind instrument ";
}
@Override
public String statement() {
String op=this.SuggestType()+super.play();
return op;
}
}