Logo Example: Elements of Programming Style

Tom Kelliher, CS 116

Sept. 15, 2000

Administrivia

Announcements

Assignment

Read Section 2.4 and the Lab 2 handout.

From Last Time

Methods and graphics programming.

Outline

  1. Variable declaration.

  2. Elements of programming style.

  3. Logo applet, first and second tries.

Coming Up

Lab 2.

Variable Declaration

  1. Two basic forms:
    typeName identifier;
    
    typeName identifier = value;
    

  2. Examples:
    int x;
    int x = 10;
    Color Color1, Color2;
    Rectangle rect1 = new Rectangle(200, 20, 40, 40),
              rect2 = new Rectangle(10, 10, 20, 60);
    

Elements of Programming Style

  1. Documentation conventions:
    1. Project documentation:
      1. Project: Course, brief summary.

      2. Author.

      3. File: name of current file.

      4. Purpose: More detailed description of the applet.

    2. Class documentation:
      1. Class: name of the class.

      2. Purpose: Description of the class' function.

    3. Method documentation:
      1. Method: name of the method.

      2. Purpose: Description of the method's function.

      3. Input: (only if the method takes parameters) description of the method's parameters.

      4. Output: (only if the method is non-void) description of the method's return value.

    See class handouts for examples.

  2. Minimize your use of constants. Use variables instead.

    Example:

    circumference = 2 * 3.14159 * radius;   // Avoid.
    
    
    float PI = 3.14159;
    
    circumference = 2 * PI * radius;   // Preferable.
    
    Why are constants bad? (Maintenance, readability).

  3. Identifier creation conventions:
    1. Class names: Add, SodaMachine.

    2. Method names: add(), accumulateSum().

    3. Variable names: sum, myColor.

    4. Named constants: PI, BASE_OFFSET.

  4. Increasing readability:
    1. Use of whitespace.

    2. Consistent indentation.

  5. Example of a poorly written program:
                         import java.applet.Applet;import java.awt.Graphi
    cs;import java.awt.Color;
          public class Logo1 extends Applet{
               public void init()
                  {setBackground(Color.black);}
          public void paint(Graphics g){
              g.setColor(Color.red);
              g.drawRect(50,80,100,100);
              g.setColor(Color.white);
          g.drawRect(75,105,150,150);
          g.drawRect(76,106,148,148);
                  g.setColor(Color.blue);
                  g.drawRect(100,130,200,200);
                  g.drawRect(101,131,198,198);
            g.drawRect(102,132,196,196);}}
    

Logo Applet, First Try

//======================================================================
//      Project:    CS 116, Logo1 applet demo
//      Author:     Tom Kelliher
//      File:       Logo1.java
//      Purpose:    First version of the Logo demo.  This applet
//                  displays a simple logo consisting of three squares
//                  of different colors.  Everything is "hard-coded" in
//                  this version of the applet.
//======================================================================

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

//======================================================================
//      Class:      Logo1
//      Purpose:    Display a simple logo, as described above.
//======================================================================

public class Logo1 extends Applet
{

   //===================================================================
   //   Method:     init
   //   Purpose:    Set background color.
   //===================================================================

   public void init()
   {
      setBackground(Color.black);
   }

   //===================================================================
   //   Method:     paint
   //   Purpose:    Display the logo
   //   Input:      A graphics object.
   //===================================================================

   public void paint(Graphics g)
   {
      // Render the first square.
      g.setColor(Color.red);
      g.drawRect(50, 80, 100, 100);

      // Render the second square.
      g.setColor(Color.white);
      g.drawRect(75, 105, 150, 150);
      g.drawRect(76, 106, 148, 148);

      // Render the third square.
      g.setColor(Color.blue);
      g.drawRect(100, 130, 200, 200);
      g.drawRect(101, 131, 198, 198);
      g.drawRect(102, 132, 196, 196);
   }
}

Logo Applet, Second Try

//======================================================================
//      Project:    CS 116, Logo2 applet demo
//      Author:     Tom Kelliher
//      File:       Logo2.java
//      Purpose:    Second version of the Logo demo.  This applet
//                  displays a simple logo consisting of three squares
//                  of different colors.  The code in the paint() method
//                  has been re-written to ease modification.
//======================================================================

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

//======================================================================
//      Class:      Logo2
//      Purpose:    Display a simple logo, as described above.
//======================================================================

public class Logo2 extends Applet
{

   //===================================================================
   //   Method:     init
   //   Purpose:    Set background color.
   //===================================================================

   public void init()
   {
      Color BACKGROUND_COLOR = Color.black;

      setBackground(BACKGROUND_COLOR);
   }

   //===================================================================
   //   Method:     paint
   //   Purpose:    Display the logo
   //   Input:      A graphics object.
   //===================================================================

   public void paint(Graphics g)
   {
      int x = 50;           // X coordinate of first square.
      int y = 80;           // Y coordinate of first square.
      int size = 100;       // Size of first square.
      int OFFSET = 25;      // Coordinate offset between squares.
      int INCREMENT = 50;   // Size increment between squares.

      Color COLOR1 = Color.red;     // Color of first square.
      Color COLOR2 = Color.white;   // Color of second square.
      Color COLOR3 = Color.blue;    // Color of third square.
      
      // Render the first square.
      g.setColor(COLOR1);
      g.drawRect(x, y, size, size);

      // Render the second square.
      g.setColor(COLOR2);
      // Update coordinates and size for second square.
      x = x + OFFSET;
      y = y + OFFSET;
      size = size + INCREMENT;
      // Draw square with a line width of two.
      g.drawRect(x, y, size, size);
      g.drawRect(x + 1, y + 1, size - 2, size - 2);

      // Render the third square.
      g.setColor(COLOR3);
      // Update.
      x = x + OFFSET;
      y = y + OFFSET;
      size = size + INCREMENT;
      // Draw square with a line width of three.
      g.drawRect(x, y, size, size);
      g.drawRect(x + 1, y + 1, size - 2, size - 2);
      g.drawRect(x + 2, y + 2, size - 4, size - 4);
   }
}



Thomas P. Kelliher
Wed Sep 13 09:45:36 EDT 2000
Tom Kelliher