iteration

  • This is more of an imperative programming style, as the cars aren't objects. They don't have attributes.
  • to access a 2D array, you can print out the stuff by a single dimension, ie: printing out rows and columns seperately
class Main {
  // The area between class definition and the 1st method is where we keep data for object in Java
  String [][] cars;    

  public Main() {
      //Storing Data in 2D arrays
      cars = new String[][]{   
              //car 0
              
                {
                    "     ______  vroom             ", //[0][0]
                    "    /[_][_|`.__                ",
                    "   (   _    _ _|               ",
                    " =`-(_)--(_)-                  ",
                    "==============================="    
                },
                //car 1    
                {
                    "    -_-  _/|______||__  mmmmm  ",
                    " -_-__  / ,-. -|-  ,-.`-.      ",
                    "   _-_- `( o )----( o )-'      ",
                    "          `-'      `-'         ",
                    "==============================="   
                },
                //car 2
                {
                    "      ____    vroom vroom  ",
                    "   __/  |_|_               ",
                    " |  _     _``-.            ",
                    " '-(_)---(_)--'            ",  
                    "===========================" 
                },
                //car 3
                {
                    "       __    car               ",
                    "  _.-.___|__                   ",
                    " |  _      _`-.                ",  
                    " '-(_)----(_)--`               ",
                    "===============================" 
                },

      };
  }


  public void printPoem() {
      //starts poem
      System.out.println();
      System.out.println("Car crash poem");
      System.out.println("---------------------------------");

      // carCount is the number of elements in the carcount array
      int carCount = cars.length;
      for (int i = carCount; i >= 1; i--)  //loop through array, decreasing the amount of cars by one each time it loops
      {

            
          System.out.println(i + " car(s) driving on the road...");


          int bz = cars[0].length;
          for (int row = 0; row < bz; row++) {  //cycles through "cells" of 2d array

                //prints column
              for (int col = 0; col < carCount; col++) {

   
                  System.out.print(cars[col][row] + "");

              }

              System.out.println();
          }

        // decrease car count by 1
          System.out.println("One car crashed and skidded off the road.");
          carCount -= 1;
      }


      System.out.println("No more cars driving on the road");
      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 Main().printPoem();   //a new ghost list and output in one step
  }

}
Main.main(null);
Car crash poem
---------------------------------
4 car(s) driving on the road...
     ______  vroom                 -_-  _/|______||__  mmmmm        ____    vroom vroom         __    car               
    /[_][_|`.__                 -_-__  / ,-. -|-  ,-.`-.         __/  |_|_                 _.-.___|__                   
   (   _    _ _|                  _-_- `( o )----( o )-'       |  _     _``-.             |  _      _`-.                
 =`-(_)--(_)-                            `-'      `-'          '-(_)---(_)--'             '-(_)----(_)--`               
========================================================================================================================
One car crashed and skidded off the road.
3 car(s) driving on the road...
     ______  vroom                 -_-  _/|______||__  mmmmm        ____    vroom vroom  
    /[_][_|`.__                 -_-__  / ,-. -|-  ,-.`-.         __/  |_|_               
   (   _    _ _|                  _-_- `( o )----( o )-'       |  _     _``-.            
 =`-(_)--(_)-                            `-'      `-'          '-(_)---(_)--'            
=========================================================================================
One car crashed and skidded off the road.
2 car(s) driving on the road...
     ______  vroom                 -_-  _/|______||__  mmmmm  
    /[_][_|`.__                 -_-__  / ,-. -|-  ,-.`-.      
   (   _    _ _|                  _-_- `( o )----( o )-'      
 =`-(_)--(_)-                            `-'      `-'         
==============================================================
One car crashed and skidded off the road.
1 car(s) driving on the road...
     ______  vroom             
    /[_][_|`.__                
   (   _    _ _|               
 =`-(_)--(_)-                  
===============================
One car crashed and skidded off the road.
No more cars driving on the road
------------------------------------
              THE END