Ghosts floating in the house

  • These are imperative, not objects
    • This program does have classes and methods, indicating OOP format. However, the ghosts are not really objects as they do not have attributes, therefore the actual code is mostly imperative.
  • You can also access a 2D array by simply printing everything in each 1D part of the object, similar to this program
  • An example of turning each ghost into an object is by making them have attributes like 'ghost1.head', 'ghost1.body' and more to make it more straightforward.
class GhostLoop {
    // The area between class definition and the 1st method is where we keep data for object in Java
    String [][] ghosts;    

    /**
     * Constructor initializes a 2D array of s
     */
    public GhostLoop() {
        //Storing Data in 2D arrays
        ghosts = new String[][]{   
                //Ghost 0
                {
                        " .-.      ",      //[n][0] 
                        "(o o) boo!",      //[n][1] 
                        "| O \\     ",       //[n][2] 
                        " \\   \\    ",        //[n][3] 
                        "  `~~~'   " //[n][4]
                },
                //Ghost 1
                {
                    " .-.      ",      //[n][0] 
                    "(o o) hoo!",      //[n][1] 
                    "|---\\     ",       //[n][2] 
                    " \\   \\    ",        //[n][3] 
                    "  `~~~'   " //[n][4]
                },
                //Ghost 2
                {
                    " .-.      ",      //[n][0] 
                    "(o o) goo!",      //[n][1] 
                    "|⼝ \\     ",       //[n][2] 
                    " \\   \\    ",        //[n][3] 
                    "  `~~~'   " //[n][4]
                },
                //Ghost 3
                {
                    " .-.      ",      //[n][0] 
                    "(o o) moo!",      //[n][1] 
                    "|<->\\     ",       //[n][2] 
                    " \\   \\    ",        //[n][3] 
                    "  `~~~'   " //[n][4]
                },
                //Ghost 4
                {
                    " .-.      ",      //[n][0] 
                    "(o o) foo!",      //[n][1] 
                    "|'' \\     ",       //[n][2] 
                    " \\   \\    ",        //[n][3] 
                    "  `~~~'   " //[n][4]
                },

        };
    }

    /**
     * Loop and print s in array
     * ... repeat until you reach zero  ...
     */
    public void printPoem() {
        //begin the poem
        System.out.println();
        System.out.println("Ghost Floating Poem in Java Loopy");
        System.out.println("---------------------------------");

        // ghosts (non-primitive) defined in constructor knows its length
        int ghostCount = ghosts.length;
        for (int i = ghostCount; i >= 1; i--)  //loops through 2D array length backwards
        {

            //this print statement shows current count of ghosts
            //  concatenation (+) of the loop variable and string to form a countdown message
            System.out.println(i + " little ghosts floating in the house...");

            //how many separate parts are there in a ghost ghost?
            int partCount = ghosts[0].length;
            for (int row = 0; row < partCount; row++) {  //cycles through "cells" of 2d array

                /*cycles through columns to print
                each ghost part by part, will eventually print entire column*/
                for (int col = 0; col < ghostCount; col++) {

                    // prints specific part of the ghost from the column
                    System.out.print(ghosts[col][row] + "    ");

                    //this is new line between separate parts
                }

                //this new line gives separation between stanza of poem
                System.out.println();
            }

            //countdown for poem, decrementing ghostCount variable by 1
            System.out.println("One got caught and lost his stead.");
            ghostCount -= 1;
        }

        //out of all the loops, prints finishing messages
        System.out.println("No more ghosts floating in the house");
        System.out.println("------------------------------------");
        System.out.println("              THE END               ");
    }

    /**
    * A Java Driver/Test method that is the entry point for execution
    */
    public static void main(String[] args)  {
        new GhostLoop().printPoem();   //a new ghost list and output in one step
    }

}
GhostLoop.main(null);
Ghost Floating Poem in Java Loopy
---------------------------------
5 little ghosts floating in the house...
 .-.           .-.           .-.           .-.           .-.          
(o o) boo!    (o o) hoo!    (o o) goo!    (o o) moo!    (o o) foo!    
| O \         |---\         |⼝ \         |<->\         |'' \         
 \   \         \   \         \   \         \   \         \   \        
  `~~~'         `~~~'         `~~~'         `~~~'         `~~~'       
One got caught and lost his stead.
4 little ghosts floating in the house...
 .-.           .-.           .-.           .-.          
(o o) boo!    (o o) hoo!    (o o) goo!    (o o) moo!    
| O \         |---\         |⼝ \         |<->\         
 \   \         \   \         \   \         \   \        
  `~~~'         `~~~'         `~~~'         `~~~'       
One got caught and lost his stead.
3 little ghosts floating in the house...
 .-.           .-.           .-.          
(o o) boo!    (o o) hoo!    (o o) goo!    
| O \         |---\         |⼝ \         
 \   \         \   \         \   \        
  `~~~'         `~~~'         `~~~'       
One got caught and lost his stead.
2 little ghosts floating in the house...
 .-.           .-.          
(o o) boo!    (o o) hoo!    
| O \         |---\         
 \   \         \   \        
  `~~~'         `~~~'       
One got caught and lost his stead.
1 little ghosts floating in the house...
 .-.          
(o o) boo!    
| O \         
 \   \        
  `~~~'       
One got caught and lost his stead.
No more ghosts floating in the house
------------------------------------
              THE END