วันจันทร์ที่ 6 กุมภาพันธ์ พ.ศ. 2555

TV

package
java_1;


public
class TV {

private int chanel = 1;//1-10

private int volume = 1;//0-5

private boolean status = false;// By default TV is off

private String brand = "";

public static int count = 0;


TV(){

count++;

brand = "Sony";

}

TV(String newBrand){

count++;

brand = newBrand;

}

TV(String newBrand,
int newchanel){

count++;

brand = newBrand;

chanel = newchanel;

}

public String getBrand(){

return brand;

}

public void turnOn(){

//Mutator of status variable

status = true;

}

public void turnOff(){

//Mutator of status variable

status = false;

}

public boolean getStatus(){

//Accessor of status variable

return status;

}

public void chanelUp(){

chanel++;

if(chanel > 10){

chanel = 1;


}

}

public void chanelDown(){

chanel--;

if(chanel < 1){

chanel = 10;

}

}

public int getChanel(){

if(status == false)

{

System.
out.println("Please TurnOn TV");

return 0;

}

else

return chanel;

}

public void volumeUp(){

if(volume < 5)

volume++;

else

System.
out.println("Maximum");

}

public void volumeDown(){

if(volume > 0)

volume--;

else

System.
out.println("Minimum");


}

public int getVolume(){

return volume;

}

public boolean equalsTV(TV o){

if(o.getChanel() != this.getChanel())

return false;

if(o.getStatus() != this.getStatus())

return false;

if(o.getVolume() != this.getVolume())

return false;

return true;

}


}

Test TV

package
java_1;
import
java.util.ArrayList;
public
class Test {
public static void main(String[] args) {
TV tv1 =
new TV();
System.
out.println("******TV 1*******");
tv1.turnOn();
//turnOn TV
System.
out.println(tv1.getBrand());
System.
out.println(tv1.getChanel());
tv1.chanelDown();
System.
out.println(tv1.getChanel());
tv1.volumeDown();
tv1.volumeDown();
System.
out.println(tv1.getVolume());
tv1.volumeUp();
tv1.volumeUp();
tv1.volumeUp();
tv1.volumeUp();
tv1.volumeUp();
tv1.volumeUp();
System.
out.println(tv1.getVolume());
TV tv2 =
new TV("Sumsung",10);
System.
out.println("******TV 2*******");
tv2.turnOn();
//turnOn TV
System.
out.println(tv2.getBrand());
System.
out.println(tv2.getChanel());
tv2.chanelUp();
System.
out.println(tv2.getChanel());
////// ArrayList ////
ArrayList<TV> listTV =
new ArrayList<TV>();
listTV.add(
new TV());
listTV.add(
new TV("PHILIPS"));
listTV.add(
new TV("SAMSUNG"));
listTV.add(
new TV("PANASONIC"));
System.
out.println("ArraySize:"+listTV.size());
System.
out.println(listTV.get(0).getBrand());
System.
out.println(listTV.get(1).getBrand());
System.
out.println(listTV.get(2).getBrand());
System.
out.println(listTV.get(3).getBrand());
////// Array /////
/*
TV[] arrayTV = new TV[5];
for(int i = 0; i < 5; i++)
{
arrayTV[i] = new TV();
}
System.out.println("Count TV:"+TV.count);
System.out.println(arrayTV[0].getBrand());
System.out.println("Brand:"+arrayTV[0].getBrand());
*/
///// check equals /////
/*tv1.turnOn();
tv1.turnOn();
if(tv1.equalsTV(tv2)==true){
System.out.println("Equal");
}else{
System.out.println("Not equal");
}
tv2.chanelDown();
if(tv1.equalsTV(tv2)==true){
System.out.println("Equal");
}else{
System.out.println("Not equal");
}*/
}
}