Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSE 142, Spring 2015 Programming Assignment #2: ASCII Art (32 points) Due Tuesda

ID: 3688176 • Letter: C

Question

CSE 142, Spring 2015 Programming Assignment #2: ASCII Art (32 points) Due Tuesday, April 14, 2015, 11:00 PM This assignment focuses on for loops, expressions, variables, and constants. Turn in two Java files as described below. Part A: ASCIl Art (4 points): The first part of your assignment is to write a program that produces any text art (sometimes called "ASCII art") picture like. Write a Java class named AsciiArt in a file named AsciArt.java. We will share everyone's art on the you class website. Your program can produce any text picture you like, with the following restrictions and details: The picture should be your own creation, not an ASCII image you found on the Internet or elsewhere. · The number of lines drawn should be at least 3 but no more than 200, and no more than 100 characters/ line. . The picture should not include hateful, offensive, or otherwise inappropriate images. · The code should use at least one for loop or static method, but should not contain infinite loops. · The picture must not be identical to your solution for Part B or consist entirely of reused Part B code. . Your code should not use material beyond Ch. 3 of the book. Ir your Part A program compiles and runs successfully and meets the above constraints, it will rececive the full 4 points. Part A will not be graded on style ("internal correctness") Part B: Space Needle (28 points): The second part of your assignment is to produce a specific text figure that is sup- | posed to look like Seattle's Space Needle. Turn in a class named spaceNeedle One way to write a Java program to draw this figure would be to write a single |////V/-, this solution would not receive full credit. A major part of this assignment is In lines that have repeated patterns of characters that vary in number from line to in a file named Spacelleedle.java. You should exactly reproduce the format of the output at left. This includes having identical characters and spacing. ///////- --' system.out.printin statement that prints each line of the figure. However, ////// / showing that you understand for loops _ _ line, represent the lines and character patterns using nested tor loops. (See Chap- ter 2's case study.) It may help to write pseudocode and tables to understand the patterns, as described in the textbook and lecture. Another significant component of this assignment is the task of generalizing the program using a single class constant that can be changed to adjust the size of the figure. See the next page for a description of this constant and how it should be used in your program. The course web site will contain files that show you the expected output if your size constant is changed to various other values. You can use our Output Com parison Tool on the course web site to measure numbers of characters and to ver- ify the correctness of your output for various Part A will not be graded for style except as specified on the first page. But Part B will be graded both on "external correctness" (whether the produces exactly the expected output) and "internal correctness" (whether your source code follows the style guidelines in this document). | 881188 981188 881188 S113 81188 program compiles and 1188 Fesz11 HEEsRcz:: (continued on back) 1 of 2

Explanation / Answer

AsciiArt.java


public class AsciiArt {

   /**
   * class constant SIZE determines size of figure. total number of lines is two times SIZE
   */
   public static final int SIZE = 15;

   /**
   * prints a figure using symbols to the console
   */
   public static void main(String[] args) {
       printFigure();
   }

   /**
   * prints the figure to the console
   */
   public static void printFigure() {
       for(int lineNumber = 0; lineNumber < SIZE; lineNumber++) {
           printChar(SIZE*2-2*lineNumber, '\');   
           printChar(2*lineNumber, '<');
           printChar(2*lineNumber, '\');
           printChar(SIZE*2-2*lineNumber, '<');   
           System.out.println();
       }
       for(int lineNumber = SIZE-1; lineNumber >= 0; lineNumber--) {
           printChar(SIZE*2-2*lineNumber, '>');   
           printChar(2*lineNumber, '\');
           printChar(2*lineNumber, '>');
           printChar(SIZE*2-2*lineNumber, '\');   
           System.out.println();
       }
   }

   /**
   * prints a character a given number of times
   */
   public static void printChar(int numberOfTimes, char character) {
      for(int ii = 0; ii < numberOfTimes; ii++) {
         System.out.print(character);
      }
   }
}


sample output

\\\\\\\\\\\\\\\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<                                                                                                
\\\\\\\\\\\\\\<<\<<<<<<<<<<<<<<<<<<<<<<<<<<<<                                                                                                
\\\\\\\\\\\\\<<<<\\<<<<<<<<<<<<<<<<<<<<<<<<<<                                                                                                
\\\\\\\\\\\\<<<<<<\\\<<<<<<<<<<<<<<<<<<<<<<<<                                                                                                
\\\\\\\\\\\<<<<<<<<\\\\<<<<<<<<<<<<<<<<<<<<<<                                                                                                
\\\\\\\\\\<<<<<<<<<<\\\\\<<<<<<<<<<<<<<<<<<<<                                                                                                
\\\\\\\\\<<<<<<<<<<<<\\\\\\<<<<<<<<<<<<<<<<<<                                                                                                
\\\\\\\\<<<<<<<<<<<<<<\\\\\\\<<<<<<<<<<<<<<<<                                                                                                
\\\\\\\<<<<<<<<<<<<<<<<\\\\\\\\<<<<<<<<<<<<<<                                                                                                
\\\\\\<<<<<<<<<<<<<<<<<<\\\\\\\\\<<<<<<<<<<<<                                                                                                
\\\\\<<<<<<<<<<<<<<<<<<<<\\\\\\\\\\<<<<<<<<<<                                                                                                
\\\\<<<<<<<<<<<<<<<<<<<<<<\\\\\\\\\\\<<<<<<<<                                                                                                
\\\<<<<<<<<<<<<<<<<<<<<<<<<\\\\\\\\\\\\<<<<<<                                                                                                
\\<<<<<<<<<<<<<<<<<<<<<<<<<<\\\\\\\\\\\\\<<<<                                                                                                
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\\\\\\\\\\\\\<<                                                                                                
>>\\\\\\\\\\\\\\>>>>>>>>>>>>>>>>>>>>>>>>>>>>\                                                                                                
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<\\\\\\\\\\\\\\<<                                                                                                
>>\\\\\\\\\\\\\\>>>>>>>>>>>>>>>>>>>>>>>>>>>>\                                                                                                
>>>>\\\\\\\\\\\\\>>>>>>>>>>>>>>>>>>>>>>>>>>\\                                                                                                
>>>>>>\\\\\\\\\\\\>>>>>>>>>>>>>>>>>>>>>>>>\\\                                                                                                
>>>>>>>>\\\\\\\\\\\>>>>>>>>>>>>>>>>>>>>>>\\\\                                                                                                
>>>>>>>>>>\\\\\\\\\\>>>>>>>>>>>>>>>>>>>>\\\\\                                                                                                
>>>>>>>>>>>>\\\\\\\\\>>>>>>>>>>>>>>>>>>\\\\\\                                                                                                
>>>>>>>>>>>>>>\\\\\\\\>>>>>>>>>>>>>>>>\\\\\\\                                                                                                
>>>>>>>>>>>>>>>>\\\\\\\>>>>>>>>>>>>>>\\\\\\\\                                                                                                
>>>>>>>>>>>>>>>>>>\\\\\\>>>>>>>>>>>>\\\\\\\\\                                                                                                
>>>>>>>>>>>>>>>>>>>>\\\\\>>>>>>>>>>\\\\\\\\\\                                                                                                
>>>>>>>>>>>>>>>>>>>>>>\\\\>>>>>>>>\\\\\\\\\\\                                                                                                
>>>>>>>>>>>>>>>>>>>>>>>>\\\>>>>>>\\\\\\\\\\\\                                                                                                
>>>>>>>>>>>>>>>>>>>>>>>>>>\\>>>>\\\\\\\\\\\\\                                                                                                
>>>>>>>>>>>>>>>>>>>>>>>>>>>>\>>\\\\\\\\\\\\\\                                                                                                
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\\\\\\\\\\\\\\\                                                                                                

SpaceNeedle.java

public class SpaceNeedle {

   /**
   * the class constant SIZE determines the size of spaceneedle.
   */
   public static final int SIZE = 6;

   /**
   * the main method prints a spaceneedle to the console using different symbols
   */
   public static void main(String[] args) {
      spire();
      domeMidSection();
      domeBottom();
      spire();
      body();
      domeMidSection();
   }

   /**
   * prints the spire to the console
   */
   public static void spire() {
      //each iteration of for loop represents on line of output
      for(int lineNumber = 0; lineNumber < SIZE; lineNumber++) {
         int numSpaces = SIZE*3; //calulates number of spaces

         printChar(numSpaces," "); //prints spaces
         System.out.println("||");
      }
   }

   /**
   * prints the mid section of the dome to the console
   */
   public static void domeMidSection() {
      domeTop();
      domeFloor();
   }

   /**
   * prints the top of dome to the console
   */
   public static void domeTop() {
      //each iteration of for loop represents one line of output
      for(int lineNumber = 0; lineNumber < SIZE; lineNumber++) {
         int numSpaces = (SIZE*3-3)-(3*lineNumber); //calculates number of spaces
         int numSemiColons = 3*lineNumber; //calculates number of semicolons

         printChar(numSpaces," "); //prints spaces
         System.out.print("__" + "/");
         printChar(numSemiColons,":"); //prints semicolons
         System.out.print("||");
         printChar(numSemiColons,":"); //prints semicolons
         System.out.println("\" + "__");
      }
   }

   /**
   * prints the dome floor to the console
   */
   public static void domeFloor() {
      int numQuotes = SIZE*6; //calcualtes number of quotes

      System.out.print("|");
      printChar(numQuotes,"""); //prints quotations
      System.out.println("|");
   }

   /**
   * prints bottom of the dome to the console
   */
   public static void domeBottom() {
      //each iteration of for loop represents on line of output
      for(int lineNumber = 0; lineNumber < SIZE; lineNumber++) {
         int numSpaces = 2*lineNumber; //calcualtes number of spaces
         int numV = (SIZE*3-2)-(2*lineNumber); //calculates number of V's

         printChar(numSpaces," "); //prints spaces
         System.out.print("\_/");
         printChar(numV,"\/"); //prints V's
         System.out.println("\_/");
      }
   }

   /**
   * prints body of needle to the console
   */
   public static void body() {
      //each iteration of for loop represents one line of output
      for(int lineNumber = 0; lineNumber < SIZE*SIZE; lineNumber++) {
         int numSpaces = 2*SIZE+1; //calculates number of spaces
         int numPercents = SIZE-2; //calculates number of percents

         printChar(numSpaces," "); //prints spaces
         System.out.print("|");
         printChar(numPercents,"%"); //prints %s
         System.out.print("||");
         printChar(numPercents,"%"); //prints %s
         System.out.println("|");
      }
   }

   /**
   * prints a string a given number of times to the console
   */
   public static void printChar(int numberOfTimes, String symbol) {
      for(int ii = 0; ii < numberOfTimes; ii++) {
         System.out.print(symbol);
      }
   }
}

sample output

                  ||                                                                                                                                        
                  ||                                                                                                                                        
                  ||                                                                                                                                        
                  ||                                                                                                                                        
                  ||                                                                                                                                        
                  ||                                                                                                                                        
               __/||__                                                                                                                                     
            __/:::||:::__                                                                                                                                  
         __/::::::||::::::__                                                                                                                               
      __/:::::::::||:::::::::__                                                                                                                            
   __/::::::::::::||::::::::::::__                                                                                                                         
__/:::::::::::::::||:::::::::::::::__                                                                                                                      
|""""""""""""""""""""""""""""""""""""|                                                                                                                      
_/////////////////_/                                                                                                                      
_///////////////_/                                                                                                                        
    _/////////////_/                                                                                                                          
      _///////////_/                                                                                                                            
        _/////////_/                                                                                                                              
          _///////_/                                                                                                                                
                  ||                                                                                                                                        
                  ||                                                                                                                                        
                  ||                                                                                                                                        
                  ||                                                                                                                                        
                  ||                                                                                                                                        
                  ||                                                                                                                                        
             |%%%%||%%%%|                                                                                                                                   
             |%%%%||%%%%|                                                                                                                                   
             |%%%%||%%%%|                                                                                                                                   
             |%%%%||%%%%|                                                                                                                                   
               __/||__                                                                                                                                     
            __/:::||:::__                                                                                                                                  
         __/::::::||::::::__                                                                                                                               
      __/:::::::::||:::::::::__                                                                                                                            
   __/::::::::::::||::::::::::::__                                                                                                                         
__/:::::::::::::::||:::::::::::::::__                                                                                                                      
|""""""""""""""""""""""""""""""""""""|