Chapter 2

Test Your Thinking

If you haven't already done so, please visit the Web site for this book.  It contains additional material and information.  You will also find the code used in this book, both in the examples and in the exercises.

The Web page for this book can be found at the following location:

http://www.phptr.com/phptrinteractive

 

1) Go to the Web site and download the source code to the Thinking2_1.java program.  Modify this program to print your own name instead of the "John Smith" name.

 

Answer:

Even if you don't fully understand Java yet, you should be able to guess that you just need to replace one string with another.  In this case, replace "John Smith" with your name.

 

Here is the "before" code:

public class Thinking2_1

{

    public static void main(String args[])

    {

          System.out.println("John Smith");

    }

}

And here is the "after" code:

public class Thinking2_1

{

    public static void main(String args[])

    {

          System.out.println("Your Name");

    }

}


2) Modify your program from Question 1 to print your name 5 times.

 

Answer:

Cut and paste can be your friend!  Just copy the "print" line five times.  We will show you better ways of doing repetitive operations later.

 

We took the previous code and copied the print line five times.

 

public class Thinking2_1

{

    public static void main(String args[])

    {

          System.out.println("Your Name");

          System.out.println("Your Name");

          System.out.println("Your Name");

          System.out.println("Your Name");

          System.out.println("Your Name");

    }

}


3) Add comments to any of your programs.

 

Answer:

Use the comment characters to delimit comments.

 

Here is the same code with comments added to it:

 

// This is a comment!

public class Thinking2_1

{

    // This is another comment!

    public static void main(String args[])

    {

          /**

           * This is a long comment

           * that has several lines!.

           */

          System.out.println("Your Name");

    }

}