Class to model headphones and Junit test

Class to model headphones and Junit test

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.

The google recommended Java style guide, provided as link in the week 2 content, should be used to format and document your code. Specifically, the following style guide attributes should be addressed:

 Header comments include filename, author, date and brief purpose of the program.

 In-line comments used to describe major functionality of the code.

 Meaningful variable names and prompts applied.

 Class names are written in UpperCamelCase.

 Variable names are written in lowerCamelCase.

 Constant names are in written in All Capitals.

 Braces use K&R style. 

Solution

HeadPhone

import java.awt.Color;

public class HeadPhone {

public static final int LOW = 1;

public static final int MEDIUM = 1;

public static final int HIGH = 1;

private int volume;

private booleanpluggedIn;

private String manufacturer;

private Color headPhoneColor;

private String headPhoneModel;

/*

* This method sets volume to the headphone.

* It receives one argument – int volume

*/

public void setVolume(int volume){

this.volume = volume;

}

/*

* this method returns the volume of the headphone

*/

public intgetVolume(){

return this.volume;

}

/*

* This method sets boolean value to plugged in argument.

* It receives one argument – booleanpluggedIn

*/

public void setPluggedIn(booleanpluggedIn){

this.pluggedIn = pluggedIn;

}

/*

* this method returns true if headphone is plugged in

* and false if it is not plugged in

*/

public booleangetPluggedIn(){

return this.pluggedIn;

}

/*

* This method sets name of the manufacturer for headphone.

* It receives one argument – string manufacturer

*/

public void setManufacturer(String manufacturer){

this.manufacturer = manufacturer;

}

/*

* this method returns the manufacturer of headphone

*/

public String getManufacturer(){

return this.manufacturer;

}

/*

* This method sets color of the headphone.

* It receives one argument – Color headPhoneColor

*/

public void setHeadPhoneColor(Color headPhoneColor){

this.headPhoneColor = headPhoneColor;

}

/*

* this method returns the color of the headphone

*/

public Color getHeadPhoneColor(){

return this.headPhoneColor;

}

/*

* This method sets model of the headphone

* It receives one argument – string headPhoneModel

*/

public void setHeadPhoneModel(String headPhoneModel){

this.headPhoneModel = headPhoneModel;

}

/*

* this method returns the model of the headphone

*/

public String getHeadPhoneModel(){

return this.headPhoneModel;

}

/*

* no argument constructor of HeadPhone class

*/

public HeadPhone(){

volume = MEDIUM;

pluggedIn = false;

}

/*

* This method returns String – information about the headphone

* with following order:

* volume, plugged in/not plugged in, manufacturer, color, model

*/

public String toString(){

String answer = “volume equals: ” + Integer.toString(volume);

if(pluggedIn) answer += “, plugged in, “;

else answer += “, not plugged in, “;

answer += “manufacturer is: ” + manufacturer;

answer += “, Color is ” + String.valueOf(headPhoneColor.getRGB());

answer += “, model is ” + headPhoneModel;

return answer;

}

/*

* This method changes volume to the headphone

* It receives one argument – int volume

*/

public void changeVolume(int volume){

this.volume = volume;

}

}

TestHeadPhone

import java.awt.Color;

public class TestHeadPhone {

public static void main(String[] args) throws Exception {

test1();

test2();

test3();

}

public static void test1() throws Exception{

HeadPhonehp = new HeadPhone();

hp.setHeadPhoneColor(Color.BLACK);

hp.setHeadPhoneModel(“model1”);

hp.setManufacturer(“Panasonic”);

hp.setPluggedIn(true);

hp.setVolume(HeadPhone.HIGH);

if(!hp.getHeadPhoneColor().equals(Color.BLACK)){

throw new Exception(“HeadPhone color is not black”);

}

if(!hp.getHeadPhoneModel().equals(“model1”)){

throw new Exception(“HeadPhone model is not model1”);

}

if(!hp.getManufacturer().equals(“Panasonic”)){

throw new Exception(“HeadPhone manufacturer is not panasonic”);

}

if(!hp.getPluggedIn()){

throw new Exception(“HeadPhone is not plugged in”);

}

System.out.println(hp.toString());

}

public static void test2() throws Exception{

HeadPhonehp = new HeadPhone();

hp.setHeadPhoneColor(Color.GREEN);

hp.setHeadPhoneModel(“model2”);

hp.setManufacturer(“Sony”);

hp.setPluggedIn(true);

hp.setVolume(HeadPhone.HIGH);

if(!hp.getHeadPhoneColor().equals(Color.GREEN)){

throw new Exception(“HeadPhone color is not green”);

}

if(!hp.getHeadPhoneModel().equals(“model2”)){

throw new Exception(“HeadPhone model is not model2”);

}

if(!hp.getManufacturer().equals(“Sony”)){

throw new Exception(“HeadPhone manufacturer is not Sony”);

}

if(!hp.getPluggedIn()){

throw new Exception(“HeadPhone is not plugged in”);

}

System.out.println(hp.toString());

}

public static void test3() throws Exception{

HeadPhonehp = new HeadPhone();

hp.setHeadPhoneColor(Color.WHITE);

hp.setHeadPhoneModel(“model3”);

hp.setManufacturer(“Samsung”);

hp.setPluggedIn(false);

hp.setVolume(HeadPhone.HIGH);

if(!hp.getHeadPhoneColor().equals(Color.WHITE)){

throw new Exception(“HeadPhone color is not green”);

}

if(!hp.getHeadPhoneModel().equals(“model3”)){

throw new Exception(“HeadPhone model is not model3”);

}

if(!hp.getManufacturer().equals(“Samsung”)){

throw new Exception(“HeadPhone manufacturer is not Samsung”);

}

if(hp.getPluggedIn()){

throw new Exception(“HeadPhone is not plugged in”);

}

System.out.println(hp.toString());

}

} 

In this testcase new HeadPhone was created and in yellow are colored fields that define new HeadPhone. Then, by four test cases getter methods are tested and then toString method is called that prints information colored in yellow in Colsole.

In this test case new HeadPhone was created and in yellow are colored fields that define new HeadPhone. Then, by four test cases getter methods are tested and then to String method is called that prints information colored in yellow in Colsole.

In this testcase new HeadPhone was created and in yellow are colored fields that define new HeadPhone. Then, by four test cases getter methods are tested and then toString method is called that prints information colored in yellow in Colsole.