Create a Java class named HeadPhone to represent a headphone set. The class cont
ID: 3711060 • 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
TestHeadPhone.java
import java.util.Scanner;
public class TestHeadPhone {
public static void main(String[] args) {
HeadPhone h[] = new HeadPhone[3];
Scanner scan = new Scanner(System.in);
for(int i=0; i<h.length; i++){
System.out.println("Enter New Head Phone Details: ");
System.out.print("Enter Volume:");
int volume = scan.nextInt();
System.out.print("Enter pluggedIn :");
boolean pluggedIn = scan.nextBoolean();
System.out.print("Enter manufacturer :");
String manufacturer = scan.next();
System.out.print("Enter headPhoneColor :");
String headPhoneColor = scan.next();
HeadPhone obj = new HeadPhone();
obj.setHeadPhoneColor(headPhoneColor);
obj.setManufacturer(manufacturer);
obj.setPluggedIn(pluggedIn);
obj.changeVolume(volume);
h[i] = obj;
}
System.out.println("Headphone details are: ");
System.out.println("--------------------------------");
for(int i=0; i<h.length; i++){
System.out.println(h[i].toString());
System.out.println("--------------------------------");
}
}
}
HeadPhone.java
public class HeadPhone {
public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;
private int volume = MEDIUM;
private boolean pluggedIn = false;
private String manufacturer ;
private String headPhoneColor ;
public HeadPhone(){
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public boolean isPluggedIn() {
return pluggedIn;
}
public void setPluggedIn(boolean pluggedIn) {
this.pluggedIn = pluggedIn;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getHeadPhoneColor() {
return headPhoneColor;
}
public void setHeadPhoneColor(String headPhoneColor) {
this.headPhoneColor = headPhoneColor;
}
public String toString(){
return "Volume: "+getVolume()+" PluggedIn: "+isPluggedIn()+" Manufacturer: "+getManufacturer()+" HeadPhoneColor: "+getHeadPhoneColor();
}
public void changeVolume(int value){
setVolume(value);
}
}
Output:
Enter New Head Phone Details:
Enter Volume:11
Enter pluggedIn :true
Enter manufacturer :aa
Enter headPhoneColor :black
Enter New Head Phone Details:
Enter Volume:22
Enter pluggedIn :false
Enter manufacturer :bbb
Enter headPhoneColor :red
Enter New Head Phone Details:
Enter Volume:33
Enter pluggedIn :false
Enter manufacturer :cccc
Enter headPhoneColor :orange
Headphone details are:
--------------------------------
Volume: 11
PluggedIn: true
Manufacturer: aa
HeadPhoneColor: black
--------------------------------
Volume: 22
PluggedIn: false
Manufacturer: bbb
HeadPhoneColor: red
--------------------------------
Volume: 33
PluggedIn: false
Manufacturer: cccc
HeadPhoneColor: orange
--------------------------------