V22.0101 Programming Assignment 2 Fall 2004
Digital Displays

Due: Marateck: WED, Oct 13, 11:59 pm

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. After you type your 15-digit number in the dialog box and press Enter, you will have to resize the window to see all the digits displayed.

Start by creating the bare bones of your program, namely:

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" (use the switch statement). 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 25 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.

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, int 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 to display one line of five 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 three times, extracting the relevant five-digit substring from the original String variable read by init. Each five-digit number 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 (also called by paint) 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, 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, that will be discussed in class. You can keep track of the sum by using one more instance or static variable, declared next to the String variable. Your program should be clearly organized and well documented with comments. All variables besides the String and sum variable 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

Don't forget that you can get help from your etutor by email or from the TA Anna Zaks at 14 Washington Place; see the main web page for his office hours.