patternjavaMinor
Java class for creating HeadPhone class and Test class
Viewed 0 times
creatingjavaheadphonefortestandclass
Problem
Here is the Assignment instructions:
Create a Java class named HeadPhone to represent a headphone set. The
class contains:
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 Head
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 Head
Solution
I think your
It is always a good idea to make your members private unless you have a reason for giving them another access level.
As for other ideas, your specification specificies very detailed what your code should do, so there is not so much possibility to change anything. But the constants could be replaced with enums (of course this does not conform to your spec).
Apart from that, you can replace
After
If/else statements in
changeVolume() is correct. But it does actually the same thing like setVolume(), which may be sort of redundant.It is always a good idea to make your members private unless you have a reason for giving them another access level.
As for other ideas, your specification specificies very detailed what your code should do, so there is not so much possibility to change anything. But the constants could be replaced with enums (of course this does not conform to your spec).
Apart from that, you can replace
if(pluggedIn==true) with if(pluggedIn) and if(pluggedIn!=true) with if(!pluggedIn)After
if(!pluggedIn) you don't need if(pluggedIn == true && volume == 1) because it can only be true. So if(volume == 1) is enough.If/else statements in
getVolume() can be somewhat simplified by using a switch statement.Context
StackExchange Code Review Q#94804, answer score: 5
Revisions (0)
No revisions yet.