Compiling New Features


Ongoing Learning--Compiling New Structures

As we have noted before in class, as you leave the university and begin our careers, you will no longer have someone around to teach you in the usual sense.  You will need to extend your knowledge on your own to new related concepts and even transfer our understanding to what appear to be completely different situations.  You can do it.  

As an example of this activity, we considered the problem of translating classes and objects by looking at the following simple program as an example:

public class CounterClass
{
  private int counter = 0;
  public static final char BOY  = 'M';
  public static final char GIRL = 'F';
  public CounterClass()
  {
    // This constructor does nothing.  Use it when you want to start
    // the counter at 0.
  }
  public CounterClass(int startingValue)
  {
     counter = startingValue;
  }
  public void plusOne()
  {
    counter++;
  }
  public void minusOne()
  {
    counter--;
  } 
  public int getCounter()
  {
    return counter;
  }
}
public class Countem
{
  public static void main (String[] args) throws Exception
  {
    CounterClass numberOfBoys  = new CounterClass();
    CounterClass numberOfGirls = new CounterClass(10);
    char gender;
    while (true)
    {
      System.out.print("Please enter M for a boy or F for a girl > ");
      gender = BasicIo.readCharacter();
      if (gender == 'M')
      {
        numberOfBoys.plusOne();
      }
      else
      {
        numberOfGirls.plusOne();
      }
      System.out.println("Boys  = " + numberOfBoys.getCounter());
      System.out.println("Girls = " + numberOfGirls.getCounter());
      System.out.println("Total = " + (numberOfBoys.getCounter() + numberOfGirls.getCounter()));
    }
  }
}

We decided on a number of approaches to translating classes: