Search This Blog(textbook name or author as the keywords)You can cantact me by the Contact Form

2/15/14

Java Software Solutions: Foundations of Program Design, 7/E Loftus & Lewis solutions manual and test bank

Java Software Solutions: Foundations of Program Design plus MyProgrammingLab with Pearson eText -- Access Card, 7/E solutions manual and test bank John Lewis, Virginia Tech William Loftus


Downloadable Instructor Resources

Help downloading instructor resources
  1. Lab Manual for Java Software Solutions: Foundations of Program Design, 7/E
    Lewis
    ISBN-10: 0132662418 • ISBN-13: 9780132662413
    ©2012 • Online • Live
    More info
    1. Lab Manual (ZIP) (5.4MB)
  2. Lab Manual Solutions for Java Software Solutions: Foundations of Program Design, 7/E
    Lewis
    ISBN-10: 0132662396 • ISBN-13: 9780132662390
    ©2012 • Online • Live
    More info
    1. Lab Manual Solutions (ZIP) (0.2MB)
  3. PowerPoint Slides for Java Software Solutions: Foundations of Program Design, 7/E
    Lewis
    ISBN-10: 0132662442 • ISBN-13: 9780132662444
    ©2012 • Online • Live
    More info
    1. PowerPoint Slides (ZIP) (11.3MB)
  4. Solution Manual (online only) forJava Software Solutions: Foundations of Program Design, 7/E
    Lewis
    ISBN-10: 013266240X • ISBN-13: 9780132662406
    ©2012 • Online • Live
    More info
    1. Exercise Solutions (ZIP) (4.0MB)
      These are the solutions for the end of chapter exercises
    2. Programming Problem Solutions (ZIP) (2.1MB)
      These are the solutions for the programming problems
  5. Source Code for Java Software Solutions: Foundations of Program Design, 7/E
    Lewis
    ISBN-10: 0132662426 • ISBN-13: 9780132662420
    ©2012 • Online • Live
    More info
    1. Source Code (ZIP) (1.7MB)
    2. Source Code for the PaintBox Project (ZIP) (0.1MB)
      Available for Download
  6. Test Bank for Java Software Solutions: Foundations of Program Design, 7/E
    Lewis
    ISBN-10: 0132662434 • ISBN-13: 9780132662437
    ©2012 • Online • Live
    More info
    1. Test Bank (ZIP) (0.4MB)
      Available for Download
  7. TestGen for Java Software Solutions: Foundations of Program Design, 7/E
    Lewis
    ISBN-10: 0132577801 • ISBN-13: 9780132577809
    ©2012 • Online • Live
    More info
      Please note:This testbank file must be used in conjunction with Pearson's TestGen application. Go to the TestGen website to download software, upgrade, and access "getting started" TestGen resources.

    1. TestGen Testbank file - MAC (SIT) (1.4MB)
      Compressed file contains testbank files produced for TestGen version 7.4. TestGen test generator software is required to work with this testbank. You can download the latest version by clicking on the "Help downloading Instructor Resources" link
    2. TestGen Testbank file - PC (ZIP) (1.7MB)
      Compressed file contains testbank files produced for TestGen version 7.4. TestGen test generator software is required to work with this testbank. You can download the latest version by clicking on the "Help downloading Instructor Resources" link
    3. Blackboard (ZIP) (0.8MB)
      The standalone BB pool


Java Software Solutions, 7th Edition Exercise Solutions, Ch. 2
Chapter 2 Exercise Solutions
EX 2.1. Explain the following programming statement in terms of objects and the
services they provide.
System.out.println ("I gotta be me!");
The System.out object has a println method which accepts a string, enclosed in
parentheses and quotation marks, which it displays on the monitor.
EX 2.2. What output is produced by the following code fragment? Explain.
System.out.print ("Here we go!");
System.out.println ("12345");
System.out.print ("Test this if you are not sure.");
System.out.print ("Another.");
System.out.println ();
System.out.println ("All done.");
The output produced is:
Here we go!12345
Test this if you are not sure.Another.
All done.
After printing its data, the println method moves to the next line of output, whereas the
print method does not. A println statement with no data has the effect of moving down
to the next line.
EX 2.3. What is wrong with the following program statement? How can it be fixed?
System.out.println ("To be or not to be, that
is the question.");
The string to be printed is not all on one line. The problem can be fixed by using the
string concatenation operator (+) or by using a print statement for part of the string and
a println statement for the remainder of the string.
EX 2.4. What output is produced by the following statement? Explain.
System.out.println ("50 plus 25 is " + 50 + 25);
The output produced is:
50 plus 25 is 5025
First the string “50 plus 25” is concatenated with the integer 50; since one of the
operands associated with the “+” operator is a string, the result is a string. Then the
string “50 plus 25 is 50” is concatenated with the integer 25, with similar results.
EX 2.5. What is the output produced by the following statement? Explain.
System.out.println ("He thrusts his fists\n\tagainst" +
" the post\nand still insists\n\the sees the \"ghost\"");
The output produced is:
He thrusts his fists
against the post
and still insists
he sees the "ghost"
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Solutions, 7th Edition Exercise Solutions, Ch. 2
Escape characters are used to go to the beginning of new lines (\n), to tab (\t), and to
print quotation marks (\").
EX 2.6. What value is contained in the integer variable size after the following
statements are executed?
size = 18;
size = size + 12;
size = size * 2;
size = size / 4;
The final value stored in size is 15.
EX 2.7. What value is contained in the floating point variable depth after the following
statements are executed?
depth = 2.4;
depth = 20 – depth * 4;
depth = depth / 5;
The final value stored in depth is 2.08.
EX 2.8. What value is contained in the integer variable length after the following
statements are executed?
length = 5;
length *= 2;
length *= length;
length /= 100;
The final value stored in length is 1.
EX 2.9. Write four different program statements that increment the value of an integer
variable total.
total = total + 1;
total += 1;
total++;
++total;
EX 2.10. Given the following declarations, what result is stored in each of the listed
assignment statements?
int iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5;
double fResult, val1 = 17.0, val2 = 12.78;
a. iResult = num1 / num4;
iResult is assigned 5
b. fResult = num1 / num4;
fResult is assigned 5.0
c. iResult = num3 / num4;
iResult is assigned 3
d. fResult = num3 / num4;
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Solutions, 7th Edition Exercise Solutions, Ch. 2
fResult is assigned 3.0
e. fResult = val1 / num4;
fResult is assigned 3.4
f. fResult = val1 / val2;
fResult is assigned 1.3302034...
g. iResult = num1 / num2;
iResult is assigned 0
h. fResult = (double) num1 / num2;
fResult is assigned 0.625
i. fResult = num1 / (double) num2;
fResult is assigned 0.625
j. fResult = (double) (num1 / num2);
fResult is assigned 0.0
k. iResult = (int) (val1 / num4);
iResult is assigned 3
l. fResult = (int) (val1 / num4);
fResult is assigned 3.0
m. fResult = (int) ((double) num1 / num2);
fResult is assigned 0.0
n. iResult = num3 % num4;
iResult is assigned 2
o. iResult = num 2 % num3;
iResult is assigned 6
p. iResult = num3 % num2;
iResult is assigned 17
q. iResult = num2 % num4;
iResult is assigned 0
EX 2.11. For each of the following expressions, indicate the order in which the operators
will be evaluated by writing a number beneath each operator.
a. a – b – c – d
1 2 3
b. a – b + c – d
1 2 3
c. a + b / c / d
3 1 2
d. a + b / c * d
3 1 2
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Solutions, 7th Edition Exercise Solutions, Ch. 2
e. a / b * c * d
1 2 3
f. a % b / c * d
1 2 3
g. a % b % c % d
1 2 3
h. a – (b – c) – d
2 1 3
i. (a – (b – c)) – d
2 1 3
j. a – ((b – c) – d)
3 1 2
k. a % (b % c) * d * e
2 1 3 4
l. a + (b – c) * d – e
3 1 2 4
m. (a + b) * c + d * e
1 2 4 3
n. (a + b) * (c / d) % e
1 3 2 4
EX 2.12. Explain the role played by the World Wide Web in the translation and execution
of some Java programs.
Java applets are designed to be moved across the web and executed in a browser.
The applet source code is compiled into Java bytecode which is sent across the
Internet using HMTL and the World Wide Web software. A Web browser with a Java
interpreter interprets and execute the bytecode.
EX 2.13. Compare and contrast a traditional coordinate system and the coordinate system
used by Java graphical components.
A traditional coordinate system has the origin in the lower-left corner, with x increasing
to the right and y increasing upward. The coordinate system used by Java has the
origin in the upper-left corner with x increasing to the right and y increasing downward.
EX 2.14. How many bits are needed to store a color picture that is 400 pixels wide and 250
pixels high? Assume color is represented using the RGB technique described in
this chapter and that no special compression is done.
Allowing 8 bits for each of the 3 (red, green, and blue) color components of each of 400
x 250 pixels comprising the picture means that 8 x 3 x 400 x 250 = 2,400,000 bits are
needed.
EX 2.15. Assuming you have a Graphics object called page, write a statement that will
draw a line from point (20, 30) to point (50, 60).
page.drawLine(20,30,50,60);
©

Java Software Solutions, 7e (Lewis/Loftus)
Chapter 3   Using Classes and Objects

3.1   Multiple-Choice Questions

1) In Java a variable may contain
A) a value or a reference
B) a package
C) a method
D) a class
E) any of the above
Answer:  A
Explanation:  A) Java variables contain values or references to instances of classes (which contain values and/or additional references).

2) If two variables contain aliases of the same object then
A) the object may be modified using either alias
B) the object cannot be modified unless there's but a single reference to it
C) a third alias is created if/when the object is modified
D) the object will become an "orphan" if both variables are set to null
E) answers A and D are correct
Answer:  E
Explanation:  E) By definition, a alias provides a means by which an object can be modified (an alias is like a pointer).  If both variables are set to null, then the object is not referenced by any variable (via any alias) and it does, indeed, become an "orphan"and it will be garbage collected at some point in the future.

3) Which properties are true of String objects?
A) Their lengths never change
B) The shortest string has zero length
C) Individual characters within a String may be changed using the replace method
D) The index of the first character in a string is one
E) Only A and B are true
Answer:  E
Explanation:  E) Strings are immutable.  That means that once a string object is created it cannot be changed.  Therefore the length of a string never changes once it has been created.  The shortest length string is ""there are no characters between the quotes, so the length is zero.  The replace method allows you to create a new string from an original one, replacing some of the characters.  The index of the first location in a String is zeronot one.  Also, the last byte of every string contains the end-of-string character which is a byte of low-values, or binary zeros.


4) What happens if you attempt to use a variable before it has been initialized?
A) A syntax error may be generated by the compiler
B) A runtime error may occur during execution
C) A "garbage" or "uninitialized" value will be used in the computation
D) A value of zero is used if a variable has not been initialized
E) Answers A and B are correct
Answer:  E
Explanation:  E) Many times the compiler is able to detect the attempted use of an uninitialized variableit will generate a syntax error in this case.  If such as use escapes detection by the compiler then a runtime error occurs upon usage.  Java is a very "safe" language, so it does not allow "garbage" or zero to be used if an uninitialized variable is used in a computation.

5) What is the function of the dot operator?
A) It serves to separate the integer portion from the fractional portion of a floating point number
B) It allows one to access the data within an object when given a reference to the object
C) It allows one to invoke a method within an object when given a reference to the object
D) It is used to terminate commands (much as a period terminates a sentence in English)
E) Both B and C are correct
Answer:  E
Explanation:  E) The dot operator is appended directly after the object reference, followed by the data or method to which access is desired.  In the case of data, the access may be for reading or writing.  In the case of a method, the access is to allow one to invoke the method.  The dot within a floating point number is a decimal pointnot a dot operator.

6) In Java, "instantiation" means
A) noticing the first time something is used
B) creating a new object of the class
C) creating a new alias to an existing object
D) launching a method
E) none of the above
Answer:  B
Explanation:  B) "Instantiation" means creating a new instance of an object.  This usually is accomplished by using the new operator.  In the case of Strings, new instances (instantiation) may be created just by using quotes in an expression.  For example:  String s;  s = "A new string (instance)";

7) In the StringMutation program shown in Listing 3.1, if phrase is initialized to "Einstein" what will Mutation #3 yield if Mutation #1:  mutation1 = phrase.concat(".")?
A) Einstein.
B) EINSTEIN.
C) XINSTXIN.
D) einstein.
E) xinstxin.
Answer:  C
Explanation:  C) Mutation #2 changes every letter to upper case.  Then Mutation #3 replaces Es with Xs.
8) Say you write a program that makes use of the Random class, but you fail to include an import statement for java.util.Random (or java.util.*).  What will happen when you attempt to compile and run your program.
A) The program won't run, but it will compile with a warning about the missing class.
B) The program won't compile-you'll receive a syntax error about the missing class.
C) The program will compile, but you'll receive a warning about the missing class.
D) The program will encounter a runtime error when it attempts to access any member of the Random class
E) none of the above
Answer:  B
Explanation:  B) The missing class means that there will be undefined variables and/or methods.  The compiler will detect these and will issue error messages.  Your program won't be executable.

9) Which of the following will yield a pseudorandom number in the range [ -5, +5 ) given the following:
               Random gen = new Random( );
A) gen.nextFloat( ) * 5
B) gen.nextFloat( ) * 10 - 5
C) gen.nextFloat( ) * 5 - 10
D) gen.nextInt( ) * 10 - 5
E) gen.nextInt(10) - 5
Answer:  B
Explanation:  B) nextFloat yields a pseudorandom number in the range [ 0, 1 ); multiplying by 10 yields numbers in the range [ 0, 10 ); subtracting 5 yields numbers in the range [ -5, 5 ).

10) What will be displayed by this command:  System.out.println(Math.pow(3, 3-1));
A) 9
B) 8
C) 6
D) 4
E) 27
Answer:  A
Explanation:  A) This evaluates to Math.pow(3, 2) which is 32 which is 9.

11) Consider the following two lines of code.  What can you say about s1 and s2?
         String s1 = "testing" + "123";
         String s2 = new String("testing 123");
A) s1 and s2 are both references to the same String object
B) the line declaring s2 is legal Java; the line declaring s1 will produce a syntax error
C) s1 and s2 are both references to different String objects
D) s1 and s2 will compare "equal"
E) none of the above
Answer:  C
Explanation:  C) Both declarations are legal Java.  s1 is a String reference, and it is initialized to the String "testing123".  s2 is a String reference, and it is initialized to the String "testing 123".  Note the space between the "testing" and the "123".  So the two Strings are not equal.
12) An "alias" is when
A) two different reference variables refer to the same physical object
B) two different numeric variables refer to the same physical object
C) two different numeric variables contain identical values
D) two variables have the same names
E) none of the above
Answer:  A
Explanation:  A) An "alias" occurs when there are two or more references to the same physical object so that by following either reference, one can read/write/modify the object.

13) The String class' compareTo method
A) compares two string in a case-independent manner
B) yields true or false
C) yields 0 if the two strings are identical
D) returns 1 if the first string comes lexically before the second string
E) none of the above
Answer:  C
Explanation:  C) compareTo yields 0 if the two strings are identical, -1 if the first string comes lexically before the second, +1 if the first string comes lexically after the second.

14) Given the following code fragment
       String strA = "aBcDeFg";
       String strB = strA.toLowerCase( );
       strB = strB.toUpperCase( );
       String strC = strA.toUpperCase( );
A) strB.equals(strC) would be true
B) strB.compareTo(strC) would yield 0
C) strA.compareTo(strC) would yield 0
D) strA.equals(strC) would be true
E) none of the above
Answer:  B
Explanation:  B) strB contains the uppercase forms of all the letters in strA; so does strC.  So compareTo would yield 0, indicating equality.

15) An API is
A) an Abstract Programming Interface
B) an Application Programmer's Interface
C) an Application Programming Interface
D) an Abstract Programmer's Interface
E) an Absolute Programming Interface
Answer:  C
Explanation:  C) An API is an Application Programming Interfacea collection of related classes that have been built for specific purposes.  They usually reside in a class library.


16) The advantage(s) of the Random class' pseudo-random number generators, compared to the Math.random method, is that
A) you may create several random number generators
B) the generators in Random are more efficient than the one in Math.random
C) you can generate random ints, floats, and ints within a range
D) you can initialize and reinitialize Random generators
E) all but answer B
Answer:  E
Explanation:  E) The efficiency of all the random number generators are the same.  The advantages of Random generators over Math.random include all the other properties.

17) Java.text's NumberFormat class includes methods that
A) allow you to format currency
B) allow you to format percentages
C) round their display during the formatting process
D) truncate their display during the formatting process
E) A, B, C, but not D
Answer:  E
Explanation:  E) NumberFormat always rounds; it never truncates.  And, it does provide methods for both currency and percentages.

18) The advantages of the DecimalFormat class compared with the NumberFormat class include
A) precise control over the number of digits to be displayed
B) control over the presence of a leading zero
C) the ability to truncate values rather than to round them
D) the ability to display a % automatically at the beginning of the display
E) only A and B
Answer:  E
Explanation:  E) While DecimalFormat does provide far more control than NumberFormat, truncation remains in the hands of the programmervia one or more of the Math methods.  The % symbol would appear at the end of the displaynot the beginning.

19) Consider the following enumeration
       enum Speed { FAST, MEDIUM, SLOW };
A) The ordinal value of MEDIUM is 2
B) The ordinal value of SLOW is 1
C) The name of the Speed enumeration whose ordinal value is zero is FAST
D) The name of the Speed enumeration whose ordinal value is one is SLOW
E) None of the above
Answer:  C
Explanation:  C) Enumerations have ordinal values beginning at zero, in left-to-right ascending order.  So, Speed.FAST.ordinal( ) is zero, Speed.MEDIUM.ordinal( ) is one, and Speed.SLOW.ordinal( ) is two.


20) What is the advantage of putting an image in a JLabel instance?
A) It becomes part of the component and is laid out automatically
B) It becomes part of the container and is automatically scaled
C) Although it becomes part of the instance, it still must be drawn separately
D) The runtime efficiency is greatly improved
E) None of the above
Answer:  A
Explanation:  A) Once an image becomes part of the JLabel instance, all the normal Java drawing mechanisms come into play.  The image is laid out automatically when the JLabel instance is drawn, with no further actions required of the programmer.

21) What is the function of a frame's pack method?
A) It deletes unused storage
B) It forces orphan objects to be reclaimed by the garbage collector
C) It sets the size appropriately for display
D) It forces the frame contents to be up-left justified
E) None of the above
Answer:  C
Explanation:  C) The pack method of a frame sets its size appropriately based on its contentsso that it will display correctly, with minimal or no extraneous spaces or margins.

22) Layout managers are associated with
A) objects
B) interfaces
C) classes
D) containers
E) none of the above
Answer:  D
Explanation:  D) Every container has an associated layout manager that controls how the contents within the container will be laid out when being displayed.

23) A containment hierarchy is
A) a collection of relationships among panels, all at the same level
B) a nested collection of relationships among containers
C) a nested collection of relationships among applets
D) a collection of relationships among frames, usually at different levels
E) none of the above
Answer:  B
Explanation:  B) A containment hierarchy is the relationship among a nested collection of containers, which is used to achieve a desired visual effect.  The containers almost never are all at the same levelthat's why the term "hierarchy" is used.


24) The main difference between a frame and a panel is
A) a panel can have a title bar; a frame cannot
B) neither frames nor panels can be nested, but frames can be contained within panels
C) frames can have a title bar; panels cannot
D) frames can be nested; panels cannot
E) none of the above
Answer:  C
Explanation:  C) Frames are displayed as separate windows, each with an optional title bar.  Panels cannot have a title bar.

25) The Swing package
A) completely replaces the AWT
B) partially replaces the AWT
C) is complementary to the AWT
D) has no relationship to the AWT
E) none of the above
Answer:  C
Explanation:  C) The Swing package includes a large amount of functionality in addition to that provided by the AWT, but it does not replace the AWT.  The AWT is still required for much fundamental basic functionality.  The best answer is that the Swing package complements the AWT, and extends its functionality.

26) Autoboxing is
A) the automatic conversion of a wrapper object to/from its corresponding primitive type
B) the automatic enclosure of a graphics object within a bounding box
C) the automatic widening of ints into floats or doubles, as required
D) the automatic conversion of an enumeration into its numeric representation
E) none of the above
Answer:  A
Explanation:  A) Boxing is the enclosure of a primitive type by a wrapper object.  Autoboxing occurs when the data within wrapped object is automatically extracted or when primitive data is automatically wrapped.

27) In addition to their usage providing a mechanism to convert (to box) primitive data into objects, what else do the wrapper classes provide?
A) enumerations
B) static constants
C) arrays to contain the data
D) exceptions
E) none of the above
Answer:  B
Explanation:  B) The wrapper classes also provide static constants, like MIN_VALUE and MAX_VALUE (the smallest and largest ints).


28) A JPanel can be added to a JFrame to form a GUI.  In order to place other GUI components such as JLabels and Buttons into the JFrame, which of the following messages is passed to the JPanel?
A) insert
B) include
C) get
D) getContentPane
E) add
Answer:  E
Explanation:  E) Components can be added to a JPanel and then the JPanel can be added to the JFrame.  The message add is used to add to a JPanel.  The message get.ContentPane.add is used to add to a JFrame.

29) Which of the following GUI components is used to accept input into a JFrame?
A) JLabel
B) JPanel
C) JInput
D) JTextField
E) JInputField
Answer:  D
Explanation:  D) The JTextField accepts any input (numbers, Strings, etc).  There is no such component as a JInput or JInputField while the JLabel is used for output, not input, and a JPanel is a collection of GUI components.

30) JOptionPane is a class that provides GUI
A) dialog boxes
B) buttons
C) output fields
D) panels and frames
E) all of the above
Answer:  A
Explanation:  A) the JOptionPane class contains three formats of dialog boxes, an input dialog box that prompts the user and inputs String text, a message dialog box that outputs a message, and a confirm box that prompts the user and accepts a Yes or No answer.



3.2   True/False Questions

1) Only difficult programming problems require a pseudocode solution before the programmer creates the implementation (program) itself.
Answer:  FALSE
Explanation:  Some form of design should be created for all problems before the program is written, with the possible exception of only the most trivial of problems.  The form of the design may vary.  For instance, programmers in the 1960s and 1970s often used flow charts instead of pseudocode.

2) You may apply the prefix and postfix increment and decrement operators to instances of the Integer class.
Answer:  FALSE
Explanation:  These operators may only be applied to numeric data.  An instance of a class is an objectnot a piece of primitive data.
3) In Java, the symbol "=" and the symbol "==" are used synonymously (interchangeably).
Answer:  FALSE
Explanation:  "=" is used for assignment statements while "==" is used to test equality in conditional statements.

4) When comparing any primitive type of variable, == should always be used to test to see if two values are equal.
Answer:  FALSE
Explanation:  This is true of int, short, byte, long, char and boolean, but not of double or float variables.  If two double variables, x and y, are being tested, (x == y) is true only if they are exactly equal to the last decimal point.  It is common instead to compare these two values but allow for a small difference in value.  For instance, if THETA = 0.000001, we might test x and y by (x - y <= THETA) instead of (x == y) to get a better idea of if they are close enough to be considered equal.

5) These two ways of setting up a string yield identical results:
         a)  String string = new String("string");
         b)  String string = "string";
Answer:  TRUE
Explanation:  Java has a very strong notion about what Strings are and how they function.  The second declaration is merely "shorthand" for the first.  The string reference will be initialized identically by both declarations.

6) These two ways of setting up a String yield identical results:
         a) String string = "12345"
         b) String string = 12345;
Answer:  FALSE
Explanation:  In fact, the second statement won't even compile!  The second statement will receive a syntax error about incompatible types.  There is no automatic conversion from a number to a String.

7) These two ways of setting up a String yield identical results:
         a) String string = new String("123.45");
         b) String string = "" + 123.45;
Answer:  TRUE
Explanation:  Java understands the + operator when used to combine Strings with numbers means that the number should be converted into a numeric String, and then concatenation should occur.

8) These two ways of setting up a String yield identical results:
         a) String string = new String(12345);
         b) String string = new String("12345");
Answer:  FALSE
Explanation:  In fact, the first statement won't even compile!  There is no overloaded version of the String constructor that accepts a numeric argument.

9) System.out.println("123" + 4) will display the value 127.
Answer:  FALSE
Explanation:  "123" is a String.  So, the + will cause String concatenation to occur.  The int 4 will be converted into the String "4", and the String 1234 will be created and displayed.
10) You may use the String replace( ) method to remove characters from a String.
Answer:  FALSE
Explanation:  The replace( ) method only replaces single characters with other single characters.  The replace( ) method will not add or delete characters to a String; the String length will remain the same.

11) If you need to import not only the top-level of a package, but all its secondary levels as well, you should write:  import package.*.*;
Answer:  FALSE
Explanation:  The import statement can only be used with a single * (wild card).  If you need to import all the secondary levels of a package as well, you must write them out explicitly:
      import package.A.*;
      import package.B.*;
            ...

12) The Random class' setSeed( ) method allows you to restart the pseudo-random number sequence.
Answer:  TRUE
Explanation:  You can specify the initial seed for a Random instance either by using the Random(long seed) constructor, or once you have an instance, say Random random = new Random( );, then you may re-initialize the seed by using  random.setSeed(long seed);

13) All the methods in the Math class are declared to be static.
Answer:  TRUE
Explanation:  The Math class methods are designed to be generally useful in arithmetic expressions, so no instances of anything are required to make use of them.  This is achieved by ensuring that all the Math methods are static.

14) The printf method within System.out is designed to ease the conversion of legacy C code into Java.
Answer:  TRUE
Explanation:  C programs use the C printf function for output.  Java's printf method is modeled closely after the C printf function, so C output statements can be translated into Java extremely easily.

15) The names of the wrapper classes are just the names of the primitive data types, but with an initial capital letter.
Answer:  FALSE
Explanation:  This is true for most of the wrapper classes, but it is false for int (Integer) and char (Character).
3.3   Free-Form Questions

1) Rewrite the following five assignment statements into a single statement using prefix and postfix increment and decrement operators as necessary.  Assume all variables are int variables.
               x = y + 1;
               y = y + 1;
               z = z - 1;
               x = x - z;
               x = x + 1;
Answer:  x = (y++ + 1) - (--z) + 1;

2) Write the code to define the JFrame which will have the components placed into the JPanel first where the JPanel will have the inputLabel and ageField side by side and the outputLabel and messageLabel beneath them.  Do not worry about making the JFrame do anything (i.e., don't implement an ActionListener).
Answer:  frame = new JFrame ("Age GUI");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
inputLabel = new JLabel ("Enter your age:");
outputLabel = new JLabel ("Here is a message about your age");
messageLabel = new JLabel ("");
ageField = new JTextField (3);
panel = new JPanel( );
panel.setPreferredSize (new Dimension(2, 2));
panel.setBackground (Color.white);
panel.add (inputLabel);
panel.add (ageField);
panel.add (outputLabel);
panel.add (messageLabel);
frame.getContentPane( ).add (panel);
frame.pack( );
frame.show( );



3) Consider the condition (x == y).  How is this handled if x and y are primitive types?  How is this handled if x and y are objects?
Answer:  If x and y are primitive types, then the values stored in x and y are compared and true is returned if the two values are equal.  If x and y are objects, then the object that x and y reference are compared.  If they are the same object, it returns true, otherwise it returns false.

4) Given two String variables, s1 and s2, is it possible for (s1 != s2) to be true while (s1.equals(s2)) is also true?  Why or why not?
Answer:  The condition (s1 != s2) means that s1 and s2 are references to two distinct Objects.  It says nothing about the contents of the referenced Objects.  So, the contents of s1 may well be identical or different from the contents of s2.  Therefore it is quite possible that (s1.equals(s2)) is true while (s1 != s2) is also true.

5) Why does one set up a containment hierarchy?
Answer:  It is common to use multiple layers of nested panels to organize and group components in various waysthis is called a containment hierarchy.  It allows you to create sophisticated layouts that scale (or do not scale) appropriately.
6) What is the purpose of the ImageIcon class?
Answer:  The ImageIcon class is used to represent an image that is included in a label.  The ImageIcon constructor takes the name of an image file (which may contain a JPEG, GIF, or PNG image) and loads it into the object.

7) What is a wrapper class?  Why are they useful?
Answer:  A wrapper class is a class that allows you to embed one piece of primitive data within an Object.  There are methods for extracting the data from the Object.  Wrapper classes are useful where you have methods (or constructors) that only accept Objectsnot primitive data.  By wrapping the primitive data within an Object the functionality of the method (and its class) may be accessed.

8) What is the difference between a heavyweight and a lightweight container?
Answer:  A heavyweight container is one that is managed by the underlying operating system on which the program is run.  A lightweight container is managed by the Java environment (the packages and library routines that Java provides).

9) What is autoboxing?
Answer:  Autoboxing provides automatic conversions between primitive values and corresponding wrapper objects.  It makes your code shorter by relieving you of the need to provide explicit wrapping of primitive values and explicit extraction of primitive values.

For the questions below: Assume an interactive Java program which asks the user for their first name and last name, and outputs the user's initials.

10) For the program to get a name interactively a Scanner object must be instantiated. Write the Java statement to do this.
Answer:  Scanner scan = new Scanner(System.in);

11) Write a statement using a Scanner method to get the first name interactively.
Answer:  firstName = scan.nextLine( );

12) Write a method to extract the initial from the first name.
Answer:  firstInitial = firstName.substring(0,1);

13) Write a statement using a method to guarantee that the initial will be a capital letter.
Answer:  firstInitial = firstInitial.toUpperCase( ); 

No comments:

Post a Comment

Linkwithin

Related Posts Plugin for WordPress, Blogger...