TEST BANK Annotated Solution Guide for Thinking in Java 4th Edition By Bruce Eckel and Ervin Varga
- GradeMaster1
- Rating : 1
- Grade : C+
- Questions : 0
- Solutions : 1124
- Blog : 0
- Earned : $278.60
Detailed instructions for installing, configuring and testing the
source code.
These instructions describe a Windows installation, but they will also act as a
guide for OSX and Linux installations.
These instructions also work with the free demo version of the guide.
1. Create a directory called C:\TIJ4-Solutions\code.
2. Using WinZip or some other zip utility (if one is not preinstalled, search
the web for a free utility), extract the zip file containing the code that you
received when you purchased the guide. Unzip it into the
C:\TIJ4-Solutions\code directory. When you’re done, you should see
numerous subdirectories in the C:\TIJ4-Solutions\code directory,
including subdirectories corresponding to the chapters in the solution
guide.
3. Install the Java SE Development Kit (JDK), version 5 or newer, from the
download site at Sun (http://java.sun.com/javase/downloads/index.jsp).
You’ll also eventually want the documentation, which is available from the
same site.
4. Set the CLASSPATH in your computer’s environment. For Windows
machines, right-click on the “My Computer” icon and select “Properties.”
Then select the “Advanced” tab and click the “Environment Variables”
button at the bottom. Under “System Variables,” look to see if there’s
already a “CLASSPATH” variable. If there is, double click it and add
;.;..;C:\TIJ4-Solutions\code;
to the end of the current entry.
If there is no “CLASSPATH” variable, click the “New” button and enter
CLASSPATH
In the “Variable name” box, and
.;..;C:\TIJ4-Solutions\code;
In the “Variable value” box, then click “OK”. To verify that your classpath
has been set, start a command prompt (see below), then enter set and
look for the CLASSPATH information in the output.
5. Using the same technique as in Step 4, but for PATH instead of
CLASSPATH, add the bin directory from your Java installation into your
2 Thinking in Java, 4th Edition Annotated Solution Guide
system’s PATH environment variable. On Windows, the default JDK
installation path is under “C:\Program Files” and because this has
spaces, you must quote that directory when adding it to the PATH:
C:\"Program Files"\Java\bin;
6. Create a directory called C:\jars. Place the following files into this
directory:
• javassist.jar (download here:
http://sourceforge.net/project/showfiles.php?group_id=22866; you
may need to search for it).
• swt.jar from the Eclipse SWT library
(http://download.eclipse.org/eclipse/downloads/). Click on the most
recent build number, then scroll down to “SWT Binary and Source”
and select the file corresponding to your platform. Further details
about finding the jar file are in Thinking in Java, 4th Edition, under
the heading “Installing SWT.”
• tools.jar, which is actually part of the JDK, but you must explicitly
add it to your classpath. You’ll find it in the lib directory wherever you
installed the JDK on your machine. (The default is C:\"Program
Files"\Java\lib).
• javaws.jar, also part of the JDK, in the /jre/lib/ directory.
• xom.jar, available from http://www.cafeconleche.org/XOM/.
7. You must explicitly add each of the Jar files to your CLASSPATH,
following the directions in Step 4. However, you must also include the
name of the Jar file in the CLASSPATH entry. For example, after you put
the javassist.jar file into the C:\jars\ directory, the associated
CLASSPATH entry is C:\jars\javassist.jar;.
8. Install the Ant 1.7 (or newer) build tool by following the instructions you
will find in the Ant download at http://ant.apache.org/.
Note: Ant is required in order to compile the examples in the book. Once
you successfully run ‘ant build’ in the root directory, you can also
compile each example individually (once you have the CLASSPATH set, as
described in Step 4) using the javac command-line compiler that was
installed when you completed the steps 3 and 5. To compile a file called
MyProgram.java, you type javac MyProgram.java.
9. Start a command prompt in the C:\TIJ4- Solutions\code directory. To
do this in Windows, press the “Start” button, then select “Run” and type
“cmd” and press “OK.” then type
Installing the Code 3
cd C:\TIJ4-Solutions\code
into the resulting command window.
10. At the prompt, type
ant build
The build should successfully compile all the chapters in the solution
guide.
11. Once you’ve run ant build in the root directory, you can also move into
individual chapters and type ant (to compile and execute the code in that
chapter) or ant build (to compile the code only).
12. This code is designed to work without an IDE, but it has also been tested
with Eclipse (free at http://www.eclipse.org/); see the following section
for instructions on how to use the code with Eclipse.
If you want to use this code inside other IDEs you might need to make
appropriate adjustments. Different IDEs have different requirements and
you might find it’s more trouble than it’s worth right now; instead, you
may want to begin with a more basic editor like JEdit (free at
http://www.jedit.org/).
13. Note: The output for the programs has been verified for Java 6. Certain
programs (primarily those that use hashing) can produce different output
from one version to the next.
Using Eclipse
Once you’ve followed the above instructions, you can use the code inside the
Eclipse development environment as follows:
1. Install Eclipse from http://www.eclipse.org/downloads/; choose a version
for Java developers and follow the installation instructions.
2. Start Eclipse, then choose File | New | Java Project from the main menu.
3. In the ensuing dialog box, under “Contents,” select “Create Project from
Existing Source.” Press the “Browse” button and navigate to C:\TIJ4-
Solutions\code. Enter “TIJ4-Solutions” as the project name and press
the “Finish” button.
Note: If you are installing the demo version of the solution guide, you do
not need to perform any of the following steps.
4 Thinking in Java, 4th Edition Annotated Solution Guide
4. Eclipse will work for awhile and then present you with a “problems” pane
containing a lot of errors and warnings. We’ll remove the errors in the
following steps.
5. In the “Package Explorer” pane, right click on “TIJ4-Solutions” and select
“Properties” (at the bottom). In the left column of the ensuing dialog box,
select “Java Build Path.”
6. Select the “Source” tab. The default Eclipse configuration may have
chosen to exclude and include some files. Click on “Included” and press
the “Remove” button, then click on “Excluded” and press “Remove.”
7. Click on “Excluded” and press “Edit.” Under “Exclusion Patterns,” add the
following files, which are not intended to compile. After you add the files,
press the “Finish” button.
• access/E04_ForeignClass.java
• arrays/E11_AutoboxingWithArrays.java
• interfaces/E02_Abstract.java
• reusing/E06_ChessWithoutDefCtor.java
• reusing/E20_OverrideAnnotation.java
• reusing/E21_FinalMethod.java
• reusing/E22_FinalClass.java
8. Click on the “Libraries” tab, then the “Add External Jars” button. Add
tools.jar, swt.jar, javassist.jar and xom.jar that are described in step
6 of the previous section.
9. When you press OK to close the dialog box, the project should rebuild
without any errors. The warnings that you see refer to code that is
intentional for those solutions, in order to demonstrate features and
issues of the language.
5
Packages & IDEs
When Java first appeared there was no Integrated
Development Environment (IDE) support, so you typically
used a text editor and the command-line compiler. Over the
years, IDE support has gotten so good (and many prevalent
IDEs are free) that it’s less and less likely that you’ll develop in
Java – or even learn the language – without an IDE.
There’s a conflict, however, between IDEs and the way that Thinking in Java
attempts to teach the language: One step at a time, using language features only
after they’ve been introduced.
An IDE like Eclipse (from www.Eclipse.org) likes to have all its code in packages
(later versions have become more tolerant of unpackaged code, but it still prefers
packages). Packages, however, are not introduced until the Access Control
chapter.
Because of the prevalence of IDEs, we have chosen to include package
statements for all the code in this book, even for chapters before Access Control.
If you have solved the problems in those chapters without using package
statements, your solutions are still correct.
7
Left to the Reader
We have left only a few exercises to the reader. For these, the solution typically
requires some configuration on your own computer.
The exercises left to the reader include:
• Exercises 12 & 13 from the chapter Everything Is an Object
• Exercise 13 from the chapter Initialization & Cleanup
• Exercise 2 from the chapter Access Control
• Exercise 15 from the chapter Generics
• Exercise 8 from the chapter Arrays
• Exercise 21 from the chapter Containers in Depth
• Exercise 35, 36, 38, 39, and 43 from the chapter Graphical User Interfaces
9
Everything is an
Object
To satisfy IDEs like Eclipse, we have included package statements for chapters
before Access Control. If you have solved the problems in this chapter without
using package statements, your solutions are still correct.
Exercise 1
//: object/E01_DefaultInitialization.java
/****************** Exercise 1 *****************
* Create a class containing an int and a char
* that are not initialized. Print their values
* to verify that Java performs default
* initialization.
***********************************************/
package object;
public class E01_DefaultInitialization {
int i;
char c;
public E01_DefaultInitialization() {
System.out.println("i = " + i);
System.out.println("c = [" + c + ']');
}
public static void main(String[] args) {
new E01_DefaultInitialization();
}
} /* Output:
i = 0
c = [ ]
*///:~
When you run the program you’ll see that both variables are given default
values: 0 for the int, and a “space” for the char.
Exercise 2
//: object/E02_HelloWorld.java
10 Thinking in Java, 4th Edition Annotated Solution Guide
/****************** Exercise 2 ******************
* Follow the HelloDate.java example in this
* chapter to create a "hello, world" program that
* simply displays that statement. You need only a
* single method in your class (the "main" one that
* executes when the program starts). Remember
* to make it static and to include the argument
* list (even though you don't use it).
* Compile the program with javac and run it using
* java. If you are using a different development
* environment than the JDK, learn how to compile
* and run programs in that environment.
************************************************/
package object;
public class E02_HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
} /* Output:
Hello, world!
*///:~
Exercise 3
//: object/E03_ATypeName.java
/****************** Exercise 3 ******************
* Turn the code fragments involving ATypeName
* into a program that
[Solved] TEST BANK Annotated Solution Guide for Thinking in Java 4th Edition By Bruce Eckel and Ervin Varga
- This solution is not purchased yet.
- Submitted On 11 Nov, 2021 10:01:49
- GradeMaster1
- Rating : 1
- Grade : C+
- Questions : 0
- Solutions : 1124
- Blog : 0
- Earned : $278.60