CS99

Fundamental Programming Concepts
Summer 2001

Lab 2

 

Overview

The goals of this lab are to:

  • Examine a few of the compiler errors that can happen when syntax mistakes are made

  • Learn how to use escape sequences to format output on the screen

 

Part I: Syntax

Create a new project and call it Lab2.  If you don't remember how to do this, look at the instructions in the first lab.  As before, delete TrivialApplication.java from the project; create a new text file called Test.java and add it to your project (Don't forget to change the Java Target to Test).

Enter, compile, and run the following code in Test.java :

class Test {
   public static void main( String[] args ) {
      System.out.println( "An Emergency Broadcast." );
   }
}

Introduce the following errors, one at a time, to this program. Fix the previous error before you try the next one. 

a)       Change Test to test.

b)       Change Emergency to emergency.

c)       Remove the first quotation mark in the string literal.

d)       Remove the last quotation mark in the string literal.

e)       Change System to system.

f)        Change main to man.

g)       Change println to bogus.

h)       Change Broadcast to Brxoadxcaxst.

i)        Remove the semicolon at the end of the println statement.

j)         Remove the last curly brace in the program.

For each error, copy into a text file the message the compiler produces. If no error message is produced, explain why.

Save the text file as Test.txt.  Put your name, net ID, and date at the top of the file and print it.  Be sure to clearly label which errors go with which items on the list above.
 

Part II: Escape Sequences

Remove Test.java from the project and create a new Text file.  Call it Lab2.java and write the following code exactly into it:

/**
* CS99 Lab 2
* Author: your name here

* NetID: your NetID here
* Date: today's date
*/

class Lab2 {
   public static void main( String[] args ) {
      System.out.println( "Jenny Kiss'd Me "
         + "Jenny kiss'd me when we met,Jumping from the chair she sat in;" 
         + "Time, you thief, who love to get Sweets into your list, put that in!"
        
+ "Say I'm weary, say I'm sad, Say that health and wealth have miss'd me,"
         + "Say I'm growing old, but add, Jenny kiss'd me. "
         + "--Leigh Hunt, Poet."

      
); 
   }
}

Compile and run the program to see how the statements appear on the screen (don't forget to change the target to Lab2).  You should see a long messy run-on sentence.  We want to format this sentence without using extra println statements. Java has escape sequences designed for this very purpose.  This is a short list of a few of them:

Escape Sequence Meaning
\t tab
\n newline
\r carriage return
\" double quote
\' single quote
\\ backslash

We use escape sequences inside quotation marks.  For instance, change the first string from "Jenny Kiss'd Me" to "Jenny Kiss'd Me\n" and run the program again to see what happens.  Did you notice anything different?

Using these escape sequences, modify the strings in the code above so that the following output is produced on the computer screen:

Jenny Kiss'd Me

Jenny kiss'd me when we met,
      Jumping from the chair she sat in;
Time, you thief, who love to get
      Sweets into your list, put that in!
Say I'm weary, say I'm sad,
      Say that health and wealth have miss'd me,
Say I'm growing old, but add,
      Jenny kiss'd me.

                     --Leigh Hunt, Poet

 

Part IV: Submit Your Lab

Hand in printouts for

  • Test.txt

  • Lab2.java

  Don't forget to put your name and net ID at the top of each file.

This lab is due at the beginning of lab on Tuesday!  Hand it in to Siddarth.