Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- TODO: Complete this class so that the title is printed
- centered in the rectangle. Hint: Override printLine.
- */
- /**
- A rectangle whose interior is filled with a pattern
- obtained by repeating a character.
- */
- public class TitledRectangle extends Rectangle
- {
- /**
- Constructs a rectangle with a given width, height, and title
- @param aWidth the width (including the corners)
- @param aHeight the height (including the corners)
- @param aTitle the title
- */
- public TitledRectangle(int aWidth, int aHeight, String aTitle)
- {
- super(aWidth, aHeight);
- title = aTitle;
- }
- public void print()
- {
- for (int i = 1; i <= super.getHeight(); i++)
- {
- if(i == super.getHeight() / 2 + 1)
- {
- printHelper('|', ' ', title);
- i += 1;
- }
- printLine(i);
- }
- }
- public void printHelper(char boundary, char fill, String middle)
- {
- System.out.print(boundary);
- for (int j = 2; j < super.getWidth(); j++)
- if(j == super.getWidth()/2 - middle.length()/2)
- {
- System.out.print(middle);
- j += middle.length() - 1;
- }
- else
- System.out.print(fill);
- System.out.println(boundary);
- }
- private String title;
- // The following method tests your class
- public static void main(String[] args)
- {
- TitledRectangle tr = new TitledRectangle(15, 7, "Hello");
- tr.print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement