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;
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
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";
}
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~