**Question after given code public class Instrument { private String name; priva
ID: 3830596 • Letter: #
Question
**Question after given code
public class Instrument {
private String name;
private double cost;
private String pictureUrl;
private String sound;
public Instrument() {
}
/**
*
* @param name
*
* @param cost
*
* @param pictureUrl
*
* @param sound
*
*/
public Instrument(String name, double cost, String pictureUrl, String sound) {
this.name = name;
this.cost = cost;
if (cost < 0)
cost = 299.99;
this.pictureUrl = pictureUrl;
this.sound = sound;
}
/**
*
* @return name
*
*/
public String getName() {
return name;
}
/**
*
* @return cost
*
*/
public double getCost() {
return cost;
}
/**
*
* @return pictureUrl
*
*/
public String getPictureUrl() {
return pictureUrl;
}
/**
*
* @return sound
*
*/
public String getSound() {
return sound;
}
/**
*
* @param name
*
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @param cost
*
*/
public void setCost(double cost) {
this.cost = cost;
if (cost < 0)
cost = 299.99;
}
/**
*
* @param pictureUrl
*
*/
public void setPictureUrl(String pictureUrl) {
this.pictureUrl = pictureUrl;
}
/**
*
* @param sound
*
*/
public void setSound(String sound) {
this.sound = sound;
}
@Override
public String toString() {
return "Name: " + name + ", Cost: " + cost + ", Picture Url: " + pictureUrl + ", Sound: " + sound;
}
public void display(String color, int fontSize) {
System.out.println(name + " Color: " + color + ", Font Size: " + fontSize);
}
}
Write the definition for a subclass Percussion for base class Instrument which has the following fields:
1.drumType (e.g. “base”, “snare”, “tom-tom”)
2.pitched (e.g. true or false only)
For the ADT make sure you code the following:
*a no-argument constructor
*another constructor to set all the data of the class
*all accessor and modifier methods
*a toString method
Explanation / Answer
public class Percussion extends Instrument{
private String drumType;
private boolean pitched;
/**
*
*/
public Percussion() {
}
/**
*
* @param name
*
* @param cost
*
* @param pictureUrl
*
* @param sound
*
* @param drumType
*
* @param pitched
*
*/
public Percussion(String name, double cost, String pictureUrl,
String sound, String drumType, boolean pitched) {
super(name, cost, pictureUrl, sound);
this.drumType = drumType;
this.pitched = pitched;
}
/**
* @return drumType
*/
public String getDrumType() {
return drumType;
}
/**
* @return pitched
*/
public boolean isPitched() {
return pitched;
}
/**
* @param drumType
*/
public void setDrumType(String drumType) {
this.drumType = drumType;
}
/**
* @param pitched
*/
public void setPitched(boolean pitched) {
this.pitched = pitched;
}
@Override
public String toString() {
return super.toString()+", Drum Type"+drumType+", Is pitched: "+pitched;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.