/* This class represents a TV which has the ability to generate a picture-in-picture (pip). The class will remember its current channel and volume, * have a max number of channels, a max volume of 100, and if pip capable, then whether pip is on or off and if on, the channel and the location * of the picture in picture (location is a number from 0-3 to represent UL, UR, LR, LL). The instance data startChannel and lastVolume are used * to indicate what channel and volume the TV is set to upon being turned on and on is used to indicate whether the tv is on or off. */ public class TV { // maxChannels - maximum channel, assumed to be 99 if not specified // channel - current channel, set to 1 initially but if startChannel is establisehd, then the startChannel is used every time the tv is turned on // volume - set to 0 initially, is set to lastVolume every time the tv is turned on thereafter // channel2 - the pip channel if on // location - the quadrant of the screen that the pip is displayed if on // pipCapable - can the TV do pip? // pipOn - whether pip is on or off (if pipCapable) // on - whether the tv is on or off, all TVs start with the tv off private int maxChannels, channel, volume, startChannel, channel2, location, lastVolume; private boolean pipCapable, pipOn, on; // 3 constructors, a no-arg, a 1-arg where we are given the maximum number of channels, and a 2-arg where we are given the max channels and pipCapable, everything // else is set to "manufacturer values" (channel 1, volume 0, startChannel 1, pipOn and on are false) public TV() { maxChannels = 99; channel = 1; volume = 0; startChannel = 1; channel2 = -1; location = -1; lastVolume = 0; pipCapable = false; pipOn = false; on = false; } public TV(int m) { maxChannels = m; channel = 1; volume = 0; startChannel = 1; channel2 = -1; location = -1; lastVolume = 0; pipCapable = false; pipOn = false; on = false; } public TV(int m, boolean p) { maxChannels = m; channel = 1; volume = 0; startChannel = 1; channel2 = -1; location = -1; lastVolume = 0; pipCapable = p; pipOn = false; on = false; } // mutator to change the channel as long as the new channel is within bounds public void setChannel(int c) { if(c<=maxChannels&&c>0) channel = c; } // program the TV to have a new start channel rather than the default of 1 public void setStartChannel(int c) { if(c>0&&c<=maxChannels) startChannel = c; } // mutator to change the volume as long as the new volume is within bounds public void setVolume(int v) { if(v>=0&&v<=100) volume = v; } // increase the channel by 1, if you reach maxChannels, rotate around back to channel 1 public void incChannel() { if(channel1) channel--; else channel=maxChannels; } // increase the volume by 1, max at 100 public void incVolume() { if(volume<100) volume++; } // decrease the volume by 1, min at 0 public void decVolume() { if(volume>0) volume--; } // if pipCapable, turn on pip establishing the channel as the same as the larger screen's channel, and its location in the UL (0) public void turnOnPip() { if(pipCapable&&!pipOn) { pipOn = true; channel2 = channel; location = 0; } } // turn pip off (note that we don't bother to check for pipCapable, not really needed) public void turnOffPip() { pipOn = false; } // if pip is on, rotate the location by 1, wrapping back to UL upon a full rotation public void moveLocation() { if(pipOn) location++; if(location>3) location = 0; } // change the pip channel as long as pip is on and the new channel is in bounds public void changePipChannel(int c) { if(pipOn&&c<=maxChannels&&c>0) channel2 = c; } // turn the TV on or off public void onOff() { if(on) // if on, TV turned off, reset channels, save last volume so that the next time we turn on the TV, it starts at the new volume { on = false; channel = channel2 = 1; lastVolume = volume; volume = 1; pipOn = false; } else { // turn TV on, set channel to startChannel and set volume to the last volume before it was turned off, keep pip off on = true; channel = startChannel; volume = lastVolume; pipOn = false; } } public String toString() // If the TV is on, output info about what the TV is doing now including if pip is on, if off, output "OFF" { if(!on) return "OFF"; else { String temp = "Current channel: " + channel + "\nCurrent volume: " + volume; if(pipOn) { temp += "\nPicture in Picture on\n Channel: " + channel2 + " at location " + (location+1); } return temp; } } }