An Introduction To Object Oriented Programming With Java 5th Edition C Thomas Wu Solution Manual
An Introduction To Object Oriented Programming With Java 5th Edition C Thomas Wu Solution Manual
An Introduction To Object Oriented Programming With Java 5th Edition C Thomas Wu Solution Manual
Program Exercise1
// 1
import swing.JFrame; 2
class Exercise 1 { 3
public void Main() { 4
JFrame frame; 5
frame.setVisible(TRUE) 6
}
}
class two {
4. Describe the purpose of comments. Name the types of comments available. Can you
include comment markers inside a comment?
Purposes:
To describe the program, methods, data values, and other components
To explain the meaning of code
To provide other helpful information to improve the readability
Types:
Multi-line comments
Single-line comments
Javadoc comments
In some cases yes (like including // in a multi-line comment), but in other cases
no.
5. What is the purpose of the import statement? Does a Java program always have to
include an import statement?
6. Show the syntax for importing one class and all classes in a package.
One class:
import <package name>.<class name>;
All classes:
import <package name>.*;
Every java application requires one class to be designated as its main class. The
designated main class must include the main method.
8. What is a reserved word? List all the Java reserved words mentioned in this chapter.
Reserved words introduced in this chapter are new, import, class, public, static,
void, true, false, and null.
a. R2D2 g. 3CPO
b. Whatchamacallit h. This is okay.
c. HowAboutThis? i. thisIsReallyOkay
d. Java j. DEFAULT_AMT
e. GoodChoice k. Bad-Choice
f. 12345 l. A12345
Invalid identifiers are c (no question mark is allowed), f (the first character must
be a non-digit), g (the first character must be a non-digit), h (no space is
allowed), and k (no hyphen is allowed).
10. Describe the steps you take to run a Java application and the tools you use in each
step. What are source files and bytecode files? What different types of errors are
detected at each step?
The cycle is Edit-Compile-Run. In the Edit state, you enter a program using any
text editor. The file you create is called the source file, which is in human
readable form and contains the Java statements that make up the program. In the
Compile stage, you compile the source file to create a bytecode file. The tool
(another piece of software) to compile the Java source file is called javac. In the
Run stage, you execute the bytecode file by using the Java interpreter called java.
Note: The Java interpreter is called the Java Virtual Machine (JVM).
Syntax and semantic errors are caught by the compiler. A compiler cannot catch
logic errors. Logic errors are detected when the program is actually executed.
11. Describe the difference between object declaration and object creation. Use a state-
of-memory diagram to illustrate the difference.
JFrame myFrame;
Object creation (provided that the object is already declared) allocates the
memory space for the object itself and stores the reference (arrow) to this object
in the object name.
JFrame window1;
Resident res1, res2;
a. r2D2 e. CPO
b. whatchamacallit f. ThisIsReallyOkay
c. Java g. java
d. GoodName h. badName
The convention is to use an uppercase letter for the first character. The first
character of a new word is also capitalized. All other letters are lowercase. The
identifiers a, b, g, and h violate the convention. The identifier CPO should be
considered as valid if it is interpreted as some kind of acronym.
15. Which of these identifiers violate the naming convention for object names?
a. R2D2 e. 3CPO
b. isthisokay? f. ThisIsReallyOkay
c. Java g. java
d. goodName h. anotherbadone
The convention for object names is almost identical to the one for class names,
except the first character is lowercase. The identifiers a, b, c, e, f, and h violate
the convention. Notice that b and e are not even valid identifiers.
16. For each of these expressions determine its result. Assume the value of text is a
string Java Programming.
a. text.substring(0, 4)
b. text.length( )
c. text.substring(8, 12)
d. text.substring(0, 1) + text.substring(7, 9)
e. text.substring(5, 6) + text.substring(text.length() – 3,
text.length())
a. Java
b. 16
c. gram
d. Jog
e. Ping
17. Write a program that displays a frame window 800 pixels wide and 600 pixels high.
Set the title of the frame to Welcome to Java
See Exercise2_17.java
18. Input the user’s first and last name as two separate strings. Then display a frame
window with its title set to <last>, <first> where <last> and <first> are the input
values. For example, if the input values are Johann and Strauss, then the title would
be Strauss, Johann.
See Exercise2_18.java
19. Input the user’s first, middle and last name as three separate strings and display the
name in the order of the first name, the middle initial, and the last name. Include the
period after the middle initial. If the input strings are Wolfgang, Amadeus, and
Mozart, for example, the output would be Wolfgang A. Mozart. Use the console
window for output.
See Exercise2_19.java
20. Write a program to display today’s date in this format: 10 December 2007. Use the
console window for output. Refer to Table 2.1 for the necessary designator symbols.
See Exercise2_20.java
21. Repeat exercise 20, but this time use this format: Monday December 10, 2007.
See Exercise2_21.java
22. Write a program that displays a frame window W pixels wide and H pixels high.
Use the Scanner to enter the values for W and H. The title of the frame is also
entered by the user.
See Exercise2_22.java
23. Display the current time in the title of a frame window using this format: 12:45:43
PM. Refer to Table 2.1 for the necessary designator symbols.
See Exercise2_23.java
24. Write a Java program that displays a frame window 300 pixels wide and 200
pixels high with the title My First Frame. Place the frame so that its top, left corner
is at a position 50 pixels from the top of the screen and 100 pixels from the left of the
screen. To position a window at a specified location, you use the setLocation
method, as in
See Exercise2_24.java
25. Because today’s computers are very fast, you will probably not notice any
discernable difference on the screen between the code
JFrame myWindow;
myWindow = new JFrame( );
myWindow.setVisible( true );
and
JFrame myWindow;
myWindow = new JFrame( );
myWindow.setVisible( true );
myWindow.setVisible( false );
myWindow.setVisible( true );
One way to see this disappearance and reappearance of the window is to put a delay
between the successive setVisible messages. Here’s the magic code that puts a delay
of 0.5 s:
The argument we pass to the sleep method specifies the amount of delay in
milliseconds [note: 1000 miliseconds (ms) = 1 second (s)]. We will not explain this
magic code.
See Exercise2_25.java
26. Using the Scanner, input a string that contains a single exclamation mark. Divide the
input string into two strings, one before and the other after the exclamation mark, and
output them. Do not include the exclamation mark in the output. For example, if the
input string is one potato two potato !three, then the output would be:
one potato two potato
three
See Exercise2_26.java
27. Write a program that accepts a string input and outputs the number of characters in
the string and the first and last character in separate lines. For example, if the input is
I like Java then the output would be
11
I
a
See Exercise2_27.java
28. Write a program that accepts a word and prints out the middle character. The length
of the input word is odd. For example, if the input is magnificent, which has 11
characters, you output the sixth character f. You use the division operator /. This
operator returns only the quotient. For example, the expression 10/4 would result in 2
(not 2.4).
See Exercise2_28.java
29. At the McGraw-Hill book website, you will find a Java package called
galapagos. The galapagos package includes a Turtle class that is modeled after
Seymour Papert’s logo. This Turtle has a pen, and when you move the Turtle, its
pen will trace the movement. So by moving a Turtle object, you can draw many
different kinds of geometric shapes. For example, this program commands a Turtle
to draw a square:
import galapagos.*;
class Square {
public static void main( String[] args ) {
Turtle turtle;
turtle = new Turtle();
turtle.move( 50 );
turtle.turn( 90 );
turtle.move( 50 );
turtle.turn( 90 );
turtle.move( 50 );
}
}
Write a program to draw a triangle. Read the documentation and see if you can find
a way to draw the square in a different color and line thickness.
See Exercise2_29.java
You can use the penSize and penColor methods to change the thickness and
color of the Turtle’s pen.
See Exercise2_30.java
31. Using the Turtle introduced in Exercise 29, draw a big letter J. You can draw a big J
by drawing one horizontal line, one vertical line, and one semicircle as follows:
See Exercise2_31.java
32. Using the Turtle introduced in Exercise 29, draw three nested rectangles.
See Exercise2_32.java
33. Using the Turtle from Exercise 29, write a Java program that displays the text Hello
as illustrated here:
See Exercise2_33.java
Development Exercises
Problem Statement: Write a program that asks the user for his or her birth date and
replies with the day of the week on which he or she was born.
We learned in this chapter that we can create a Date object for today’s date by
writing
import java.util.*;
…
Date today = new Date();
To create a Date object for a date other than today, we can use the Date class from
the java.sql package. (A more general and flexible way to deal with a date by using
the GregorianCalendar class is introduced in Chapter 3.) Notice that there are two
distinct classes with the same name Date, but from different packages – one from
java.util and another from java.sql. To distinguish the two we will use the fully
qualified names. To create a new java.util.Date object, we can call the class method
valueOf of the java.sql.Date class with the string representation of a date. The
string representation must be in the format yyyy-MM-dd. For example, to create a
java.util.Date object for July 4, 1776, we write
java.util.Date bdate = java.sql.Date.valueOf("1776-07-04");
Notice that valueOf is a class method of the Data class in the java.sql package.
Calling it with a correct argument will return a java.util.Date object for the specified
date.
See Exercise2_34.java
JOptionPane
35. yyy
yyy
See Exercise2_YY.java
java.sql.Date
27. yyy
yyy
Exercise2_34
See Exercise2_YY.java
java.util.Date
27. yyy
yyy
See Exercise2_YY.java
SimpleDateFormat
27. Using a Turtle from Exercise 23 and employing the incremental development steps,
build a Java application that draws a house.
\
Problem Statement: Write a program that asks the user for her or his full name
in the format
first middle last
where the last name is followed by a comma and the middle initial is followed by a
period.
Latte, Decafe C.
See Exercise2_35.java
Assumptions: The user enters valid input in the form first middle last
String
Exercise2_35
Problem Statement: Using the Turtle from exercise 29, write a program that draws a
house.
See Exercise2_36.java
Turtle
Exercise2_36