Exercise 5 Solution

 
Code Diagram
import java.awt.*;
import java.applet.Applet;

public class NameSq extends Applet {

   //declaring instance variables
   public int topOffset, leftOffset;
   public int xPosition, yPosition, lineSpacing;
   public String firstName, lastName;
   
   public void paint(Graphics g) {

      //initializing instance variables

      //these two variables control the
      //position of the rectangle.
      xPosition = 25;
      yPosition = 25;
      lineSpacing = 15;

      //these two variables control the 
      //position of the text within the rectangle
      topOffset = 20;
      leftOffset = 20;

      firstName = "Ben";
      lastName = "McCrea";

      g.setColor(Color.green);
      g.fillRect(xPosition, yPosition, 100, 100);
      g.setColor(Color.black);
      g.drawString(firstName, xPosition + leftOffset, 
                   yPosition+topOffset);
      g.drawString(lastName, xPosition + leftOffset, 
                   yPosition + topOffset + lineSpacing);
    }

 }