*Question after 3 programs import java.awt.Color; import java.awt.Font; import j
ID: 3835454 • Letter: #
Question
*Question after 3 programs
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
/**
*
*/
/**
* @author
*
*/
public class Instrument {
public NumberFormat nf = NumberFormat.getCurrencyInstance();
private String name;
private double cost;
private Picture picture;
private Sound sound;
/**
* no argument constructor
*/
public Instrument() {
}
/**
*
* @param name
*
* @param cost
*
* @param picture
*
* @param sound
*
*/
public Instrument(String name, double cost, Picture picture, Sound sound) {
this.name = name;
this.cost = cost;
this.picture = picture;
this.sound = sound;
}
/**
*
* @return name
*
*/
public String getName() {
return name;
}
/**
*
* @return cost
*
*/
public double getCost() {
return cost;
}
/**
*
* @return picture
*
*/
public Picture getPicture() {
return picture;
}
/**
*
* @return sound
*
*/
public Sound 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;
JOptionPane.showMessageDialog(null, "Cost is less than zero, default cost set to $299.99");
}
/**
*
* @param picture
*
*/
public void setPicture(Picture picture) {
this.picture = picture;
}
/**
*
* @param sound
*
*/
public void setSound(Sound sound) {
this.sound = sound;
}
@Override
public String toString() {
return "Name: " + name + ", Cost: " + nf.format(cost) + ", Picture Url: " + picture + ", Sound: " + sound;
}
public Picture labelImage(Color c, int fontSize)
{
Picture temp = picture;
Graphics g = temp.getGraphics();
g.setColor(c);
g.setFont(new Font("Arial",Font.BOLD,fontSize));
g.drawString(name, 25, 75);
return temp;
}
}
/**
*
*/
/**
* @author
*
*/
public class Percussion extends Instrument {
private String drumType;
private boolean pitched;
/**
*no argument constructor
*/
public Percussion() {
}
/**
* @param name
*
* @param cost
*
* @param pictureUrl
*
* @param sound
*
* @param drumType
*
* @param pitched
*
*/
public Percussion(String name, double cost, Picture picture,
Sound sound, String drumType, boolean pitched) {
super(name, cost, picture, 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;
}
}
import java.text.NumberFormat;
import javax.swing.JOptionPane;
/**
*
*/
/**
* @author
*
*/
public class MusicStore {
/**
* @param args
*/
public static void main(String[] args) {
Instrument [] inventory = new Instrument [6];
TestMusicStore store = new TestMusicStore();
store.fillInventory(inventory);
store.displayMenu(inventory);
}
public NumberFormat nf = NumberFormat.getCurrencyInstance();
/**
* @param inventory
*/
public void fillInventory (Instrument [] inventory) {
inventory[0] = new Instrument("Whistle", 62.55, new Picture ("Whistle.PNG"), new Sound ("Whistle.wav"));
inventory[1] = new Instrument("Harp", 599.99, new Picture ("Harp.PNG"), new Sound ("Harp.wav"));
inventory[2] = new Percussion("Bongos", 49.99, new Picture ("Bongos.PNG"), new Sound ("Bongos.wav"), "macho", false);
inventory[3] = new Percussion("Drums", 200.99, new Picture ("drum.jpg"), new Sound ("Drums.wav"), "bass", true);
inventory[4] = new Instrument();
String tName = JOptionPane.showInputDialog("Please Enter name of an instrument");
String tcost = JOptionPane.showInputDialog("How much does " + tName + " cost?");
inventory[4].setName(tName);
double tCost = 0.0;
inventory[4].setCost(tCost);
inventory[4].setPicture(new Picture ("Bell.PNG"));
inventory[4].setSound(new Sound ("Bell.wav"));
inventory[5] = new Percussion();
tName = JOptionPane.showInputDialog("Enter Name of a percussion instrument");
inventory[5].setName(tName);
tCost = Double.parseDouble(JOptionPane.showInputDialog("How much does " + tName + " cost?"));
inventory[5].setCost(tCost);
inventory[5].setPicture(new Picture ("cymbals.jpg"));
inventory[5].setSound(new Sound ("Cymbals.wav"));
((Percussion)inventory[5]).isPitched();
((Percussion)inventory[5]).setDrumType("crash");
}
**Call a method named displayMenu with the following choice:
Display Labeled Images
Use a for loop to label (use the method you wrote in your base class) and display all the images of your Instrument objects array. Use 3 different colors and 3 different font sizes.
Explanation / Answer
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.NumberFormat;
import javax.swing.JOptionPane;
/**
*
*/
/**
* @author
*
*/
public class Instrument {
public NumberFormat nf = NumberFormat.getCurrencyInstance();
private String name;
private double cost;
private Picture picture;
private Sound sound;
/**
* no argument constructor
*/
public Instrument() {
}
/**
*
* @param name
*
* @param cost
*
* @param picture
*
* @param sound
*
*/
public Instrument(String name, double cost, Picture picture, Sound sound) {
this.name = name;
this.cost = cost;
this.picture = picture;
this.sound = sound;
}
/**
*
* @return name
*
*/
public String getName() {
return name;
}
/**
*
* @return cost
*
*/
public double getCost() {
return cost;
}
/**
*
* @return picture
*
*/
public Picture getPicture() {
return picture;
}
/**
*
* @return sound
*
*/
public Sound 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;
JOptionPane.showMessageDialog(null, "Cost is less than zero, default cost set to $299.99");
}
/**
*
* @param picture
*
*/
public void setPicture(Picture picture) {
this.picture = picture;
}
/**
*
* @param sound
*
*/
public void setSound(Sound sound) {
this.sound = sound;
}
@Override
public String toString() {
return "Name: " + name + ", Cost: " + nf.format(cost) + ", Picture Url: " + picture + ", Sound: " + sound;
}
public Picture labelImage(Color c, int fontSize)
{
Picture temp = picture;
Graphics g = temp.getGraphics();
g.setColor(c);
g.setFont(new Font("Arial",Font.BOLD,fontSize));
g.drawString(name, 25, 75);
return temp;
}
}
/**
*
*/
/**
* @author
*
*/
public class Percussion extends Instrument {
private String drumType;
private boolean pitched;
/**
*no argument constructor
*/
public Percussion() {
}
/**
* @param name
*
* @param cost
*
* @param pictureUrl
*
* @param sound
*
* @param drumType
*
* @param pitched
*
*/
public Percussion(String name, double cost, Picture picture,
Sound sound, String drumType, boolean pitched) {
super(name, cost, picture, 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;
}
}
import java.text.NumberFormat;
import javax.swing.JOptionPane;
/**
*
*/
/**
* @author
*
*/
public class MusicStore {
/**
* @param args
*/
public static void main(String[] args) {
Instrument [] inventory = new Instrument [6];
TestMusicStore store = new TestMusicStore();
store.fillInventory(inventory);
store.displayMenu(inventory);
}
public NumberFormat nf = NumberFormat.getCurrencyInstance();
/**
* @param inventory
*/
public void fillInventory (Instrument [] inventory) {
inventory[0] = new Instrument("Whistle", 62.55, new Picture ("Whistle.PNG"), new Sound ("Whistle.wav"));
inventory[1] = new Instrument("Harp", 599.99, new Picture ("Harp.PNG"), new Sound ("Harp.wav"));
inventory[2] = new Percussion("Bongos", 49.99, new Picture ("Bongos.PNG"), new Sound ("Bongos.wav"), "macho", false);
inventory[3] = new Percussion("Drums", 200.99, new Picture ("drum.jpg"), new Sound ("Drums.wav"), "bass", true);
inventory[4] = new Instrument();
String tName = JOptionPane.showInputDialog("Please Enter name of an instrument");
String tcost = JOptionPane.showInputDialog("How much does " + tName + " cost?");
inventory[4].setName(tName);
double tCost = 0.0;
inventory[4].setCost(tCost);
inventory[4].setPicture(new Picture ("Bell.PNG"));
inventory[4].setSound(new Sound ("Bell.wav"));
inventory[5] = new Percussion();
tName = JOptionPane.showInputDialog("Enter Name of a percussion instrument");
inventory[5].setName(tName);
tCost = Double.parseDouble(JOptionPane.showInputDialog("How much does " + tName + " cost?"));
inventory[5].setCost(tCost);
inventory[5].setPicture(new Picture ("cymbals.jpg"));
inventory[5].setSound(new Sound ("Cymbals.wav"));
((Percussion)inventory[5]).isPitched();
((Percussion)inventory[5]).setDrumType("crash");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.