Advertisement
advictoriam

Untitled

Mar 4th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. /*
  2.    TODO: Complete this class so that the title is printed
  3.    centered in the rectangle. Hint: Override printLine.
  4. */
  5.  
  6. /**
  7.    A rectangle whose interior is filled with a pattern
  8.    obtained by repeating a character.
  9. */
  10. public class TitledRectangle extends Rectangle
  11. {
  12.    /**
  13.       Constructs a rectangle with a given width, height, and title
  14.       @param aWidth the width (including the corners)
  15.       @param aHeight the height (including the corners)
  16.       @param aTitle the title
  17.    */
  18.    public TitledRectangle(int aWidth, int aHeight, String aTitle)
  19.    {
  20.       super(aWidth, aHeight);
  21.       title = aTitle;
  22.    }
  23.  
  24.    public void print()
  25.    {
  26.       for (int i = 1; i <= super.getHeight(); i++)
  27.       {
  28.          if(i == super.getHeight() / 2 + 1)
  29.          {
  30.             printHelper('|', ' ', title);
  31.             i += 1;
  32.          }
  33.          printLine(i);        
  34.       }
  35.    }
  36.    
  37.    public void printHelper(char boundary, char fill, String middle)
  38.    {
  39.       System.out.print(boundary);
  40.       for (int j = 2; j < super.getWidth(); j++)
  41.          if(j == super.getWidth()/2 - middle.length()/2)
  42.          {
  43.             System.out.print(middle);
  44.             j += middle.length() - 1;
  45.          }
  46.          else
  47.             System.out.print(fill);
  48.       System.out.println(boundary);
  49.    }
  50.  
  51.  
  52.    private String title;
  53.  
  54.    // The following method tests your class
  55.  
  56.    public static void main(String[] args)
  57.    {
  58.       TitledRectangle tr = new TitledRectangle(15, 7, "Hello");
  59.       tr.print();
  60.    }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement