Sunteți pe pagina 1din 7

Week 3 Homework

Problem 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 following sequence of statements sets n to the number of characters in the string object "Hello, World!" (i.e., 13 characters).
String greeting = "Hello, World!"; int n = greeting.length();

Let us look at another method of the String class, toUpperCase. When you apply the toUpperCase method to a String object, the method creates another String object that contains the characters of the original string but with lowercase letters converted to uppercase. For example, the following sequence of statements sets bigRiver to the String object "MISSISSIPPI".
String river = "Mississippi"; String bigRiver = river.toUpperCase();

Similarly, the toLowerCase method applied to a String object creates another String object that contains the characters of the original string, with uppercase letters converted to lowercase. a) Write a program that constructs a String object with the value "This is a Test" and then creates a new String with the same message as the original string but with every character converted to lowercase. The program should then print the new string.

Week 3 Homework Rev. 3/3/10

b) Now, add the following two lines right after the System.out.println statement in the program you created for the previous exercise:
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.

c) After applying the toUpperCase method, is the value of the string bigTestString identical (characters and case) to the value of the original string in the first statement of part a? What will the System.out.println(bigTestString) statement print?

Week 3 Homework Rev. 3/3/10

Week 3 Homework Rev. 3/3/10

Problem 2 Complete the following program so that it constructs a string with the contents "the quick brown fox jumps over the lazy dog" using the provided string variables and then prints that string and its length. Do NOT alter the value of any of the original string variables, and note that the final string should contain embedded blanks as appropriate to accurately reproduce the specified message.
public class ConcatTester { 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 */

} }

Week 3 Homework Rev. 3/3/10

Problem 3 The Rectangle class in the standard Java API package java.awt is designed to logically represent a rectangle by simply storing information about a given rectangle's position on a graph (its x-y coordinates) as well as its dimensions (width and height). If you call one of the System.out print methods on a Rectangle object variable, the x-coordinate, ycoordinate, width and height of the rectangle logical representation is displayed. The following program creates a new Rectangle and prints its information.
import java.awt.Rectangle; public class RectangleTester { public static void main(String[] args) { Rectangle r1 = new Rectangle(0, 0, 100, 50); } } System.out.println(r1);

a) Add code to the program above to create a second rectangle with the same values (x, y, width, and height) as the first Rectangle (see instructions below on what constructor to use). Print the second rectangle as per the code provided for the first rectangle. Then apply the grow method to the second rectangle variable (grow(10, 20)) and, after the grow method, print both rectangles again. This will give you a before and after view of the attribute values associated with each of the two rectangle object variables. For more information on the grow method, look at the API documentation. Use the following Rectangle constructor to create the second rectangle:
public Rectangle(Rectangle r)

Copy your modified program source code below. Compile and run the program and copy the output below the source code.

Week 3 Homework Rev. 3/3/10

Week 3 Homework Rev. 3/3/10

b) Modify your program and change the line where you create the second rectangle to:
Rectangle r2 = r1;

Compile and run your program. Copy the output below. Compare and contrast the output of the two programs and provide an explanation for the result.

Week 3 Homework Rev. 3/3/10

S-ar putea să vă placă și