public class Utilities
2: /**
3: * A class for some methods that print things.
4: *
5: * @author Mark Young (A00000000)
6: */
7: public class Utilities {
9: /**
10: * Print a title.
11: * <p>
12: * The title is printed underlined with hyphens,
13: * and with blank lines before and after.
14: * The capitalization of the title is not changed for printing.
15: *
16: * @param title the title to be printed
17: */
18: public static void printTitle(String title) {
19: // print a blank line
20: System.out.println();
22: // print the title
23: System.out.println(title);
25: // underline the title
26: for (int i = 1; i <= title.length(); ++i) {
27: System.out.print("-");
28: }
29: System.out.println();
31: // print a blank line
32: System.out.println();
33: }
34:
35: /** Maximum length of a line printed by printParagraph */
36: public static final int MAX_LINE_LENGTH = 76;
38: /**
39: * Print a paragraph, wrapped to MAX_LINE_LENGTH characters per line.
40: * <p>
41: * The paragraph has a blank line printed after it.
42: * If the text contains a word of over MAX_LINE_LENGTH characters,
43: * that word will overflow the right edge of the paragraph.
44: * (This method does not hyphenate words.)
45: * <p>
46: * The method assumes that the line it starts on was empty.
47: * If it is not empty,
48: * the first line of the paragraph may overflow the right margin.
49: *
50: * @param text the text of the paragraph to be printed.
51: */
52: public static void printParagraph(String text) {
53: // create variable for how many characters are on the current line
54: int usedSoFar = 0;
56: // break string into words
57: String[] words = text.split(" ");
59: // for each word
60: for (int i = 0; i < words.length; ++i) {
61: // need to print every character of the word, plus a space
62: int spaceNeeded = words[i].length() + 1;
63:
64: // if there's not enuf space for this word
65: if (usedSoFar + spaceNeeded > MAX_LINE_LENGTH) {
66: // end the line, giving us a new, blank line
67: System.out.println();
68: usedSoFar = 0;
69: }
70:
71: // print the word; update the number of characters on this line
72: System.out.print(words[i] + " ");
73: usedSoFar += spaceNeeded;
74: }
75: // end the last line of the paragraph
76: System.out.println();
77:
78: // print a blank line
79: System.out.println();
80: }
81: }