Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Homework 3

CMIS 141
July 29 2020
WON KIM

public class HeadPhone {


// Three constants named LOW, MEDIUM and HIGH with values of 1, 2 and 3 to denote the
headphone volume.
public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;

private int volume = MEDIUM; // A private int data field named volume that specifies the volume
of the headphone. The default volume is MEDIUM.
private boolean pluggedIn = false; // A private boolean data field named pluggedIn that specifies if
the headphone is plugged in. The default value is false.
private String manufacturer; // A private String data field named manufacturer that specifies the
name of the manufacturer of the headphones.
private String headPhoneColor; // A private Color data field named headPhoneColor that specifies
the color of the headphones.
private String headPhoneModel; // A private String data field named headPhoneModel that
specifies the Model of the headphones.

//getter and setter methods for all data fields.


public int getVolume() {
return volume;
}

public void setVolume(int volume) {


this.volume = volume;
}

public boolean isPluggedIn() {


return pluggedIn;
}

public void setPluggedIn(boolean pluggedIn) {


this.pluggedIn = pluggedIn;
}

public String getManufacturer() {


return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}

public String getHeadPhoneColor() {


return headPhoneColor;
}

public void setHeadPhoneColor(String headPhoneColor) {


this.headPhoneColor = headPhoneColor;
}

public String getHeadPhoneModel() {


return headPhoneModel;
}

public void setHeadPhoneModel(String headPhoneModel) {


this.headPhoneModel = headPhoneModel;
}

public static int getLow() {


return LOW;
}

public static int getMedium() {


return MEDIUM;
}

public static int getHigh() {


return HIGH;
}

// A no argument constructor that creates a default headphone.


public HeadPhone() {
super();
this.volume = volume;
this.pluggedIn = pluggedIn;
this.manufacturer = manufacturer;
this.headPhoneColor = headPhoneColor;
this.headPhoneModel = headPhoneModel;
}

// A constructor using fields


public HeadPhone(int volume, boolean pluggedIn, String manufacturer, String headPhoneColor,
String headPhoneModel) {
super();
this.volume = volume;
this.pluggedIn = pluggedIn;
this.manufacturer = manufacturer;
this.headPhoneColor = headPhoneColor;
this.headPhoneModel = headPhoneModel;
}

// A method named toString() that returns a string describing the current field values of the
headphones.
@Override
public String toString() {
return "HeadPhone [volume=" + volume + ", pluggedIn=" + pluggedIn
+ ", manufacturer=" + manufacturer + ", headPhoneColor="
+ headPhoneColor + ", headPhoneModel=" + headPhoneModel + "]";
}

public String toStringAfterChange() {


return "Changed volume to: " + volume
+ "\nChanged pluggedIn to: " + pluggedIn
+ "\nChanged manufacturer to: " + manufacturer
+ "\nChanged headPhone color to: " + headPhoneColor
+ "\nChanged headPhone model to: " + headPhoneModel + "\n";
}

// A method named changeVolume(value) that changes the volume of the headphone to the value
passed into the method
public int changeVolume(int value){
if(value == 1){
setVolume(getLow());
}else if(value == 2){
setVolume(getMedium());
}else if(value == 3){
setVolume(getHigh());
}

return getVolume();
}

public static void main(String[] args) {


// TODO Auto-generated method stub
}

//UMGC- CMIS141
//HOMEWORK 3
//File Name: TestHeadPhone
//Author: STUDENT: WONKIM

package HeadPhone;

public class TestHeadphone {

public static void main(String[] args) {


// Create a TestHeadPhone class that constructs at least 3 HeadPhone objects.
HeadPhone testHeadPhone1 = new HeadPhone(1, false, "Bose", "Red", "123");
HeadPhone testHeadPhone2 = new HeadPhone(2, false, "JBL", "Blue", "xyz");
HeadPhone testHeadPhone3 = new HeadPhone(3, false, "Sony", "Green", "zyx");
// Test case1
System.out.println("***Output***");
System.out.println(testHeadPhone1.toString());
testHeadPhone1.changeVolume(2);
testHeadPhone1.setPluggedIn(true);
testHeadPhone1.setManufacturer("Samsung");
testHeadPhone1.setHeadPhoneColor("Black");
testHeadPhone1.setHeadPhoneModel("Galaxy");
System.out.println(testHeadPhone1.toStringAfterChange());
// Test case2
System.out.println("***Output***");
System.out.println(testHeadPhone2.toString());
testHeadPhone2.changeVolume(3);
testHeadPhone2.setPluggedIn(true);
testHeadPhone2.setManufacturer("Apple");
testHeadPhone2.setHeadPhoneColor("White");
testHeadPhone2.setHeadPhoneModel("iphone");
System.out.println(testHeadPhone2.toStringAfterChange());
// Test case3
System.out.println("***Output***");
System.out.println(testHeadPhone3.toString());
testHeadPhone3.changeVolume(1);
testHeadPhone3.setPluggedIn(true);
testHeadPhone3.setManufacturer("Microsoft");
testHeadPhone3.setHeadPhoneColor("Blue");
testHeadPhone3.setHeadPhoneModel("Surface");
System.out.println(testHeadPhone3.toStringAfterChange());

}
Test Cases
Expected Output Actual Output Pass?
Headphone # 1 Headphone # 1 Yes
Manufacturer: Bose Manufacturer: Bose
Model: 123 Model: 123
Color: red Color: red
Plugged In: false Plugged In: false
Volume: 1 Volume: 1
Changed vol: 2 Changed vol: 2
Changed pluggedIn: true Changed pluggedIn: true
Changed manufacturer to: Samsung Changed manufacturer to: Samsung
Changed headphone color to: black Changed headphone color to: black
Changed headphone model to:Galaxy Changed headphone model to: Galaxy

Headphone # 2 Headphone # 2 Yes


Manufacturer: JBL Manufacturer: JBL
Model: xyz Model: xyz
Color: Blue Color: Blue
Plugged In: false Plugged In: false
Volume: 2 Volume: 2
Changed vol: 3 Changed vol: 3
Changed pluggedIn: true Changed pluggedIn: true
Changed manufacturer to: apple Changed manufacturer to: apple
Changed headphone color to: white Changed headphone color to: white
Changed headphone model to: Changed headphone model to: iphone
iphone
Headphone # 3 Headphone # 3 Yes
Manufacturer: Sony Manufacturer: Sony
Model: zyx Model: zyx
Color: Green Color: Green
Plugged In: false Plugged In: false
Volume: 3 Volume: 3
Changed vol: 1 Changed vol: 1
Changed pluggedIn: true Changed pluggedIn: true
Changed manufacturer to: Changed manufacturer to: Microsoft
Microsoft Changed headphone color to: Blue
Changed headphone color to: Blue Changed headphone model to: surface
Changed headphone model to:
surface

You might also like