Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make the tv program run

Status
Not open for further replies.

cgikanga

MIS
Jun 3, 2001
2
US
class TV{
private boolean power;
private int channel;
private int volume;

TV() {
this.power=false;
this.channel=2;
this.volume=0;
}

boolean getPower() {
return this.power;
}

int getChannel() {
return this.channel;
}

int getVolume() {
return this.volume;
}

void getDisplay() {
System.out.println("The power is "+this.power+".");
System.out.println("The channel is "+this.channel+".");
System.out.println("The volume is "+this.volume+".");
}

if (this.power=1){
this.power=true;
else
this.power= false;
System.out.println("Please enter 1 to turn on TV or 0 to turn off TV.");
}
if (this.channel >= 2){
this.channel++;
else if (this.channel > 13)
this.channel=2;
else
System.out.println("Re-enter again!!!");
}

if ((this.volume >= 0)&&(this.volume<10)){
this.volume++;
else
System.out.println(&quot;Re-enter again!!!&quot;);
}
}
 
Code:
import java.util.*;
import java.io.*;

public class TV {
  private boolean power = false;
  private int channel = 2;
  private int volume = 0;

  public boolean getPower() { return this.power; }
  public int getChannel() { return this.channel; }
  public int getVolume() { return this.volume; }

  public void setPower(boolean power) {
    this.power = power;
    System.out.println(&quot;Power set to &quot; + this.power);
  }

  public void setChannel(int channel) {
    this.channel = channel;
    System.out.println(&quot;Chanel set to &quot; + this.channel);
  }

  public void setVolume(int volume) {
    this.volume = volume;
    System.out.println(&quot;Volume set to &quot; + this.volume);
  }

  public String toString() {
    String output = new String();
    output += &quot;The power is &quot; + this.power + &quot;.\n&quot;;
    output += &quot;The channel is &quot; + this.channel + &quot;.\n&quot;;
    output += &quot;The volume is &quot; + this.volume + &quot;.&quot;;
    return output;
  }

  private void processString(String input) {
    StringTokenizer tokenizer = new StringTokenizer(input);
    String currentToken = new String();
    while (tokenizer.hasMoreElements()) {
      currentToken = tokenizer.nextToken();
      if (currentToken.equals(&quot;channel&quot;)) {
        currentToken = tokenizer.nextToken();
        try {
          setChannel(Integer.parseInt(currentToken));
        } catch(NumberFormatException e) {
          e.printStackTrace();
        }
      } else if (currentToken.equals(&quot;volume&quot;)) {
        currentToken = tokenizer.nextToken();
        try {
          setVolume(Integer.parseInt(currentToken));
        } catch(NumberFormatException e) {
          e.printStackTrace();
        }
      } else if (currentToken.equals(&quot;power&quot;)) {
        currentToken = tokenizer.nextToken();
        try {
          setPower(Boolean.valueOf(currentToken).booleanValue());
        } catch(Exception e) {
          e.printStackTrace();
        }
      } else {
        showHelpMessage();
        break;
      }
    }
  }

  private void showHelpMessage()
  {
    System.out.println(&quot;Usage of the command line:\nchannel c volume v power p&quot;);
    System.out.println(&quot;You can use one or more of those commands at a time&quot;);
  }

  public static void main(String[] args)
  {
    TV tv = new TV();
    tv.doShell();
  }

  public void doShell() {
    InputStreamReader stream = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(stream);
    String input = new String();
    try {
      while (!(input = reader.readLine()).equals(&quot;quit&quot;)) {
        processString(input);
      }
    } catch ( IOException ioe ) {
      System.exit(-1);
    }
  }
}

You should easily be able to adapt the set-methods to your needs (like range checking). If you have any questions just post them here or mail me at haslo@haslo.ch. allow thyself to be the spark that lights the fire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top