Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Java class named HeadPhones to represent a headphone set. The class con

ID: 640792 • Letter: C

Question

Create a Java class named HeadPhones 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.

Explanation / Answer


public class HeadPhones {
   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;
  
   HeadPhones(){
       volume = MEDIUM;
       pluggedIn = false;
       manufacturer = "DEFAULT";
       headPhoneColor = "DEFAULT";
   }
  
   public void setVolume(int v){
       if(v < LOW){
           volume = LOW;
       }
       else if(v > HIGH){
           volume = HIGH;
       }
       else{
           volume = v;  
       }
   }
  
   public void setPluggedIn(boolean p){
       pluggedIn = p;
   }
  
   public void setManufacturer(String m){
       manufacturer = m;
   }
  
   public void setColor(String C){
       headPhoneColor = C;
   }
  
   public int getVolume(){
       return volume;
   }
  
   public boolean getPluggedIn(){
       return this.pluggedIn;
   }
  
   public String getManufacturer(){
       return this.manufacturer;
   }
  
   public String getColor(){
       return this.headPhoneColor;
   }
  
   public void changeVolume(int volume){
       setVolume(volume);
   }
  
   public String toString(){
       return String.format("Volume: %d Plugged In: %b Manufacturer: %s Color: %s",
               volume, pluggedIn, manufacturer, headPhoneColor);
   }
  
   public static void main(String args[]){
       HeadPhones H = new HeadPhones();
       H.setVolume(HIGH);
       H.setManufacturer("Sony");
       H.setColor("Red");
       H.setPluggedIn(true);
       H.changeVolume(MEDIUM);
       System.out.println(H);
   }
  
}