/home/wpollock1/public_html/restricted/Java1/TextKit Model Solution/utils/TextKit.java
// TextKit.java - a utility class for text methods.
package utils;
/** TextKit is a collection of utility text methods.
*
* <p>This class is part of the package "utils". Currently it contains
* a method called "lineOfStars" which returns a line of asterisks as
* a String, a method "pad" which formats number by adding leading
* space ("pad" is no longer useful since Java5; Formatter class and
* String.printf methods do the job), and a method "numberToOrdinal"
* that puts the correct ordinal suffix on a ordinal value (e.g., "2"
* becomes "2nd").
* <p>
* This class is missing many features that would make it "production
* quality", including proper use of "final" and other modifiers, use
* of assertions and proper unit tests, and proper class (and package)
* documentation. Care should be taken to make the methods
* "thread safe" and they should be marked as such with "annotations".
*
* @author ©2004-2015 by Wayne Pollock, Tampa Florida USA
* @version 2.3
*/
public class TextKit
{
/**
* Creates a line of asterisks of a given length.
*
* @param numOfStars The number of asterisks to be drawn.
* @return A {@link java.lang.String String} of asterisks.
* @throws IllegalArgumentException if {@code numOfStars < 0}.
*/
public static String lineOfStars ( int numOfStars )
throws IllegalArgumentException
{
if ( numOfStars < 0 )
throw new IllegalArgumentException( "Number of stars to draw is "
+ numOfStars
+ ", and should be greater than or equal to zero." );
StringBuilder stars = new StringBuilder( numOfStars );
for ( int i = 0; i < numOfStars; ++i )
stars.append( "*" );
return stars.toString();
/* Here's an alternative, not as efficient but one line shorter:
char[] stars = new char[ numOfStars ];
Arrays.fill( stars, '*' );
return new String( stars );
Or this:
return String.format("%" + numOfStars + "s", "").replace(' ', '*');
Or (since Java 11):
return "*".repeat(numStars);
*/
}
/**
* Formats an integer {@code num} with leading space,
* so the resulting {@link java.lang.String String} has at least
* {@code minWidth} length.
*
* @param num The number to be formatted with leading spaces.
* @param minWidth The minimum length of the resulting
* {@link java.lang.String String}.
* @return The formatted {@code num} with leading spaces.
* @throws IllegalArgumentException if {@code minWidth < 0}.
*/
public static String pad ( int num, int minWidth )
throws IllegalArgumentException
{
if ( minWidth < 0 )
throw new IllegalArgumentException( "Minimum width is "
+ minWidth + ", and should be greater than or equal to zero." );
StringBuilder result = new StringBuilder();
result.append( num );
while ( result.length() < minWidth )
result.insert( 0, ' ' );
return result.toString();
/* Here's the Java 5 alternative:
return String.format( "%" + minWidth + "d", num );
*/
}
/**
* Adds the correct suffix string to some ordinal number and
* returns the ordinal as a String.
* For example "1" becomes "1st", 2 becomes "2nd", etc.
* (Adapted from an idea by Roedy Green, posted 1/31/08
* in the comp.lang.java.programmer netnews group.)
* Note this routine applies for the English language only.
* @since 2.0
* @param num The number to which you want to append the
* ordinal suffix.
* @return The corresponding ordinal, i.e. {@code num} plus "st",
* "nd", "rd", or "th".
* @throws IllegalArgumentException if {@code num < 0}.
*/
public static String numberToOrdinal ( int num )
throws IllegalArgumentException
{
if ( num < 0 ) // Ordinal numbers are never negative
throw new IllegalArgumentException( "argument \""
+ num + "\" should be greater than or equal to zero." );
final int lastDigit = num % 10;
final int last2digits = num % 100;
String suffix;
switch ( lastDigit )
{
case 1: suffix = (last2digits == 11 ? "th" : "st");
break;
case 2: suffix = (last2digits == 12 ? "th" : "nd");
break;
case 3: suffix = (last2digits == 13 ? "th" : "rd");
break;
default: suffix = "th";
}
return num + suffix;
}
/** This prevents one from creating objects of this class.
*/
private TextKit () {}
}