CSCI-UA.0101 Programming Project 2 Fall 2012
Digital Displays

Due: Marateck: TUE NOV 6 at 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 uisng the Scanner class, 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 and hit RETURN, you may have to resize the window to see all the digits displayed. The following is a guide that you may wish to follow.

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

The work really begins with writing the method

 public void draw(Graphics
g, 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. If you wish you may debug this method by having paint call it directly with various input values before going on to the next part.

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

     public void drawBar(Graphics g, int digit)
     {  switch (digit) 
        {  case 3: draw(g, xStart, yStart, false, true, true, true, true, false, true); break;
           case 8: draw(g, xStart, yStart, true, true, true, true, true, true, true); break;
You need to fill in lines for the other digits. You may wish to debug this method by having paint call it for all the different digits before going on to the next part.

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

     public void processLine(Graphics g, String subString)
This method calls drawBar 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 x and y are set correctly each time you call drawBar, moving across the screen from left to right. Notice that x and y are global and that y specifies the vertical position to write on the screen, but x, 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). You may wish to 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. You can keep track of the sum by using one more instance or static variable, or pass it to procedure finalSum as a parameter. Your program should be clearly organized and well documented with comments. You can be creative with your use of color, but this is optional.

Summary of the steps to use in writing the program

You may or may not want to test the intermediate steps.

Don't forget that you can get help from your etutor by email or from the Lab Tutors,; see the main web page for their office hours.