Solution for Midterm II

Problem 1

We are going to construct TV sets. Each TV is built to handle a maximum number of channels (which defaults to 99). The TV is smart enough to remember the channel you are viewing and the previous channel you viewed. Initially both of these channels are set to 1. Channel numbers must always remain between 1 and the maximum (inclusive). The special feature alternateChannel allows you to quickly switch between the last two channels. Given the above description, write a class that implements the following TV interface:
interface TVI {
  final int DEFAULT_MAX = 99;
  int getCurrentChannel();
  void incChannel ();
  void decChannel ();
  void alternateChannel ();
}

Solution

class TV implements TVI {
  int maxChannels;
  int currentChannel;
  int previousChannel;

  TV () { this(DEFAULT_MAX); }
  TV (int m) { maxChannels = m; currentChannel=1; previousChannel=1; }

  public int getCurrentChannel () { return currentChannel; }

  public void incChannel () { 
    previousChannel = currentChannel;
    currentChannel++; 
    if (currentChannel > maxChannels) currentChannel=1; 
  }

  public void decChannel () { 
    previousChannel = currentChannel;
    currentChannel--; 
    if (currentChannel < 1) currentChannel=maxChannels;
  }

  public void alternateChannel () {
    int temp;
    temp = currentChannel;
    currentChannel = previousChannel;
    previousChannel = temp;
  }
}

Problem 2

Write a method with the following declaration:
int numberOfLines (StreamTokenizer st) throws java.io.IOException;
that reads tokens from the StreamTokenizer object st until the end of the stream, and returns a count of the number of lines it encountered. You may assume that the object st treats ends of line as tokens (see the method eolIsSignificant in the API).

Solution

  int numberOfLines (StreamTokenizer st) throws java.io.IOException {
    int count = 0;
    while (st.nextToken() != st.TT_EOF) {
      if (st.ttype == st.TT_EOL) count++;
    }
    return count;
  }

Problem 3

Imagine that every appliance in your house can perform your commands (for example, turn itself off). You may at any time come up with new commands for your appliances to perform. Sketch a program design that represents two of the appliances in your house, and arranges for these appliances to respond to arbitrary commands. Your solution must be in the form of correct Java code including class definitions, methods, interfaces, etc, but you can leave the bodies of the non-essential methods blank.

Solution

abstract class Appliance {
  abstract void accept (Command c);
  abstract void turnYourselfOff();
}

class TV extends Appliance {
  void accept (Command c) { c.forTV(this); }
  void incChannel () { 
    previousChannel = currentChannel;
    currentChannel++; 
    if (currentChannel > maxChannels) currentChannel=1; 
  }
  void turnYourselfOff() { ... }
}

class Fridge extends Appliance {
  void accept (Command c) { c.forFridge(this); }
  void turnYourselfOff() { ... }
}

interface Command {
  void forTV (TV a);
  void forFridge (Fridge a);
}

class OffCommand extends Command {
  void forTV (TV a) { a.turnYourselfOff(); }
  void forFridge (Fridge a) { a.turnYourselfOff(); }
}

class ChangeChannel extends Command {
  void forTV (TV a) { a.incChannel(); }
  void forFridge (Fridge a) { /* ignore */ }
}


Visited times since December 15, 1997 (or the last crash).

sabry@cs.uoregon.edu