Object-Oriented Programming with Java, part II – Exercise 1

Create the support method private static void printWithSmileys(String characterString) for the class Smileys which comes with the assignment template. The method is to print the given character string surrounded with smileys. Use the character string :) as the smiley.

My Code

Smileys

public class Smileys {

    public static void main(String[] args) {
        // Test your method at least with the following
        printWithSmileys("Method");
        printWithSmileys("Beerbottle");
        printWithSmileys("Interface");
        printWithSmileys("\\:D/");
        printWithSmileys("87.");

    }

    private static void printWithSmileys(String characterString) {
        int length = characterString.length() / 2;
        int i = 0;

        if (characterString.length() % 2 == 1) {
            length++;
        }
        while (i < length + 3) {
            System.out.print(":)");
            i++;
        }
        System.out.println("");
        if (characterString.length() % 2 == 0) {
            System.out.print(":) ");
            System.out.print(characterString);
            System.out.println(" :)");
        } else {
            System.out.print(":) ");
            System.out.print(characterString);
            System.out.println("  :)");
        }

        i = 0;
        while (i < length + 3) {
            System.out.print(":)");
            i++;
        }
        System.out.println("");

    }

}

Model Code

public class Smileys {

 

    public static void main(String[] args) {

        // Test your method at least with the following

        printWithSmileys("Method");

        printWithSmileys("Beerbottle");

        printWithSmileys("Interface");

    }

 

    private static void printWithSmileys(String characterString) {

        String middleString = createTheStringInTheMiddle(characterString);

        int number = middleString.length() / 2 + 2;

 

        printSmileys(number);

        System.out.println(":)" + middleString + ":)");

        printSmileys(number);

    }

 

    private static String createTheStringInTheMiddle(String characterString) {

        String returnCharacterString = " " + characterString + " ";

        if (characterString.length() % 2 == 1) {

            return returnCharacterString + " ";

        }

 

        return returnCharacterString;

    }

 

    private static void printSmileys(int number) {

        for (int i = 0; i < number; i++) {

            System.out.print(":)");

        }

 

        System.out.println("");

    }

}

Comments

OK, well that is a bit of a stuff up. For starters I have had to return to an older version of the course as there appeared to be a mistake in the newer version and I couldn’t get an answer on it.

Apart from that I didn’t refactor my code. I thought about it but I was a bit pressed for time so my code is a hell of a lot messier than the model code….so actually I am going backwards at the moment. 🙂

Leave a comment