this compiles but i am not getting right output.......this is the output that i was hoping but somehow it is not getting right......can someone help me what is the problem with my code.
public class Television
{
//Instance variables
private int channel;
private int volume;
boolean power = false;
String[] channelName = {"CBS","FOX","DISCOVERY","PBS","HBO","CNN","DISNEY ","CNN","TBS","USA"} ;
//No argument constructors
public Television ()
{
this(1,1);
}
//Overloaded constructor
public Television(int ch, int vol)
{
setChannel (ch);
setVolume (vol);
}
//Mutator methods
public void powerOn()
{
this.power = true;
}
public void powerOff()
{
this.power = false;
}
public void increaseVolume()
{
volume++;
}
public void decreaseVolume()
{
volume--;
}
public void setVolume (int volume)
{
if (this.power == true)
{
if (volume < 0)
{
this.volume = 0;
}
if (volume > 10)
{
this.volume = 10;
}
}
}
public void setChannel (int channel)
{
if (this.power == true)
{
if (channel < 1)
{
this.channel = 1;
}
if (channel >10)
{
this.channel = 10;
}
}
}
//Accessor methods
public int getVolume()
{
return volume;
}
public int getChannel()
{
return channel;
}
public String[] getChannelName()
{
if (this.power == true)
{
if (this.channel ==1)
{
channelName[0] = "CBS";;
}
if (this.channel ==2)
{
channelName[1] = "FOX";
}
if (this.channel ==3)
{
channelName[2] = "DISCOVERY";
}
if (this.channel ==4)
{
channelName[3] = "PBS";
}
if (this.channel ==5)
{
channelName[4] = "HBO";
}
if (this.channel ==6)
{
channelName[5] = "CNN";
}
if (this.channel ==7)
{
channelName[6] = "DISNEY";
}
if (this.channel ==8)
{
channelName[7] = "CNN";
}
if (this.channel ==9)
{
channelName[8] = "TBS";
}
if (this.channel ==10)
{
channelName[9] = "USA";
}
}
return channelName;
}
public String toString()
{
return String.format(" TV State: \n Channel No: %d \n Channel Name: %s \n Volume: %d",getChannel(), getChannelName(), getVolume() );
}
} //end class
//television app
public class TelevisionApp
{
public static void main (String[] args)
{
Television T1 = new Television(3,5);
T1.powerOn();
Television T2 = new Television ();
T2.powerOn();
T2.setChannel(4);
T2.setVolume(10);
System.out.println (T1.toString());
System.out.println ("T2 = "+T2);
}
}
//output should be something like this
TV Status: On
Channel No: 4
Channel Name: HBO
volume : 6