What Is Programming Solution 2

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > What_Is_Programming_Questions


Introduction

An exercise to print a story with repetition and special characters.

   

{{{Body}}}

What do you need to know?

  • The story to print. This is a story by little Nancy who was given a task in school, to write a 50-word essay about her pet. Here's what she wrote:
My cat was lost, so I called, "Here, kitty, kitty, kitty. 
Here, kitty, kitty, kitty. Here, kitty, kitty, kitty."
She didn't come, so I went outside and called, "Here, kitty, kitty, kitty.
Here, kitty, kitty, kitty. Here, kitty, kitty, kitty. Here, kitty, kitty, kitty."
Then she came, and I said, "Bad kitty!" and I took her home.
  • How to store a String and use it many times.
  • How to include special characters like a quotation mark and a newline character in a String.

Inputs

No inputs are required.

Outputs

  • The story approximately as shown above.
  • Make sure you start "She didn't come..." and "Then she came..." at the beginning of a line.
  • Use only one call to System.out.println.

Transformation

Since there is no input, no transformation of the input is required.

Solution

/**...............................................................PrintStory
  * Print a story with repetition and special characters
  * COMP1010 Exercise (What Is Programming?)
  * @author Terry Andres 
  * @version 2009-Sep-10
  */
public class PrintStory {   
  
  /**...................................................................main
   * Required method.
   */
  public static void main( String[] args ) {
    final String CALL = "Here, kitty, kitty, kitty." ;
    final String STORY = 
      "My cat was lost, so I called, \"" 
      + CALL + ' ' + CALL + ' ' + CALL + "\"\n"
      + "She didn't come, so I went outside and called, \""
      + CALL + ' ' + CALL + ' ' + CALL + ' ' + CALL + "\"\n" 
      + "Then she came, and I said, \"Bad kitty!\" and I took her home." ;
      
    System.out.println(STORY) ;

  }
}

Template loop detected: Template loop detected: