Source of Utilities.java


  2: import java.util.Scanner;

  4: /**
  5:  * A class for some methods that print things.
  6:  * <p>
  7:  * Version 1.1 uses a Scanner to break the paragraph into words.
  8:  * Version 1.0 used an array.
  9:  *
 10:  * @author Mark Young (A00000000)
 11:  * @version 1.1 (2015-10-15)
 12:  */
 13: public class Utilities {

 15:     /** Print the assignment information for A07. */
 16:     public static void printA07Information() {
 17:         System.out.println();
 18:         System.out.println("A00000000 -- Young, Mark");
 19:         System.out.println("CSCI 1226 -- Fall 2015");
 20:         System.out.println("A07 -- due 2015-11-20");
 21:         System.out.println();
 22:     }

 24:     /**
 25:      * Print the assignment information for a general assignment.
 26:      *
 27:      * @param   asgnNo  number of the assignment
 28:      * @param   dueDate due date for the assignment
 29:      */
 30:     public static void printAssignmentInformation(int asgnNo, String dueDate) {
 31:         System.out.println();
 32:         System.out.println("A00000000 -- Young, Mark");
 33:         System.out.println("CSCI 1226 -- Fall 2015");
 34:         System.out.printf("A%02d -- due %s\n", asgnNo, dueDate);
 35:         System.out.println();
 36:     }

 38:     /**
 39:      * Print a title.
 40:      * <p>
 41:      * The title is printed underlined with hyphens,
 42:      * and with blank lines before and after.
 43:      * The capitalization of the title is not changed for printing.
 44:      *
 45:      * @param   title   the title to be printed
 46:      */
 47:     public static void printTitle(String title) {
 48:         // print a blank line
 49:         System.out.println();

 51:         // print the title
 52:         System.out.println(title);

 54:         // underline the title
 55:         for (int i = 1; i <= title.length(); ++i) {
 56:             System.out.print("-");
 57:         }
 58:         System.out.println();

 60:         // print a blank line
 61:         System.out.println();
 62:     }

 64:     /** Maximum length of a line printed by printParagraph */
 65:     public static final int MAX_LINE_LENGTH = 80;

 67:     /**
 68:      * Print a paragraph, wrapped to MAX_LINE_LENGTH characters per line.
 69:      * <p>
 70:      * The paragraph has a blank line printed after it.
 71:      * If the text contains a word of over MAX_LINE_LENGTH characters,
 72:      * that word will overflow the right edge of the paragraph.
 73:      * (This method does not hyphenate words.)
 74:      * <p>
 75:      * The method assumes that the line it starts on was empty.
 76:      * If it is not empty,
 77:      * the first line of the paragraph may overflow the right margin.
 78:      *
 79:      * @param   text    the text of the paragraph to be printed.
 80:      */
 81:     public static void printParagraph(String text) {
 82:         // create variable for how many characters are on the current line
 83:         int spacesUsedSoFar = 0;

 85:         // use a Scanner to break the text into words
 86:         Scanner para = new Scanner(text);
 87:         //   compare new Scanner(text) to new Scanner(System.in)
 88:         //   It's reading from the String instead of from System.in

 90:         // while there are more words in the paragraph
 91:         while (para.hasNext()) {
 92:             //   Google Java Scanner to read about the hasNext method

 94:             // need to print every character of the word, plus a space
 95:             String word = para.next();
 96:             int spacesNeeded = word.length() + 1;

 98:             // if there's not enuf space for this word
 99:             if (spacesUsedSoFar + spacesNeeded > MAX_LINE_LENGTH) {
100:                 // end the line, giving us a new, blank line
101:                 System.out.println();
102:                 spacesUsedSoFar = 0;
103:             }

105:             // print the word; update the number of characters on this line
106:             System.out.print(word + " ");
107:             spacesUsedSoFar += spacesNeeded;
108:         }
109:         // end the last line of the paragraph
110:         System.out.println();

112:         // print a blank line
113:         System.out.println();
114:     }
115: }