Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Homework

Name: Anonymous 2009-03-18 18:48

Print "SUSSMAN" 300 times (each on a new line) to the output device using Java.

Name: Anonymous 2009-03-18 19:56

import java.util.ArrayList;

public class SussmanPrinter {
    final static int ARGUMENT_ERROR               = 1;
    final static int SUCCESSFULLY_PRINTED_SUSSMAN = 2;
    final static int SUCCESSFULLY_PRINTED         = 4;
    final static int UNSUCCESSFULLY_PRINTED       = 8;
    final static int ARRAYLIST_SUSSMAN_ERROR      = 16;
    final static int SUSSMAN_MUTATION_ERROR       = 32;
    final static int SUSSMAN_ESCAPE_ERROR         = 64;

    final static int NUMBER_OF_TIMES_DEFAULT = 300;
    final static String sussman = new String("Sussman");

    private static int printSussman(int numberOfTimes) {
        ArrayList<String> sussmans = new ArrayList<String>(numberOfTimes);

        for (int i = 0; i < numberOfTimes; i++) {
            sussmans.add(new String(sussman));
        }

        if (sussmans.size() != numberOfTimes) {
            System.err.println("Error! Something went wrong!");
            return ARRAYLIST_SUSSMAN_ERROR;
       
        } else {
            int counter = 0;

            for (String sussmanToBePrinted: sussmans) {
                if (sussmanToBePrinted.equals(new String(sussman))) {
                    System.out.println(new String(sussmanToBePrinted));
                    counter += 1;

                } else {
                    System.err.println("Error! Something went wrong!");
                    return SUSSMAN_MUTATION_ERROR;

                }
            }

            if (counter == numberOfTimes) {
                Integer counterInteger = new Integer(counter);
                System.out.println("We have successfully printed our " +
                                   counterInteger.toString() + " Sussmans.");
                return SUCCESSFULLY_PRINTED_SUSSMAN;

            } else {
                System.out.println("Error! Something went wrong!");
                return SUSSMAN_ESCAPE_ERROR;

            }

        }

    }

    public static void main(String[] args) {
        int numberOfTimes = NUMBER_OF_TIMES_DEFAULT;
        if (args.length > 1) {
            try {
                numberOfTimes = Integer.parseInt(args[1]);
            } catch (NumberFormatException nfe) {
                System.err.println("Error! You must provide a valid number " +
                                   "of times to print ``Sussman'' as the " +
                                   "argument, or none for three hundred!");
                System.exit(ARGUMENT_ERROR);
            }

        } else {
            numberOfTimes = NUMBER_OF_TIMES_DEFAULT;

        }

        int sussmanPrinterStatus = printSussman(numberOfTimes);

        if (sussmanPrinterStatus == SUCCESSFULLY_PRINTED_SUSSMAN) {
            System.out.println("Your Sussman has been printed successfully!");
            System.exit(SUCCESSFULLY_PRINTED);

        } else {
            System.err.println("There was an error!");
            System.exit(UNSUCCESSFULLY_PRINTED);

        }

    }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List