Create a Java class named HeadPhone to represent a headphone set. The class cont
ID: 3791533 • Letter: C
Question
Create a Java class named HeadPhone to represent a headphone set. The class contains:
Three constants named LOW, MEDIUM and HIGH with values of 1, 2 and 3 to denote the headphone volume.
A private int data field named volume that specifies the volume of the headphone. The default volume is MEDIUM.
A private boolean data field named pluggedIn that specifies if the headphone is plugged in. The default value is false.
A private String data field named manufacturer that specifies the name of the manufacturer of the headphones.
A private Color data field named headPhoneColor that specifies the color of the headphones.
A private String data field named headPhoneModel that specifies the Model of the headphones.
getter and setter methods for all data fields.
A no argument constructor that creates a default headphone.
A method named toString() that returns a string describing the current field values of
the headphones.
A method named changeVolume(value) that changes the volume of the headphone to
the value passed into the method
Create a TestHeadPhone class that constructs at least 3 HeadPhone objects. For each of the objects constructed, demonstrate the use of each of the methods. Be sure to use your IDE to accomplish this assignment.
Explanation / Answer
Here is the HeadPhone.java code for you:
import java.awt.*;
class HeadPhone
{
//Three constants named LOW, MEDIUM and HIGH with values of 1, 2 and 3 to denote the headphone volume.
public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;
//A private int data field named volume that specifies the volume of the headphone. The default volume is MEDIUM.
int volume;
//A private boolean data field named pluggedIn that specifies if the headphone is plugged in. The default value is false.
boolean pluggedIn;
//A private String data field named manufacturer that specifies the name of the manufacturer of the headphones.
String manufacturer;
//A private Color data field named headPhoneColor that specifies the color of the headphones.
Color headPhoneColor;
//A private String data field named headPhoneModel that specifies the Model of the headphones.
String headPhoneModel;
//getter and setter methods for all data fields.
public void setVolume(int vol) { volume = vol; }
public void setPluggedIn(boolean pI) { pluggedIn = pI; }
public void setManufacturer(String mfr) { manufacturer = mfr; }
public void setHeadPhoneColor(Color clr) { headPhoneColor = clr; }
public void setHeadPhoneModel(String mdl) {headPhoneModel = mdl; }
public int getVolume() { return volume; }
public boolean getPluggedIn() { return pluggedIn; }
public String getManufacturer() { return manufacturer; }
public Color getHeadPhoneColor() { return headPhoneColor; }
public String getHeadPhoneModel() { return headPhoneModel; }
//A no argument constructor that creates a default headphone.
public HeadPhone()
{
volume = MEDIUM;
pluggedIn = false;
manufacturer = "";
headPhoneColor = Color.BLACK;
headPhoneModel = "";
}
//A method named toString() that returns a string describing the current field values of
//the headphones.
public String toString()
{
String output = "";
output += "Volume: " + volume + " ";
output += "PluggedIn: " + pluggedIn + " ";
output += "Manufacturer: " + manufacturer + " ";
output += "Headphone Color: " + headPhoneColor + " ";
output += "Headphone Model: " + headPhoneModel + " ";
return output;
}
//A method named changeVolume(value) that changes the volume of the headphone to
//the value passed into the method
public void changeVolume(int value)
{
volume = value;
}
}