Lab Questions
Chapter 2: Using Objects

1.1.  The String class provides methods that you can apply to String objects. One of them is the length method. The length method counts the number of characters in a string. For example, the sequence of statements

String greeting = "Hello, World!";
int n = greeting.length();

sets n to the number of characters in the string object "Hello, World!" (13).

Let’s look at another method of the String class. When you apply the toUpperCase method to a String object, the method creates another String object that contains the characters of the original string, with lowercase letters converted to uppercase. For example, the sequence of statements

String river = "Mississippi";
String bigRiver = river.toUpperCase();

sets bigRiver to the String object "MISSISSIPPI". Similarly, applying the toLowerCase method to a String object creates another String object that contains the characters of the original string, with uppercase letters converted to lowercase.

Write a program that constructs a String object with the value "This is a Test" and then creates a new String object with the same message as the original string, but with every character converted to lowercase. Then, print the new string.

1.2.  Now, add the following lines to the program you created in Lab 2.1.1, right after the System.out.println statement:

String bigTestString = smallTestString.toUpperCase(); 
   // Replace "smallTestString" with the 
   // name you used for your lowercase string
System.out.println(bigTestString);

Notice that your program now applies the toLowerCase method to the original string, and then applies the toUpperCase method to that string. Paste the modified program below.

After applying the toUpperCase method, do you obtain the original string back? What will the System.out.println(bigTestString) statement print?

2.1.  The API (Application Programming Interface) documentation lists the classes and methods of the Java library.

Go to http://java.sun.com/javase/7/docs/api/index.html and find out what the concat method of the String class does.

2.2.  Complete the following program so that it prints the message "the quick brown fox jumps over the lazy dog".

public class ConcatDemo
{
   public static void main(String[] args)
   {
      String animal1 = "quick brown fox";
      String animal2 = "lazy dog";

      String article = "the";
      String action = "jumps over";

      /* Your work goes here */

      System.out.println(message);
   }
}

2.3.  There is an alternative to using the concat method for concatenating strings.  The + operator is commonly used for adding numeric data, but behaves as a concatenation operator when String data is involved.  For example the Java expression, "abc" + "def" will produce the String "abcdef".  The + operator will also convert numbers to Strings when one of the operators is a String.  The expression "abc" + 123 produces the String "abc123".  Repeat Lab 2.2.2 using the + operator instead of the concat method.  

2.4.  Complete the following program so that it prints the message "abcdefghi".

public class TrimIt
{
   public static void main(String[] args)
   {
      String sentence1 = "      abc   ";
      String sentence2 = "   def";
      String sentence3 = "ghi         ";
       
      /* Your work goes here */

      System.out.println(message);
   }
}

3.1.  The API (Application Programming Interface) documentation lists the classes and methods of the Java library.

Go to http://java.sun.com/javase/7/docs/api/index.html and find out what the StringTokenizer class does.

3.2.  In the following exercise, we will be using two methods from the class StringTokenizer: countTokens and nextToken.

Go to http://java.sun.com/javase/7/docs/api/index.html and find out what the methods countTokens and nextToken of the StringTokenizer class do.

3.3.  Consider the following program:

import java.util.StringTokenizer;

public class StringTokenizerDemo
{
   public static void main(String[] args)
   {
      String sentence = "Mary had a little lamb.";
      StringTokenizer mystery = new StringTokenizer(sentence);
      System.out.println(mystery.countTokens());
      System.out.println(mystery.nextToken());
      System.out.println(mystery.nextToken());
   }
}

What does it print?

4.1.  The following program creates a new Rectangle and prints its coordinates.

import java.awt.Rectangle;

public class RectanglePrinter
{
   public static void main(String[] args)
   {
      Rectangle r1 = new Rectangle(0, 0, 100, 50);
      /* Your code goes here */
      System.out.println(r1);
      /* and here */
   }
}

Add code to the program above to create a second rectangle with the same values (x, y, width and height) as the first Rectangle. Then, apply the grow method to the second rectangle (grow(10, 20)) and print both rectangles. For more information on the grow method, look at the API documentation. 

4.2.  Compile and run your program. What is its output?

4.3.  Modify your program and change the line creating the second rectangle to:

Rectangle r2 = r1;

Compile and run your program. What is the output? Why?

4.4.  Consider the following program:

public class NumberVariablesPrinter
{
   public static void main(String[] args)
   {
      double n1 = 150;
      double n2 = n1;

      n2 = n2 * 20; // Grow n2

      System.out.println(n1);
      System.out.println(n2);
   }
}

Notice that this program is very similar to the program you created for Lab 2.4.3, but it uses number variables instead of object references.

Compile and run the program. What is the output? Why? (In your answer, contrast the output of this program to that of the program in Lab 2.4.3.)

5.  Complete the missing lines in the following code:

import javax.swing.____;

public class TestFrameViewer
{
   public static void main(String[] args)
   {
      ____ frame = new ____();

      final int FRAME_WIDTH = 250;
      final int FRAME_HEIGHT = 250;

      frame.____(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setTitle("A Test Frame");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.____(true);
   }
}

6.1.  Create a component (extends JComponent) that uses three Line2D.Double objects to draw a triangle that looks like this:

6.2. To show the component that draws a triangle you need a viewer class. Write a viewer class that shows the triangle component that you created in Lab 2.6.1.

7.  Use Rectangle and Line2D.Double objects to draw a house like this one:

What is the code for your component?

8.  Generate five circles with the following diameters, 40, 80, 120, 160, and 200, all tangent at a common point.

Draw each circle in a different color. What is the code for your component?