57.4k views
3 votes
In this exercise, you are going to build a hierarchy to create instrument objects. We are going to create part of the orchestra using three classes, Instrument, Wind, and Strings. Note that the Strings class has a name very close to the String class, so be careful with your naming convention!We need to save the following characteristics:Name and family should be saved for all instrumentsWe need to specify whether a strings instrument uses a bowWe need to specify whether a wind instrument uses a reedBuild the classes out with getters and setters for all classes. Only the superclass needs a toString and the toString should print like this:Violin is a member of the String family.Your constructors should be set up to match the objects created in the InstrumentTester class.These are the files givenpublic class InstrumentTester{public static void main(String[] args){/*** Don't Change This Tester Class!** When you are finished, this should run without error.*/Wind tuba = new Wind("Tuba", "Brass", false);Wind clarinet = new Wind("Clarinet", "Woodwind", true); Strings violin = new Strings("Violin", true);Strings harp = new Strings("Harp", false); System.out.println(tuba);System.out.println(clarinet); System.out.println(violin);System.out.println(harp);}}////////////////////////////public class Wind extends Instrument{}///////////////////////////public class Strings extends Instrument{ }/////////////////////////public class Instrument{ }

1 Answer

5 votes

Answer:

Step-by-step explanation:

The following code is written in Java and creates the classes, variables, and methods as requested. The String objects created are not being passed a family string argument in this question, therefore I created two constructors for the String class where the second constructor has a default value of String for family.

package sample;

class InstrumentTester {

public static void main(String[] args) {

/*** Don't Change This Tester Class!** When you are finished, this should run without error.*/

Wind tuba = new Wind("Tuba", "Brass", false);

Wind clarinet = new Wind("Clarinet", "Woodwind", true);

Strings violin = new Strings("Violin", true);

Strings harp = new Strings("Harp", false);

System.out.println(tuba);

System.out.println(clarinet);

System.out.println(violin);

System.out.println(harp);

}

}

class Wind extends Instrument {

boolean usesReef;

public Wind(String name, String family, boolean usesReef) {

this.setName(name);

this.setFamily(family);

this.usesReef = usesReef;

}

public boolean isUsesReef() {

return usesReef;

}

public void setUsesReef(boolean usesReef) {

this.usesReef = usesReef;

}

}

class Strings extends Instrument{

boolean usesBow;

public Strings (String name, String family, boolean usesBow) {

this.setName(name);

this.setFamily(family);

this.usesBow = usesBow;

}

public Strings (String name, boolean usesBow) {

this.setName(name);

this.setFamily("String");

this.usesBow = usesBow;

}

public boolean isUsesBow() {

return usesBow;

}

public void setUsesBow(boolean usesBow) {

this.usesBow = usesBow;

}

}

class Instrument{

private String family;

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getFamily() {

return family;

}

public void setFamily(String family) {

this.family = family;

}

public String toString () {

System.out.println(this.getName() + " is a member of the " + this.getFamily() + " family.");

return null;

}

}

User NathanBrakk
by
4.5k points