Introduction to Java Programming

Lab Exercises

 

Topics                                     Lab Exercises

Printing strings                                     Prelab Exercises

                                                                Poem

Syntax errors                                         Recognizing Syntax Errors

                                                                Correcting Syntax Errors

 

 


Prelab Exercises

 

Your task is to write a Java program that will print out the following message (including the row of equal marks):

 

                  Computer Science, Yes!!!!

                  =========================

 

An outline of the program is below. Complete it as follows:

 

a. In the documentation at the top, fill in the name of the author, date written, name of the file the program would be saved in and a brief description of what the program does.

 

b. Add the code for the main method to do the printing.

 

// *******************************************************************

// AUTHOR:

// DATE:

// File Name:

//

// Purpose:

// *******************************************************************

 

public class CSYes

{

      // -------------------------------------------------

      // The following main method prints an exciting

      // message about computer science

      // -------------------------------------------------

}

 

 

 

 

 

Poem

 

Write a Java program that prints the message, "Roses are red". Your program will be a class definition containing a main

Method — see the Lincoln example in Listing 1.1 of the text if you need guidance. Remember the following:

 

 

Compile and run your program. When it works correctly, modify it so that it prints the entire poem:

 

Roses are red

Violets are blue

Sugar is sweet

And so are you!

 


Recognizing Syntax Errors

 

When you make syntax errors in your program the compiler gives error messages and does not create the bytecode file. It saves time and frustration to learn what some of these messages are and what they mean. Unfortunately, at this stage in the game many of the messages will not be meaningful except to let you know where the first error occurred. Your only choice is to carefully study your program to find the error. In the following you will introduce a few typical errors into a simple program and examine the error messages.

 

1. Type the following program into a file called Hello.java. (This is the traditional first program a computer scientist writes in a new language.)

 

// ********************************************

// Hello.java

//

// Print a Hello, World message.

// ********************************************

 

public class Hello

{

      // -----------------------------------

      // main method -- prints the greeting

      // -----------------------------------

      public static void main (String[] args)

      {

            System.out.println ("Hello, World!");

      }

}

 

Compile and run the program to see what it does. Then make the changes below, answering the questions as you go.

 

2. Class name different from file name. Delete one l (el) from the name of the class (so the first non-comment line is public class Helo), save the program, and recompile it. What was the error message?

 

3. Misspelling inside string. Correct the mistake above, then delete one l from the Hello in the message to be printed (inside the quotation marks). Save the program and recompile it. There is no error message—why not? Now run the program. What has changed?

 

4. No ending quotation mark in a string literal. Correct the spelling in the string, then delete the ending quotation mark enclosing the string Hello, World!. Save the program and recompile it. What error message(s) do you get?

 

5. No beginning quotation mark in a string literal. Put the ending quotation mark back, then take out the beginning one.

Save and recompile. How many errors this time? Lots, even though there is really only one error. When you get lots of errors always concentrate on finding the first one listed!! Often fixing that one will fix the rest. After we study variables the error messages that came up this time will make more sense.

 

6. No semicolon after a statement. Fix the last error (put the quotation mark back). Now remove the semicolon at the end of the line that prints the message. Save the program and recompile it. What error message(s) do you get?

 

 


Correcting Syntax Errors

 

File problems.java contains a simple Java program that contains a number of syntax errors. Save the program to your directory, study it and correct as many of the errors as you can find. Then compile the program; if there are still errors, correct them. Some things to remember:

 

 

// ********************************************

// Problems.java

//

// Provide lots of syntax errors for the user to correct.

//

********************************************

public class problems

{

      public Static main (string[] args)

      {

            System.out.println ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

            System.out.println (This program used to have lots of problems,");

            System.0ut.println ("but if it prints this, you fixed them all.")

            System.out.println (" *** Hurray! ***);

            System.out.println ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

      }

}

 

 

Submitting your assignment in WebCT Vista

 

Your task is to submit your assignment under the supervision of your instructor:

o        Upload this file (Introduction.rtf) with your answers to the assignment box in WebCT for this lab class.

o        Upload your programs (CSYes.Java, Poem.Java, and problems.java) to the assignment box in WebCT for this lab class.