V22.0101 Programming Assignment 2 Spring 2004
Digital Displays

Microwave ovens, clock radios, electronic watches etc. typically use a standard digital display where each digit consists of 7 bars (line segments) that are either lighted or not. For example, the digit 8 has all bars lighted, while the digit 3 has all but two lighted.

This assignment asks you to read a String of 3*5 digits from a dialog box, display them in an applet in the bar format as three integers (five digits per line), and then add the integers and display the sum on a 4th line.

Let ACCROSS = 5 be the final variable representing the number of digits in an integer and DOWN = 3 be the final variable representing the number of lines. Keep in mind that the grader will change these values to ensure your code works with other combinations (for example she could change ACROSS to 4 and DOWN to 2 so your program would add two four digit numbers together). You can assume she will not use numbers higher than five for ACROSS and three for DOWN so you will not have to resize the applet window.

Start by creating the bare bones of your program (See Introductory program where some this has been done for you):

The work really begins with writing the method

  
     public void drawBars(Graphics g, int xStart, int yStart,
                          boolean upperLeft, boolean top, boolean upperRight, 
                          boolean lowerRight, boolean bottom, 
                          boolean lowerLeft, boolean middle)

This method should draw a 7-bar digit on your applet, where the 7 bars are either "on" or "off" according to the parameter values. The drawing should start at pixel (xStart,yStart), which is the middle left of the digit, and move along the 7 bars making up the digit 8. Only draw the bars that are "on" (actually, we will use two different colors to represent on and off). For example, the digit 8 is displayed if all 7 Boolean parameters are true, and the digit 3 is displayed if all but upperLeft and lowerLeft are true. The size of the digit should be chosen so that each bar is 20 pixels wide. Debug this method by having paint call it directly with various input values (including combinations of bars that are not valid digits) before going on to the next part.

You will notice from this point forward I use strings and characters to pass the values of the digits from one method to the other. You can follow my lead OR you can choose to pass integer values (it may be wise to use long instead of int).

Method drawBars is at the lowest level of our design and will be called by the next method you write:

 
   public void processDigit(Graphics g, int xStart, int yStart, char digit)
   {  
      switch (digit) 
      {  
      case '3': 
         drawBars(g, xStart, yStart, false, true, true, true, true, false, true); 
         break;
      case '8': 
         drawBars(g, xStart, yStart, true, true, true, true, true,  true, true); 
         break;
      ......
      }
   }   

You need to fill in lines for the other digits. Debug this method by having paint call it for all the different digits (and different values of xStart, yStart) before going on to the next part.

Method processDigit is to be repeatedly called by the next method you write:

  
   public void processLine(Graphics g, int yStart, String subString)
This method calls processDigit ACROSS times to display one line of digits across the screen, extracting the digits from subString one at a time. The tricky part is making sure xStart, yStart are set correctly each time you call processDigit, moving across the screen from left to right. Notice that yStart is a parameter supplied to processLine specifying the vertical position to write on the screen, but xStart, which specifies the horizontal position, is set to several different values inside processLine. Leave some space on the left before drawing the first digit (you will find out why later). Debug this method by having paint call it before going on to the next part.

Method processLine is to be repeatedly called by the next method you write:

 
     public void processAllLines(Graphics g)

This method calls processLine DOWN times, extracting the relevant substring from the original String variable read by init. Each line (ACROSS digits wide) should appear on a different line in the graphics screen, in a visually appealing way (e.g. no overlaps or giant spaces!). Some space should be left at the bottom (see next paragraph). This method is the one that paint calls in the final version of the program.

Once your program to accomplish all this is working, modify it further to include another method (called by processAllLines) to draw a line under the last integer displayed and write out the sum of all the integers below it, remembering that you need one extra space on the left for a carry digit in the sum. This makes the assignment trickier, because you need to translate the string input into integer format (unless you decide to use integers instead of Strings, as noted above), in order to be able to add up the integers. Method processAllLines needs to do this translation as it extracts each substring from the string. There are several different possible ways to do this. Your program should be clearly organized and well documented with comments. All variables besides the String should be declared only in the methods where they are needed. You can be creative with your use of color, but this is optional.

Summary of the steps to use in writing the program

Your porgram must be well documented with comments.

Don't forget that you can get help from your etutor by email or from me.