1c

Advanced Placement

Computer Science Lab

 

 

String Modification

 

 

 

Introduction

 

In this lab we will write some simple Java programs that will modify the output of the strings that we have been printing to the screen.  Modification of the strings is accomplished using what are called "escape sequences".  A table of escape sequences can be found on page 66 of your textbook.

 

 

 

Procedure

 

Create a New Project and Test Escape Sequences

 

1.         As in the previous exercise, create a new project labeled "EscapeTest". 

 

2.         Create a new class in the project as in the previous exercise.

 

3.         Copy the following code and use it to replace the code that the IDE gives

 

 

 

/*

EscapeTest.java

(Your Name) Version (x.x) (date xx/xx/xx)

Program prints a string

*/

 

 

class EscapeTest

{

     public static void main (String args[])

     {

          System.out.println("Four score and seven years ago our forefathers brought forth a great nation conceived in liberty and dedicated to the proposition that all men are created equal.");

 

     }//end main

 

}//end class

 

 

 

4.         Compile and execute the code.  How does the output look on the screen?

 

5.         Change the code to include a "\n" some place in the string.  Compile and run.  What happens?

 

6.         Modify the code so that the phrase prints on four lines using escape sequences?

 

7.         Modify the code so that the phrase prints in the middle of the screen.  Hint:  use tab and newline sequences.

 

8.         What happens if you put quotation marks someplace in the sequence.  Determine how you could leave the quotation marks.

 

 

 

 

Programming Assignment

 

1.         Write a program to print your resume. Use various escape sequences so that nothing but the resume appears on the screen.

 

2.         Turn in a program listing your source code and output as with the first exercise.  Be sure to modify the output window properties to change the background and foreground colors.

 

 

 

 

Questions

 

 

1.         What  does the back slash in the string really do?.

 

2.         What do the beginning and end of a string literal in Java start with and end with?.

 

3.         What is Java byte code?

 

4.         Explain the difference between a compiler and interpreter?

 

5.         What are the reserved words in the above listing?