Here is the Assignment instructions:
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 if 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.
- 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.
Here is the code I have written so far that does work. I need your help in reviewing and improving the code:
// HEADPHONE CLASS
public class HeadPhone {
public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;
private int volume;
private boolean pluggedIn;
private String manufacturer;
private String headPhoneColor;
String currentVolume;
String statusPluggedIn;
String playlist;
// Constructor
public HeadPhone(int volume, boolean pluggedIn, String manufacturer, String headPhoneColor) {
this.volume = volume;
this.pluggedIn = pluggedIn;
this.manufacturer = manufacturer;
this.headPhoneColor = headPhoneColor;
}
// Default Constructor
public HeadPhone() {
volume = MEDIUM;
pluggedIn = false;
manufacturer = "DEFAULT";
headPhoneColor = "DEFAULT";
}
// Setter methods
// setVolume
public void setVolume(int volume) {
this.volume = volume;
}
// setPluggedIn
public void setPluggedIn(boolean pluggedIn) {
this.pluggedIn = pluggedIn;
}
// setManufacturer
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
// setHeadPhoneColor
public void setHeadPhoneColor(String headPhoneColor) {
this.headPhoneColor = headPhoneColor;
}
// getVolume
public int getVolume() {
if (volume == 1) {
currentVolume = "LOW";
}
else if (volume == 2) {
currentVolume = "MEDIUM";
}
else {
currentVolume = "HIGH";
}
return volume;
}
// getPluggedIn
public boolean getPluggedIn() {
if(pluggedIn == true) {
statusPluggedIn = "plugged in";
}
else {
statusPluggedIn = "disconnected";
}
return pluggedIn;
}
// getManufacturer
public String getManufacturer() {
return manufacturer;
}
// getHeadPhoneColor
public String getHeadPhoneColor() {
return headPhoneColor;
}
// getPlaylist
public String getPlaylist() {
if(pluggedIn == false) {
playlist = "Please plug the Head Phones into a device.";
}
else if(pluggedIn == true && volume == 1) {
playlist = "Currently playing classical music playlist";
}
else if(pluggedIn == true && volume == 2) {
playlist = "Currently playing country music playlist";
}
else {
playlist = "Currently playing rock music playlist";
}
return playlist;
}
// changeVolume
public void changeVolume(int volume) {
setVolume(volume);
}
// toString
public String toString() {
int volume = this.getVolume();
boolean pluggedIn = this.getPluggedIn();
String manufacturer = this.getManufacturer();
String headphoneColor = this.getHeadPhoneColor();
String currentVolume = this.currentVolume;
String playlist = this.getPlaylist();
StringBuilder sb = new StringBuilder();
sb.append(String.format("Manufacturer: %s\n", manufacturer));
sb.append(String.format("Color: %s\n", headPhoneColor));
sb.append(String.format("Currently: %s\n", statusPluggedIn));
sb.append(String.format("Volume is set to: %s\n", currentVolume));
sb.append(String.format("%s\n", playlist));
return sb.toString();
}
}
// TEST CLASS
public class TestHeadPhone {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
HeadPhone HeadPhone1 = new HeadPhone();
HeadPhone HeadPhone2 = new HeadPhone(1, true, "JVC", "Green");
HeadPhone HeadPhone3 = new HeadPhone(3, true, "Beats", "Red");
int HeadPhone1Volume = HeadPhone1.getVolume();
boolean HeadPhone1PluggedIn = HeadPhone1.getPluggedIn();
String HeadPhone1Manufacturer = HeadPhone1.getManufacturer();
String HeadPhone1HeadPhoneColor = HeadPhone1.getHeadPhoneColor();
String HeadPhone1CurrentVolume = HeadPhone1.currentVolume;
String HeadPhone1StatusPluggedIn = HeadPhone1.statusPluggedIn;
String HeadPhone1Playlist = HeadPhone1.playlist;
int HeadPhone2Volume = HeadPhone2.getVolume();
boolean HeadPhone2PluggedIn = HeadPhone2.getPluggedIn();
String HeadPhone2Manufacturer = HeadPhone2.getManufacturer();
String HeadPhone2HeadPhoneColor = HeadPhone2.getHeadPhoneColor();
String HeadPhone2CurrentVolume = HeadPhone2.currentVolume;
String HeadPhone2StatusPluggedIn = HeadPhone2.statusPluggedIn;
int HeadPhone3Volume = HeadPhone3.getVolume();
boolean HeadPhone3PluggedIn = HeadPhone3.getPluggedIn();
String HeadPhone3Manufacturer = HeadPhone3.getManufacturer();
String HeadPhone3HeadPhoneColor = HeadPhone3.getHeadPhoneColor();
String HeadPhone3CurrentVolume = HeadPhone3.currentVolume;
String HeadPhone3StatusPluggedIn = HeadPhone3.statusPluggedIn;
System.out.println("Head Phone 1 has the following parameters:");
System.out.println(HeadPhone1.toString());
System.out.println("Head Phone 2 has the following parameters:");
System.out.println(HeadPhone2.toString());
System.out.println("Head Phone 3 has the following parameters:");
System.out.println(HeadPhone3.toString());
}
}
The questions that I have directly are:
- What exactly is the intended result of the
changeVolume(change)
method in the descriptions? - Should
String currentVolume
/statusPluggedIn
/playlist
beprivate
? - Any other details or help that could improve both the usability and functionality of this code?