Skip to content

Instantly share code, notes, and snippets.

@easyaspi314
Last active February 9, 2019 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save easyaspi314/0645e779ae2dca603cb3541d5a1ee79d to your computer and use it in GitHub Desktop.
Save easyaspi314/0645e779ae2dca603cb3541d5a1ee79d to your computer and use it in GitHub Desktop.
Java assignment to print a 5x5 square
/* filename: OvercomplicatedWayToPrintA5x5Square.java */
public class OvercomplicatedWayToPrintA5x5Square {
/*
* An abstract class representing a shape that can be printed to the console.
*/
private static abstract class Shape {
/*
* Prints the contents of the shape to the console.
*/
public abstract void printToConsole();
} /* end class Shape */
/*
* class Square
* Represents a square that can be printed to the console.
*
* Extends Shape.
*/
private static class Square extends Shape {
private int mSize;
/*
* printHorizontalSide is an entirely unnecessary function that prints <size>
* space-separated asterisks to the console.
*
* This is a multipurpose function, it can either be used to print the top
* of a square, or the bottom.
*/
private void printHorizontalSide() {
/* Start the row of the asterisks with a single asterisk. */
StringBuilder sb = new StringBuilder("*");
/* Add size - 1 asterisks, space separated. */
for (int i = 1; i < mSize; i++)
sb.append(" *");
/*
* Print the contents of the StringBuilder, showing the contents of the
* horizontal side.
*/
System.out.println(sb.toString());
} /* end printRow method */
/*
* printVerticalSides is an entirely unnecessary function that prints
* size - 2 rows of two asterisks, separated by 2 * (size - 1) + 1 spaces,
* to the console.
*/
private void printVerticalSides() {
StringBuilder sb = new StringBuilder("*");
/*
* Append two spaces to align the asterisks to the length of printHorizontalSide's
* output.
*/
for (int i = 1; i < mSize - 1; i++)
sb.append(" ");
/* Append the last asterisk. */
sb.append(" *");
/* Convert the StringBuilder to a String. */
String s = sb.toString();
/* Print out size - 2 rows of asterisks. */
for (int i = 1; i < mSize - 1; i++)
System.out.println(s);
} /* end printColumn method */
/*
* Prints the contents of the square to the console.
*/
@Override
public void printToConsole() {
/* First, we must print the top row of the square. */
printHorizontalSide();
/* Print the sides of the square. */
printVerticalSides();
/* Finally, print the bottom side of the square. */
printHorizontalSide();
} /* end printToConsole method */
/*
* This constructor will create a Square of asterisks with a side
* length of size, which can be printed to the console.
*
* Size must be equal to 5.
*/
public Square(int size) throws Square.DimensionException {
if (size != 5)
throw new Square.DimensionException(size);
mSize = size;
}
public static class DimensionException extends Exception {
DimensionException(int size) {
super("Square dimension must be equal to 5, got unexpected value "
+ size + ".");
} /* end Square.DimensionException constructor */
} /* end class Square.DimensionException */
} /* end class Square */
/*
* main is the first function called on starting the program.
* This program will print a 5x5 square to the console.
*/
public static void main(String args[]) {
try {
/* Print a square to the console. */
Square square = new Square(5);
square.printToConsole();
/* printSquare may throw a Square.DimensionException */
} catch (Square.DimensionException e) {
System.err.println("Failed to create a square!");
System.err.println(e.getMessage());
}
}/* end main method */
}/* end class OvercomplicatedWayToPrintA5x5Square */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment