Download Chapter 2 Solutions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter 2
1. Identify all errors in the following program:
/*
Program Exercise1
Attempting to display a frame window
//  1
import swing.JFrame;  2
class Exercise 1 {  3
public void Main() {  4
JFrame frame;  5
frame.setVisible(TRUE)  6
}
}
2. Identify all errors in the following program:
//  1
Program Exercise2
Attempting to display a frame of the size 300 by 200 pixels
//  2
import Javax.Swing.*;  3
class two {
public static void main method() {  4
myFrame JFrame;  5
myFrame = new JFrame();
myFrame.setSize(300, 200);
myFrame.setVisible();  6
}
}
3. Identify all errors in the following program:
/*
Program Exercise3
Attempting to display the number of characters
in a given input.
*/
 1
class three {
public static void main( ) {  2
String input;
input = Scanner.next();  3,4
System.out.print ( "Input has " +
input.length() + " characters");
}
}
4. Describe the purpose of comments. Name the types of comments available. Can you
include comment markers inside a comment?
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.
7. Describe the class that must be included in any Java application.
8. What is a reserved word? List all the Java reserved words mentioned in this chapter.
9. Which of the following are invalid Java identifiers?
a.
b.
c.
d.
e.
f.
R2D2
Whatchamacallit
HowAboutThis?
Java
GoodChoice
12345
g. 3CPO
h. This is okay.
i. thisIsReallyOkay
j. DEFAULT_AMT
k. Bad-Choice
l. A12345
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?
11. Describe the difference between object declaration and object creation. Use a stateof-memory diagram to illustrate the difference.
12. Show a state-of-memory diagram after each of these statements is executed:
JFrame
Resident
window1;
res1, res2;
window1
res1
res2
= new JFrame();
= new Resident();
= new Resident();
13. Show a state-of-memory diagram after each of these statements is executed:
Person
person1
person2
person2
person1, person2;
= new Person();
= new Person();
= new Person();
14. Which of these identifiers violate the naming convention for class names?
a.
b.
c.
d.
r2D2
whatchamacallit
Java
GoodName
e.
f.
g.
h.
CPO
ThisIsReallyOkay
java
badName
15. Which of these identifiers violate the naming convention for object names?
a.
b.
c.
d.
R2D2
isthisokay?
Java
goodName
e.
f.
g.
h.
3CPO
ThisIsReallyOkay
java
anotherbadone
16. For each of these expressions determine its result. Assume the value of text is a
string Java Programming.
String text = "Java Programming";
a.
b.
c.
d.
e.
text.substring(0,
text.length( )
text.substring(8,
text.substring(0,
text.substring(5,
text.length())
4)
12)
1) + text.substring(7, 9)
6) + text.substring(text.length() – 3,
Level 1 Programming Exercises
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
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.
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.
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.
21. Repeat exercise 20, but this time use this format: Monday December 10, 2007.
Level 2 Programming Exercises
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.
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.
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
//assume mainWindow is declared and created
frame.setLocation( 50, 50 );
Through experimentation, determine how the two arguments in the setLocation
method affects the positioning of the window.
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:
try {Thread.sleep(500);} catch(Exception e) { }
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.
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