I cannot figure out my mistakes! I need some help desperately my assignment has
ID: 3714219 • Letter: I
Question
I cannot figure out my mistakes! I need some help desperately my assignment has been due & I have yet to turn it in. This is the expected output of the assignment:
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
_____________________________________________________________________________________________
Our Directions are:
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: -BlownInstrument -Fingered -Struck ?
Use four separate (external to the main class) subclasses (families) that extend the appropriate three above -Brass -Strings -Woodwind -Percussion
_________________________________________________________________________________________________________________________
?This is what I have. It runs but it must not be placed properly because it doesnt run all the way through and is kind of jumbled up.
MY MAIN CLASS:
//Imports
import java.util.Scanner;
//Begin Class Egentile_AbstractClasses
public class Egentile_AbstractClasses {
static String instrument = null;
static String enterMore = null;
static Scanner input = new Scanner(System.in);
static BrassFamily brass = new BrassFamily(instrument);
static StringFamily string = new StringFamily(instrument);
static WoodwindFamily woodwind = new WoodwindFamily(instrument);
static PercussionFamily percussion = new PercussionFamily(instrument);
//Begin Main Method
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
int instrumentFam;
String choice;
String nameOfinstrument;
System.out.println("Select one of the following instrument families: ");
do {
System.out.println("1.Brass 2.String 3.Woodwind 4.Percussion "
+ "5.Display all instruments 6.Exit");
System.out.println("->: ");
instrumentFam = input.nextInt();
switch (instrumentFam) {
case 1:
nameOfinstrument = ("Brass");
enterInst(nameOfinstrument);
System.out.println("Display instruments? (Y for Yes, N for No)");
choice = input.next();
if (choice.equalsIgnoreCase("Y")) {
Egentile_AbstractClasses.displayInstruments();
Egentile_AbstractClasses.GoodBye();
} else {
Egentile_AbstractClasses.GoodBye();
}
break;
case 2: //String instruments
nameOfinstrument = ("String");
enterInst(nameOfinstrument);
System.out.println("Display instruments? (Y for Yes, N for No)");
choice = input.next();
if (choice.equalsIgnoreCase("Y")) {
Egentile_AbstractClasses.displayInstruments();
Egentile_AbstractClasses.GoodBye();
} else {
Egentile_AbstractClasses.GoodBye();
}
break;
case 3: //Woodwind instruments
nameOfinstrument = ("Woodwind");
enterInst(nameOfinstrument);
System.out.println("Display instruments? (Y for Yes, N for No)");
choice = input.next();
if (choice.equalsIgnoreCase("Y")) {
Egentile_AbstractClasses.displayInstruments();
Egentile_AbstractClasses.GoodBye();
} else {
Egentile_AbstractClasses.GoodBye();
}
break;
case 4: //Percusssion instruments
nameOfinstrument = ("Percussion");
enterInst(nameOfinstrument);
System.out.println("Display instruments? (Y for Yes, N for No)");
choice = input.next();
if (choice.equalsIgnoreCase("Y")) {
Egentile_AbstractClasses.displayInstruments();
Egentile_AbstractClasses.GoodBye();
} else {
Egentile_AbstractClasses.GoodBye();
}
break;
case 5://Display all instruments
Egentile_AbstractClasses.displayInstruments();
break;
case 6://Exit Program
GoodBye();
{
System.out.println("Display instruments? (Y for Yes, N for No)");
choice = input.next();
if (choice.equalsIgnoreCase("Y")) {
Egentile_AbstractClasses.displayInstruments();
Egentile_AbstractClasses.GoodBye();
} else {
Egentile_AbstractClasses.GoodBye();
}
}
}
}while (enterMore.equalsIgnoreCase("Y"));}
private static void displayInstruments() {
brass.howToPlay();
string.howToPlay();
woodwind.howToPlay();
percussion.howToPlay();
}
private static void GoodBye() {
System.out.print("Enter more instrucments? (Y for Yes, N for No)->: ");
enterMore = input.next();
if (enterMore.equalsIgnoreCase("n")) {
System.out.println("Goodbye");
System.exit(0);
}
}
/**
* Method enterInst: Used to set instruments
*
* @param instName
*/
private static void enterInst(String instName) {
int numInts;
System.out.printf("How many %s instruments would you like to enter?->: ", instName);
numInts = input.nextInt();
/*Begin for loop to enter instruments.*/
for (int i = 0; i < numInts; i++) {
System.out.printf("Enter instrument #%d->: ", i + 1);
instrument = input.next();
switch (instName) {
case "brass":
brass.getInts(instrument);
break;
case "string":
string.getInts(instrument);
break;
case "woodwind":
woodwind.getInts(instrument);
break;
case "percussion":
percussion.getInts(instrument);
break;
default:
GoodBye();
}
}
}
}//End Main Method
________________________________________________________________________________________________
MY SUPER CLASS:
//Imports
//Begin Subclass Instrument
public abstract class Instrument extends Egentile_AbstractClasses {
protected String name;
abstract public void howToPlay();
} //End Subclass Instrumen
__________________________________________________________________________________________
This is what my subclass for blown, fingered, and struck instruments all look like:
//Imports
//Begin Subclass BlownInstrument
public abstract class BlownInstrument extends Instrument {
/**
*
*/
protected String blown;
} //End Subclass BlownInstrument
__________________________________________________________________________________
& then my final four subclasses for each type of instrument family all look like this:
//Imports
import java.util.ArrayList;
//Begin Subclass BrassFamily
public class BrassFamily extends BlownInstrument {
ArrayList <String> BrassInsName = new ArrayList<>();
/*declare a local ArrayList of type String to hold entered instruments*/
BrassFamily(String BrassInsName) {
super();
/*declare this name and set equal to variable declared for brass instrument*/
this.blown = " is played by mouth.";
}
public void setInts(String bI) {
BrassInsName.add(bI);
}
public ArrayList<String> getInts(String instrument) {
return BrassInsName;
}
@Override
public void howToPlay() {
getInts(instrument).forEach((array) -> {
System.out.println("The " + array + " is a brass instrument and" + blown);
});
}
} //End Subclass BrassFamily
Explanation / Answer
Code
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.