SlideShare a Scribd company logo
1 of 15
Download to read offline
// HeadPhone.java
/* All the outputs were same because the variables declared and defined in HeadPhone class
were static,
they will be updated only once during the program execution.
removing the static keyword from the variables which will be updated during the program
execution will
give the desired results
*/
public class HeadPhone {
private static final int LOW = 1;
private static final int MEDIUM = 2;
private static final int HIGH = 3;
private int volume = 0;
private boolean pluggedIn = false;
private String none = null;
private String manufacturer = none;
private String headPhoneColor = none;
private String volPos = none;
private String switchPos= none;
private String onOrOff= none;
// Default Headphone Constructor
public HeadPhone() {
volume = MEDIUM;
pluggedIn = false;
manufacturer = "Sony";
headPhoneColor = "Gold";
}
// Headphone Constructor
public HeadPhone (int volume2, boolean pluggedIn2, String manufacturer2, String
headPhoneColor2) {
volume = volume2;
pluggedIn = pluggedIn2;
manufacturer = manufacturer2;
headPhoneColor = headPhoneColor2;
}
// Setter Methods
// setVolume
public void setVolume(int volume2) {
volume = volume2;
}
// setPluggedIn
public void setPluggedIn(boolean pluggedIn2) {
pluggedIn = pluggedIn2;
}
// setManufacturer
public void setManufacturer(String manufacturer2) {
manufacturer = manufacturer2;
}
// setColor
public void setColor(String headPhoneColor2) {
headPhoneColor = headPhoneColor2;
}
// Getter Methods
// getVolume
public int getVolume() {
if(volume == 3) {
volPos = "HIGH";
}
else if (volume == 2) {
volPos = "MEDIUM";
}
else if(volume == 1) {
volPos = "LOW";
}
else {
System.out.println("Your entry is invalid, please try again.");
}
return volume;
}
//getPluggedIn
public boolean getPluggedIn() {
if(pluggedIn == false) {
onOrOff = "Off";
}
else {
onOrOff = "On";
}
return pluggedIn;
}
//getManufacturer
public String getManufacturer() {
return manufacturer;
}
//getColor
public String getColor() {
return headPhoneColor;
}
// changeVolume method
public void changeVolume(int volume2) {
setVolume(volume2);
}
// toString method
public String toString() {
String str = "toString() results: (volume=" + volume + ", pluggedIn=" + pluggedIn + ",
Manufacturer=" + manufacturer + ", HeadPhone Color=" + headPhoneColor +")";
return str;
}
}
// TestHeadPhone.java
import java.util.Scanner;
public class TestHeadPhone {
public static void main(String[] args) {
int row = 4;
HeadPhone [] hfArray = new HeadPhone[row];
HeadPhone hpObj1 = new HeadPhone();
hfArray[0] = hpObj1;
Scanner input = new Scanner(System.in);
for(int i=1; i<4; i++) {
HeadPhone hpObj2 = new HeadPhone();
System.out.println("Please enter the data for new headphones:");
System.out.println("Would you like to turn the headphones on? (Type 'true' for on, Type
'false' for off: ");
hpObj2.setPluggedIn(input.nextBoolean());
System.out.println("Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 =
High): ");
hpObj2.setVolume(input.nextInt());
System.out.println("Who manufactured the headphones? (Any string/word(s)");
hpObj2.setManufacturer(input.next());
System.out.println("What are the color of the headphones? (Any string/word(s)");
hpObj2.setColor(input.next());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
hfArray[i] = hpObj2;
}
for(int i=0; i<4; i++){
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(hfArray[i].toString());
System.out.println("getPluggedIn() results: "+hfArray[i].getPluggedIn());
System.out.println("getVolume() results: "+hfArray[i].getVolume());
System.out.println("getManufacturer() results: "+hfArray[i].getManufacturer());
System.out.println("getColor() results: "+hfArray[i].getColor());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
}
/*
output:
Please enter the data for new headphones:
Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off:
true
Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High):
1
Who manufactured the headphones? (Any string/word(s)
apple
What are the color of the headphones? (Any string/word(s)
white
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
Please enter the data for new headphones:
Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off:
true
Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High):
3
Who manufactured the headphones? (Any string/word(s)
sensiner
What are the color of the headphones? (Any string/word(s)
pink
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
Please enter the data for new headphones:
Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off:
false
Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High):
2
Who manufactured the headphones? (Any string/word(s)
voda
What are the color of the headphones? (Any string/word(s)
green
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
toString() results: (volume=2, pluggedIn=false, Manufacturer=Sony, HeadPhone Color=Gold)
getPluggedIn() results: false
getVolume() results: 2
getManufacturer() results: Sony
getColor() results: Gold
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
toString() results: (volume=1, pluggedIn=true, Manufacturer=apple, HeadPhone Color=white)
getPluggedIn() results: true
getVolume() results: 1
getManufacturer() results: apple
getColor() results: white
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
toString() results: (volume=3, pluggedIn=true, Manufacturer=sensiner, HeadPhone Color=pink)
getPluggedIn() results: true
getVolume() results: 3
getManufacturer() results: sensiner
getColor() results: pink
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
toString() results: (volume=2, pluggedIn=false, Manufacturer=voda, HeadPhone Color=green)
getPluggedIn() results: false
getVolume() results: 2
getManufacturer() results: voda
getColor() results: green
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
*/
Solution
// HeadPhone.java
/* All the outputs were same because the variables declared and defined in HeadPhone class
were static,
they will be updated only once during the program execution.
removing the static keyword from the variables which will be updated during the program
execution will
give the desired results
*/
public class HeadPhone {
private static final int LOW = 1;
private static final int MEDIUM = 2;
private static final int HIGH = 3;
private int volume = 0;
private boolean pluggedIn = false;
private String none = null;
private String manufacturer = none;
private String headPhoneColor = none;
private String volPos = none;
private String switchPos= none;
private String onOrOff= none;
// Default Headphone Constructor
public HeadPhone() {
volume = MEDIUM;
pluggedIn = false;
manufacturer = "Sony";
headPhoneColor = "Gold";
}
// Headphone Constructor
public HeadPhone (int volume2, boolean pluggedIn2, String manufacturer2, String
headPhoneColor2) {
volume = volume2;
pluggedIn = pluggedIn2;
manufacturer = manufacturer2;
headPhoneColor = headPhoneColor2;
}
// Setter Methods
// setVolume
public void setVolume(int volume2) {
volume = volume2;
}
// setPluggedIn
public void setPluggedIn(boolean pluggedIn2) {
pluggedIn = pluggedIn2;
}
// setManufacturer
public void setManufacturer(String manufacturer2) {
manufacturer = manufacturer2;
}
// setColor
public void setColor(String headPhoneColor2) {
headPhoneColor = headPhoneColor2;
}
// Getter Methods
// getVolume
public int getVolume() {
if(volume == 3) {
volPos = "HIGH";
}
else if (volume == 2) {
volPos = "MEDIUM";
}
else if(volume == 1) {
volPos = "LOW";
}
else {
System.out.println("Your entry is invalid, please try again.");
}
return volume;
}
//getPluggedIn
public boolean getPluggedIn() {
if(pluggedIn == false) {
onOrOff = "Off";
}
else {
onOrOff = "On";
}
return pluggedIn;
}
//getManufacturer
public String getManufacturer() {
return manufacturer;
}
//getColor
public String getColor() {
return headPhoneColor;
}
// changeVolume method
public void changeVolume(int volume2) {
setVolume(volume2);
}
// toString method
public String toString() {
String str = "toString() results: (volume=" + volume + ", pluggedIn=" + pluggedIn + ",
Manufacturer=" + manufacturer + ", HeadPhone Color=" + headPhoneColor +")";
return str;
}
}
// TestHeadPhone.java
import java.util.Scanner;
public class TestHeadPhone {
public static void main(String[] args) {
int row = 4;
HeadPhone [] hfArray = new HeadPhone[row];
HeadPhone hpObj1 = new HeadPhone();
hfArray[0] = hpObj1;
Scanner input = new Scanner(System.in);
for(int i=1; i<4; i++) {
HeadPhone hpObj2 = new HeadPhone();
System.out.println("Please enter the data for new headphones:");
System.out.println("Would you like to turn the headphones on? (Type 'true' for on, Type
'false' for off: ");
hpObj2.setPluggedIn(input.nextBoolean());
System.out.println("Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 =
High): ");
hpObj2.setVolume(input.nextInt());
System.out.println("Who manufactured the headphones? (Any string/word(s)");
hpObj2.setManufacturer(input.next());
System.out.println("What are the color of the headphones? (Any string/word(s)");
hpObj2.setColor(input.next());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
hfArray[i] = hpObj2;
}
for(int i=0; i<4; i++){
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(hfArray[i].toString());
System.out.println("getPluggedIn() results: "+hfArray[i].getPluggedIn());
System.out.println("getVolume() results: "+hfArray[i].getVolume());
System.out.println("getManufacturer() results: "+hfArray[i].getManufacturer());
System.out.println("getColor() results: "+hfArray[i].getColor());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
}
/*
output:
Please enter the data for new headphones:
Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off:
true
Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High):
1
Who manufactured the headphones? (Any string/word(s)
apple
What are the color of the headphones? (Any string/word(s)
white
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
Please enter the data for new headphones:
Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off:
true
Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High):
3
Who manufactured the headphones? (Any string/word(s)
sensiner
What are the color of the headphones? (Any string/word(s)
pink
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
Please enter the data for new headphones:
Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off:
false
Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High):
2
Who manufactured the headphones? (Any string/word(s)
voda
What are the color of the headphones? (Any string/word(s)
green
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
toString() results: (volume=2, pluggedIn=false, Manufacturer=Sony, HeadPhone Color=Gold)
getPluggedIn() results: false
getVolume() results: 2
getManufacturer() results: Sony
getColor() results: Gold
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
toString() results: (volume=1, pluggedIn=true, Manufacturer=apple, HeadPhone Color=white)
getPluggedIn() results: true
getVolume() results: 1
getManufacturer() results: apple
getColor() results: white
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
toString() results: (volume=3, pluggedIn=true, Manufacturer=sensiner, HeadPhone Color=pink)
getPluggedIn() results: true
getVolume() results: 3
getManufacturer() results: sensiner
getColor() results: pink
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
toString() results: (volume=2, pluggedIn=false, Manufacturer=voda, HeadPhone Color=green)
getPluggedIn() results: false
getVolume() results: 2
getManufacturer() results: voda
getColor() results: green
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
*/

More Related Content

Similar to HeadPhone.java All the outputs were same because the variabl.pdf

Introduction to Arduino and Circuits
Introduction to Arduino and CircuitsIntroduction to Arduino and Circuits
Introduction to Arduino and CircuitsJason Griffey
 
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersSheila Sinclair
 
Hi,Please fidn the Answer.Sorting,h is Header .pdf
Hi,Please fidn the Answer.Sorting,h is Header .pdfHi,Please fidn the Answer.Sorting,h is Header .pdf
Hi,Please fidn the Answer.Sorting,h is Header .pdfanandf0099
 
C program that prompts user to enter two floating point t.pdf
  C program that prompts user to enter two floating point t.pdf  C program that prompts user to enter two floating point t.pdf
C program that prompts user to enter two floating point t.pdfaoneonlinestore1
 
Arduino for Beginners
Arduino for BeginnersArduino for Beginners
Arduino for BeginnersSarwan Singh
 
Hi,I have updated the code. It is workin fine as expected. Highlig.pdf
Hi,I have updated the code. It is workin fine as expected. Highlig.pdfHi,I have updated the code. It is workin fine as expected. Highlig.pdf
Hi,I have updated the code. It is workin fine as expected. Highlig.pdfankitcom
 
Arduino final ppt
Arduino final pptArduino final ppt
Arduino final pptIndu Mathi
 

Similar to HeadPhone.java All the outputs were same because the variabl.pdf (11)

C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
Introduction to Arduino and Circuits
Introduction to Arduino and CircuitsIntroduction to Arduino and Circuits
Introduction to Arduino and Circuits
 
Arduino based applications part 1
Arduino based applications part 1Arduino based applications part 1
Arduino based applications part 1
 
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
 
Hi,Please fidn the Answer.Sorting,h is Header .pdf
Hi,Please fidn the Answer.Sorting,h is Header .pdfHi,Please fidn the Answer.Sorting,h is Header .pdf
Hi,Please fidn the Answer.Sorting,h is Header .pdf
 
C program that prompts user to enter two floating point t.pdf
  C program that prompts user to enter two floating point t.pdf  C program that prompts user to enter two floating point t.pdf
C program that prompts user to enter two floating point t.pdf
 
Pure kotlin
Pure kotlinPure kotlin
Pure kotlin
 
Arduino for Beginners
Arduino for BeginnersArduino for Beginners
Arduino for Beginners
 
Hi,I have updated the code. It is workin fine as expected. Highlig.pdf
Hi,I have updated the code. It is workin fine as expected. Highlig.pdfHi,I have updated the code. It is workin fine as expected. Highlig.pdf
Hi,I have updated the code. It is workin fine as expected. Highlig.pdf
 
Arduino.pptx
Arduino.pptxArduino.pptx
Arduino.pptx
 
Arduino final ppt
Arduino final pptArduino final ppt
Arduino final ppt
 

More from info518726

Program to print all combination of size r in an array of size n.pdf
 Program to print all combination of size r in an array of size n.pdf Program to print all combination of size r in an array of size n.pdf
Program to print all combination of size r in an array of size n.pdfinfo518726
 
Methanol is soluble in water. methanol is a one carbon organic alco.pdf
  Methanol is soluble in water.  methanol is a one carbon organic alco.pdf  Methanol is soluble in water.  methanol is a one carbon organic alco.pdf
Methanol is soluble in water. methanol is a one carbon organic alco.pdfinfo518726
 
Solubility in water happens when something is pol.pdf
                     Solubility in water happens when something is pol.pdf                     Solubility in water happens when something is pol.pdf
Solubility in water happens when something is pol.pdfinfo518726
 
moles = 0.144 .pdf
                     moles = 0.144                                    .pdf                     moles = 0.144                                    .pdf
moles = 0.144 .pdfinfo518726
 
It is used to remove water from the organic layer.pdf
                     It is used to remove water from the organic layer.pdf                     It is used to remove water from the organic layer.pdf
It is used to remove water from the organic layer.pdfinfo518726
 
nitrogen is more electronegative than phosphorus .pdf
                     nitrogen is more electronegative than phosphorus .pdf                     nitrogen is more electronegative than phosphorus .pdf
nitrogen is more electronegative than phosphorus .pdfinfo518726
 
ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf
                     ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf                     ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf
ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdfinfo518726
 
hydration of alkene. .pdf
                     hydration of alkene.                             .pdf                     hydration of alkene.                             .pdf
hydration of alkene. .pdfinfo518726
 
H2O + NO + NO2 -- 2HNO2 Three molecules collide.pdf
                     H2O + NO + NO2 -- 2HNO2  Three molecules collide.pdf                     H2O + NO + NO2 -- 2HNO2  Three molecules collide.pdf
H2O + NO + NO2 -- 2HNO2 Three molecules collide.pdfinfo518726
 
he coordination compounds , metal complexes or si.pdf
                     he coordination compounds , metal complexes or si.pdf                     he coordination compounds , metal complexes or si.pdf
he coordination compounds , metal complexes or si.pdfinfo518726
 
PdCl2 + 2DMSO PdCl22DMSO It forms a complexe..pdf
                     PdCl2 + 2DMSO  PdCl22DMSO  It forms a complexe..pdf                     PdCl2 + 2DMSO  PdCl22DMSO  It forms a complexe..pdf
PdCl2 + 2DMSO PdCl22DMSO It forms a complexe..pdfinfo518726
 
melting point of the mixture decreases when small.pdf
                     melting point of the mixture decreases when small.pdf                     melting point of the mixture decreases when small.pdf
melting point of the mixture decreases when small.pdfinfo518726
 
  Polymers are made by joining small molecules into large ones.But m.pdf
  Polymers are made by joining small molecules into large ones.But m.pdf  Polymers are made by joining small molecules into large ones.But m.pdf
  Polymers are made by joining small molecules into large ones.But m.pdfinfo518726
 
What is Operational ExcellenceAchieving Operational Excellence requ.pdf
What is Operational ExcellenceAchieving Operational Excellence requ.pdfWhat is Operational ExcellenceAchieving Operational Excellence requ.pdf
What is Operational ExcellenceAchieving Operational Excellence requ.pdfinfo518726
 
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdf
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdfthe name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdf
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdfinfo518726
 
Sucrose is a molecular compound and does not ionize in solution.KC.pdf
Sucrose is a molecular compound and does not ionize in solution.KC.pdfSucrose is a molecular compound and does not ionize in solution.KC.pdf
Sucrose is a molecular compound and does not ionize in solution.KC.pdfinfo518726
 
C. Increasing the temperature of the system .pdf
                     C. Increasing the temperature of the system      .pdf                     C. Increasing the temperature of the system      .pdf
C. Increasing the temperature of the system .pdfinfo518726
 
because Electron Affinity is the amount of energy.pdf
                     because Electron Affinity is the amount of energy.pdf                     because Electron Affinity is the amount of energy.pdf
because Electron Affinity is the amount of energy.pdfinfo518726
 
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdf
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdfQues-1Clostredium botulinum, bacteria that cause food poisoning &.pdf
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdfinfo518726
 
Programprinting.c#include stdio.hint main() {    cha.pdf
Programprinting.c#include stdio.hint main() {    cha.pdfProgramprinting.c#include stdio.hint main() {    cha.pdf
Programprinting.c#include stdio.hint main() {    cha.pdfinfo518726
 

More from info518726 (20)

Program to print all combination of size r in an array of size n.pdf
 Program to print all combination of size r in an array of size n.pdf Program to print all combination of size r in an array of size n.pdf
Program to print all combination of size r in an array of size n.pdf
 
Methanol is soluble in water. methanol is a one carbon organic alco.pdf
  Methanol is soluble in water.  methanol is a one carbon organic alco.pdf  Methanol is soluble in water.  methanol is a one carbon organic alco.pdf
Methanol is soluble in water. methanol is a one carbon organic alco.pdf
 
Solubility in water happens when something is pol.pdf
                     Solubility in water happens when something is pol.pdf                     Solubility in water happens when something is pol.pdf
Solubility in water happens when something is pol.pdf
 
moles = 0.144 .pdf
                     moles = 0.144                                    .pdf                     moles = 0.144                                    .pdf
moles = 0.144 .pdf
 
It is used to remove water from the organic layer.pdf
                     It is used to remove water from the organic layer.pdf                     It is used to remove water from the organic layer.pdf
It is used to remove water from the organic layer.pdf
 
nitrogen is more electronegative than phosphorus .pdf
                     nitrogen is more electronegative than phosphorus .pdf                     nitrogen is more electronegative than phosphorus .pdf
nitrogen is more electronegative than phosphorus .pdf
 
ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf
                     ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf                     ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf
ionic equation is 2 Cu+2(aq) + 4Cl-(aq) + 4 K+(aq.pdf
 
hydration of alkene. .pdf
                     hydration of alkene.                             .pdf                     hydration of alkene.                             .pdf
hydration of alkene. .pdf
 
H2O + NO + NO2 -- 2HNO2 Three molecules collide.pdf
                     H2O + NO + NO2 -- 2HNO2  Three molecules collide.pdf                     H2O + NO + NO2 -- 2HNO2  Three molecules collide.pdf
H2O + NO + NO2 -- 2HNO2 Three molecules collide.pdf
 
he coordination compounds , metal complexes or si.pdf
                     he coordination compounds , metal complexes or si.pdf                     he coordination compounds , metal complexes or si.pdf
he coordination compounds , metal complexes or si.pdf
 
PdCl2 + 2DMSO PdCl22DMSO It forms a complexe..pdf
                     PdCl2 + 2DMSO  PdCl22DMSO  It forms a complexe..pdf                     PdCl2 + 2DMSO  PdCl22DMSO  It forms a complexe..pdf
PdCl2 + 2DMSO PdCl22DMSO It forms a complexe..pdf
 
melting point of the mixture decreases when small.pdf
                     melting point of the mixture decreases when small.pdf                     melting point of the mixture decreases when small.pdf
melting point of the mixture decreases when small.pdf
 
  Polymers are made by joining small molecules into large ones.But m.pdf
  Polymers are made by joining small molecules into large ones.But m.pdf  Polymers are made by joining small molecules into large ones.But m.pdf
  Polymers are made by joining small molecules into large ones.But m.pdf
 
What is Operational ExcellenceAchieving Operational Excellence requ.pdf
What is Operational ExcellenceAchieving Operational Excellence requ.pdfWhat is Operational ExcellenceAchieving Operational Excellence requ.pdf
What is Operational ExcellenceAchieving Operational Excellence requ.pdf
 
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdf
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdfthe name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdf
the name arsenic acid, like phosphoric acid (H3PO4), does notconvey .pdf
 
Sucrose is a molecular compound and does not ionize in solution.KC.pdf
Sucrose is a molecular compound and does not ionize in solution.KC.pdfSucrose is a molecular compound and does not ionize in solution.KC.pdf
Sucrose is a molecular compound and does not ionize in solution.KC.pdf
 
C. Increasing the temperature of the system .pdf
                     C. Increasing the temperature of the system      .pdf                     C. Increasing the temperature of the system      .pdf
C. Increasing the temperature of the system .pdf
 
because Electron Affinity is the amount of energy.pdf
                     because Electron Affinity is the amount of energy.pdf                     because Electron Affinity is the amount of energy.pdf
because Electron Affinity is the amount of energy.pdf
 
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdf
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdfQues-1Clostredium botulinum, bacteria that cause food poisoning &.pdf
Ques-1Clostredium botulinum, bacteria that cause food poisoning &.pdf
 
Programprinting.c#include stdio.hint main() {    cha.pdf
Programprinting.c#include stdio.hint main() {    cha.pdfProgramprinting.c#include stdio.hint main() {    cha.pdf
Programprinting.c#include stdio.hint main() {    cha.pdf
 

Recently uploaded

8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 

Recently uploaded (20)

8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 

HeadPhone.java All the outputs were same because the variabl.pdf

  • 1. // HeadPhone.java /* All the outputs were same because the variables declared and defined in HeadPhone class were static, they will be updated only once during the program execution. removing the static keyword from the variables which will be updated during the program execution will give the desired results */ public class HeadPhone { private static final int LOW = 1; private static final int MEDIUM = 2; private static final int HIGH = 3; private int volume = 0; private boolean pluggedIn = false; private String none = null; private String manufacturer = none; private String headPhoneColor = none; private String volPos = none; private String switchPos= none; private String onOrOff= none; // Default Headphone Constructor public HeadPhone() { volume = MEDIUM; pluggedIn = false; manufacturer = "Sony"; headPhoneColor = "Gold"; } // Headphone Constructor public HeadPhone (int volume2, boolean pluggedIn2, String manufacturer2, String headPhoneColor2) { volume = volume2; pluggedIn = pluggedIn2; manufacturer = manufacturer2;
  • 2. headPhoneColor = headPhoneColor2; } // Setter Methods // setVolume public void setVolume(int volume2) { volume = volume2; } // setPluggedIn public void setPluggedIn(boolean pluggedIn2) { pluggedIn = pluggedIn2; } // setManufacturer public void setManufacturer(String manufacturer2) { manufacturer = manufacturer2; } // setColor public void setColor(String headPhoneColor2) { headPhoneColor = headPhoneColor2; } // Getter Methods // getVolume public int getVolume() { if(volume == 3) { volPos = "HIGH"; } else if (volume == 2) { volPos = "MEDIUM";
  • 3. } else if(volume == 1) { volPos = "LOW"; } else { System.out.println("Your entry is invalid, please try again."); } return volume; } //getPluggedIn public boolean getPluggedIn() { if(pluggedIn == false) { onOrOff = "Off"; } else { onOrOff = "On"; } return pluggedIn; } //getManufacturer public String getManufacturer() { return manufacturer; } //getColor public String getColor() { return headPhoneColor; } // changeVolume method public void changeVolume(int volume2) { setVolume(volume2); }
  • 4. // toString method public String toString() { String str = "toString() results: (volume=" + volume + ", pluggedIn=" + pluggedIn + ", Manufacturer=" + manufacturer + ", HeadPhone Color=" + headPhoneColor +")"; return str; } } // TestHeadPhone.java import java.util.Scanner; public class TestHeadPhone { public static void main(String[] args) { int row = 4; HeadPhone [] hfArray = new HeadPhone[row]; HeadPhone hpObj1 = new HeadPhone(); hfArray[0] = hpObj1; Scanner input = new Scanner(System.in); for(int i=1; i<4; i++) { HeadPhone hpObj2 = new HeadPhone(); System.out.println("Please enter the data for new headphones:"); System.out.println("Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off: "); hpObj2.setPluggedIn(input.nextBoolean()); System.out.println("Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 =
  • 5. High): "); hpObj2.setVolume(input.nextInt()); System.out.println("Who manufactured the headphones? (Any string/word(s)"); hpObj2.setManufacturer(input.next()); System.out.println("What are the color of the headphones? (Any string/word(s)"); hpObj2.setColor(input.next()); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); hfArray[i] = hpObj2; } for(int i=0; i<4; i++){ System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println(hfArray[i].toString()); System.out.println("getPluggedIn() results: "+hfArray[i].getPluggedIn()); System.out.println("getVolume() results: "+hfArray[i].getVolume()); System.out.println("getManufacturer() results: "+hfArray[i].getManufacturer()); System.out.println("getColor() results: "+hfArray[i].getColor()); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } }
  • 6. } /* output: Please enter the data for new headphones: Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off: true Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High): 1 Who manufactured the headphones? (Any string/word(s) apple What are the color of the headphones? (Any string/word(s) white ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ Please enter the data for new headphones: Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off: true Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High): 3 Who manufactured the headphones? (Any string/word(s) sensiner What are the color of the headphones? (Any string/word(s) pink ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ Please enter the data for new headphones: Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off: false Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High): 2 Who manufactured the headphones? (Any string/word(s) voda What are the color of the headphones? (Any string/word(s) green
  • 7. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ toString() results: (volume=2, pluggedIn=false, Manufacturer=Sony, HeadPhone Color=Gold) getPluggedIn() results: false getVolume() results: 2 getManufacturer() results: Sony getColor() results: Gold ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ toString() results: (volume=1, pluggedIn=true, Manufacturer=apple, HeadPhone Color=white) getPluggedIn() results: true getVolume() results: 1 getManufacturer() results: apple getColor() results: white ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ toString() results: (volume=3, pluggedIn=true, Manufacturer=sensiner, HeadPhone Color=pink) getPluggedIn() results: true getVolume() results: 3 getManufacturer() results: sensiner getColor() results: pink ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ toString() results: (volume=2, pluggedIn=false, Manufacturer=voda, HeadPhone Color=green) getPluggedIn() results: false getVolume() results: 2 getManufacturer() results: voda getColor() results: green
  • 8. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ */ Solution // HeadPhone.java /* All the outputs were same because the variables declared and defined in HeadPhone class were static, they will be updated only once during the program execution. removing the static keyword from the variables which will be updated during the program execution will give the desired results */ public class HeadPhone { private static final int LOW = 1; private static final int MEDIUM = 2; private static final int HIGH = 3; private int volume = 0; private boolean pluggedIn = false; private String none = null; private String manufacturer = none; private String headPhoneColor = none; private String volPos = none; private String switchPos= none; private String onOrOff= none; // Default Headphone Constructor public HeadPhone() { volume = MEDIUM; pluggedIn = false; manufacturer = "Sony"; headPhoneColor = "Gold"; }
  • 9. // Headphone Constructor public HeadPhone (int volume2, boolean pluggedIn2, String manufacturer2, String headPhoneColor2) { volume = volume2; pluggedIn = pluggedIn2; manufacturer = manufacturer2; headPhoneColor = headPhoneColor2; } // Setter Methods // setVolume public void setVolume(int volume2) { volume = volume2; } // setPluggedIn public void setPluggedIn(boolean pluggedIn2) { pluggedIn = pluggedIn2; } // setManufacturer public void setManufacturer(String manufacturer2) { manufacturer = manufacturer2; } // setColor public void setColor(String headPhoneColor2) { headPhoneColor = headPhoneColor2; } // Getter Methods // getVolume
  • 10. public int getVolume() { if(volume == 3) { volPos = "HIGH"; } else if (volume == 2) { volPos = "MEDIUM"; } else if(volume == 1) { volPos = "LOW"; } else { System.out.println("Your entry is invalid, please try again."); } return volume; } //getPluggedIn public boolean getPluggedIn() { if(pluggedIn == false) { onOrOff = "Off"; } else { onOrOff = "On"; } return pluggedIn; } //getManufacturer public String getManufacturer() { return manufacturer; } //getColor public String getColor() { return headPhoneColor; }
  • 11. // changeVolume method public void changeVolume(int volume2) { setVolume(volume2); } // toString method public String toString() { String str = "toString() results: (volume=" + volume + ", pluggedIn=" + pluggedIn + ", Manufacturer=" + manufacturer + ", HeadPhone Color=" + headPhoneColor +")"; return str; } } // TestHeadPhone.java import java.util.Scanner; public class TestHeadPhone { public static void main(String[] args) { int row = 4; HeadPhone [] hfArray = new HeadPhone[row]; HeadPhone hpObj1 = new HeadPhone(); hfArray[0] = hpObj1; Scanner input = new Scanner(System.in); for(int i=1; i<4; i++) { HeadPhone hpObj2 = new HeadPhone(); System.out.println("Please enter the data for new headphones:");
  • 12. System.out.println("Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off: "); hpObj2.setPluggedIn(input.nextBoolean()); System.out.println("Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High): "); hpObj2.setVolume(input.nextInt()); System.out.println("Who manufactured the headphones? (Any string/word(s)"); hpObj2.setManufacturer(input.next()); System.out.println("What are the color of the headphones? (Any string/word(s)"); hpObj2.setColor(input.next()); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); hfArray[i] = hpObj2; } for(int i=0; i<4; i++){ System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println(hfArray[i].toString()); System.out.println("getPluggedIn() results: "+hfArray[i].getPluggedIn()); System.out.println("getVolume() results: "+hfArray[i].getVolume()); System.out.println("getManufacturer() results: "+hfArray[i].getManufacturer()); System.out.println("getColor() results: "+hfArray[i].getColor());
  • 13. System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } } } /* output: Please enter the data for new headphones: Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off: true Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High): 1 Who manufactured the headphones? (Any string/word(s) apple What are the color of the headphones? (Any string/word(s) white ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ Please enter the data for new headphones: Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off: true Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High): 3 Who manufactured the headphones? (Any string/word(s) sensiner What are the color of the headphones? (Any string/word(s) pink ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ Please enter the data for new headphones: Would you like to turn the headphones on? (Type 'true' for on, Type 'false' for off: false
  • 14. Please the the volume (Options are by integer (1 = Low, 2 = Medium, 3 = High): 2 Who manufactured the headphones? (Any string/word(s) voda What are the color of the headphones? (Any string/word(s) green ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ toString() results: (volume=2, pluggedIn=false, Manufacturer=Sony, HeadPhone Color=Gold) getPluggedIn() results: false getVolume() results: 2 getManufacturer() results: Sony getColor() results: Gold ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ toString() results: (volume=1, pluggedIn=true, Manufacturer=apple, HeadPhone Color=white) getPluggedIn() results: true getVolume() results: 1 getManufacturer() results: apple getColor() results: white ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ toString() results: (volume=3, pluggedIn=true, Manufacturer=sensiner, HeadPhone Color=pink) getPluggedIn() results: true getVolume() results: 3 getManufacturer() results: sensiner getColor() results: pink ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • 15. ~~~~~~~~~~~~~~~~~~~~~ toString() results: (volume=2, pluggedIn=false, Manufacturer=voda, HeadPhone Color=green) getPluggedIn() results: false getVolume() results: 2 getManufacturer() results: voda getColor() results: green ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ */