FREQUENTLY ASKED QUESTIONS
This document contains answers to some commonly asked questions.
|
|
When will the book Introduction to Computer Science be published? That's a good question, but we still have a lot of work to do. It will be published by Addison Wesley.
Why Java? Why not C or C++ or C# or Python? The program that we are writing are very similar to their counterparts in several other languages, so our choice of language is not crucial. We use Java because it is widely available, widely used, embraces a full set of modern abstractions, and has a variety of automatic checks for mistakes in programs, so it works well for students learning to program. There is no perfect language and you certainly will find yourself programming in other languages in the future.
Do I need to read the textbook in order? The textbook is organized logically by content. However, by design, the chapters are loosely coupled. Chapter 2 (programming fundamentals) is a prerequisite for all future chapters. Chapter 3 (object oriented programming) is a prerequisite for Chatper 4 (data structures) and is assumed in Chapters 8 (systems) and some parts of Chapter 9 (scientific computing).
Do I need previous programming experience? No, this textbook starts from the beginning. If you have prior programming experience, you can move through Chapter 2 at a more rapid pace, but you are likely to find the examples worthy of careful study.
Do I need any special mathematical background? No, we assume only a basic high school background. Sections that require advanced mathematics (e.g., calculus or linear algebra) are clearly marked and are optional.
I discovered what I think is a typo or error. Who should I contact? Eventually you should check the errata list and fill out a bug submission form. In the meantime you can directly email the authors. We appreciate your feedback.
I'm an instructor at another University. Can I use your material in my class? Please email the authors for permission. In general, we are very happy to see our material used at other schools, but we do ask that you make appropriate attributions.
How can I contact the authors? Email Kevin Wayne or Bob Sedgewick.
Can I download the source code all at once? Yes, eventually we will provide jar files that contain the source code for a particular section or chapter. To extract the files, type "jar xf Chapter2.jar" at the command prompt. You must have installed Java for this command to be recognized.
|
|
What version of Java are you using?
Short answer: Java 1.4.2.
Long answer: Java 2 Platform, Standard Edition, version 1.4.2 (J2SE, v.1.4.2).
We do use some features of Java 1.4.2 that are not available in earlier
versions (e.g., regular expressions).
What's Java 2? What's Java 5? The 2 refers to the Java 2 Platform. Confusingly, Java 1.4.2 is considered Java 2. Java 5 is synonymous with Java 1.5. Java 1.5 has been recently released. We'll take advantage of some of the new features (e.g., formatted input) when it becomes widely available.
Can you recommend a good supplemental book on Java? Check out Sun's online Java Tutorial by Mary Campione. Peter van der Linden's Just Java 2 (ISBN 0-13-010534-1) is a wonderful introduction for Java for students who know a little C. Bruce Eckel's Thinking in Java is an excellent online introduction to object oriented programming. Stay away from this stinker, except to read the scathing reviews.
I've programmed in C before. Can you offer any help on transitioning? Here's a comparison table.
Is there a good interactive development environment? Check out Dr. Java.
What's the link for the jdk documentation? Here is documentation for all Java 1.4 classes. If you use Java's libraries, this is an essential reference.
Is there a Java FAQ? Here's a Java FAQ written by Peter van der Linden. Also check out the Java glossary by Roedy Green.
Who designed Java? James Gosling. Here is an interview, where he discusses some of the thoughts that went into including and not including certain featuers of Java.
Is there an official specification of the Java language? Yes, but it's quite technical. Here are links to the Java Language Specification, Second Edition in pdf and html.
Some of the demos are packaged as executable JAR files? How do can I get Windows to execute them automatically when I double-click them? Here are some instructions. You can also type java -jar file.jar at the commmand prompt.
My program runs out of memory, but my system should have more than enough. What's going on? The java heap is the area of RAM reserved for use by the Java virtual machine. You can specifically ask for more (assuming your system has enough) with the following command:
% java -Xmx300MB -Xms300MB PrimeSieve N
In this case, we are asking for 300 MB of memory. The -Xmx sets the maximum heap size, the -Xms flag sets the initial heap size.
My program runs gives me the error message StackOverflowError. What could be wrong? Typically, this is because you have a recursive function that goes into an infinite loop. It could also be caused by a deeply nested recursive function like Sum.java. You can ask for more stack space with
% java -Xss10MB Sum N
However, on Windows (NT, 2000, and XP) this does not appear to work, and appears to be a documented bug with. If you have a work-a-round, we'd be happy to hear about it.
My program crashes and gives me a stack trace, but it doesn't show the line numbers. How can I get the line numbers? Try executing with
% java -Djava.compiler=none HelloWorld
Can I compile by .java file directly into machine language instead of Java bytecode? Yes, if you have gcj type one of the following at the command prompt
% gcj --main=HelloWorld HelloWorld.java % gcj --main=HelloWorld HelloWorld.class
This will create an executable named a.out or HelloWorld.exe depending on your operating system. You can then run the program by typing a.out or HelloWorld instead of java HelloWorld.
What's Open Source software and Free Software? Here's a good description.
Is there an open source version of the Java libraries? Yes, it's called the GNU Classpath. You can also browse the source code repository.
The documentation for the Java language and libraries is really useful. How can I create the same for my code? It's really easy with javadoc. Here's the Javadoc Tool home page.
How do I set the classpath? The classpath is an environment variable which javac and java use to find the location of any needed .class files. This would enable you to put Turtle.class, StdIn.class, and other commonly used libraries and they would always be available. By default, the classpath is the current working directory. If your classpath has already been set, you may need to change it to make it include the current working directory. Here's a wealth of classpath info. It can be rather confusing.
The program links contain syntax-highlighted versions of the code. How was this accomplished? We use GNU source-highlight to automatically produce the syntax-highlighted versions.
How did you create the equations? We used Latex and this TeX to PNG converter.
How did you create the drawing screen shots? Using StdDraw.save of course!
I want to be able to publish reference solutions on the Web, but don't want my students to be able to decompile them. What can I do? Use a Java bytecode obfuscator.
Is it possible for a java application to send a command to the operating system, as in starting a non-java program, application, or script? Yes. Use the method Runtime.exec. However, this drastic step should only be used if absolutely necessary.
What's wrong with the following code for generating a pseudo-random integer between 0 and n-1?
int r = (int) (Math.random() * n); // not recommended!!
It can be slower than necessary since it performs two int to double conversions and a floating point multiply. Also, Math.random gives you no control over the seed, which is useful for debugging. Nevertheless, we use this approach for simplicity, before we introduce objects and Random. Also, if n is very large, the values produced are far from uniformly random, even if Math.random is.
How should I generate a pseudo-random integer between 0 and n-1?
import java.util.Random; ... Random rand = new Random(seed); // or new Random() for a "random" seed rand.nextInt(n);
Note: you should only create one object of type Random and only set the seed once, not one for each random number you need to generate. Unfortunately, the library random number generator is fairly poor so don't use this if it really matters (e.g., cryptography or in a slot machine).
When I compile with javac it says that one of the methods I use is deprecated. What does that mean? A deprecated method is method that was supported in older versions of Java, but is now obsolete. You should use the suggested replacement method instead, since the deprecated method may not be supported in future versions of Java.
How can I read in an integer/double from the keyboard? It's not so easy to do on your own. Use our library In.java.
Is there an equivalent to the printf function in Java? Java 1.5 will contain a method System.out.printf for C and FORTRAN style formatted output. In the meantime, might find this printf for Java.
How do Java programmers usually create formatted output? The following code fragment formats a real number using three decimal digits after the decimal place.
double value = 11.0 / 3.0; System.out.println(value); // 3.6666666666666665 DecimalFormat f = new DecimalFormat("0.000"); String formattedString = f.format(value); System.out.println(formattedString); // 3.667
Does the Java compiler always know to recompile dependent files as needed? Usually, but not always. Be careful with final static constants for primitive types and Strings.
What style should I use when coding in Java? Here are Sun's code conventions for Java programming. Here's a guide to writing unmaintenable code.
Why does (b give weird results when b is a byte? In Java, byte is an 8-bit signed integer. Before the right shift, b is converted to an integer. You may want ((b & 0xff) instead.
Why is the constructor typically declared public? Is there ever a reason
to use a private constructor?
Yes. To store a group of related constants, you might create a class as follows
public class PhysicsConstants {
public static final double GRAVITATION = 6.673E-11; // m^3 / (kg s^2)
public static final double AVOGADRO = 6.02214E+23; // 1 / mol
public static final double BOLTZMANN = 1.38065E-23; // J / K
public static final double LIGHT = 299792458.0; // m / s
private PhysicsConstants() { }
}
in which case you can refer to individual constants by PhysicsConstants.BOLTZMANN. The purpose of this class is solely to bundle the constants together. The private constructor ensures that you can't create an instance of the class. When static import comes to Java 1.5, you will be able to shorten it to BOLTZMANN.
How does Java compare in terms of speed to C or C++? To Perl or Python? The answer depends greatly on the type of application you're running. As a rule of thumb, Java's performance is good, C and C++ are blazingly fast, and Perl/Python are slow. This article claims that Java is comparable to C for scientific applications involving number crunching. My personal, but unscientific, experience is that with higher level objects (Strings, parsing text files, or hash tables) Java is quite a bit slower than C or C++, and quite a bit fasten than Perl or Python.
Does Java have pointers? Java doesn't have raw pointers like C or C++, but it has references which are almost as powerful, but much safer. Here's a good summary of the differences. The Java Virtual Machine itself can implement references with pointers (for raw speed) or with handles (for more flexible memory management).
How do I obtain a list of all the files on my computer? You can use depth-first search. Here is an implementation from Sun.
Is there an industrial strength way to get all of the hyperlinks on a web page? Yes, but it's somewhat complicated. Use Java's HTMLEditorKit library. Here's a reference.
Where can I find toolbar button graphics. Sun suggests using the following Java look and feel buttons.
What's up with layout managers? Layout managers can be confusing at first (especially GridBagLayout). Here's Sun's short course on Layout managers.
For GUI programs, should I use AWT or Swing? Swing is the new and preferred GUI model. It provides more functionality and coolness over AWT.
If I want my GUI demo to animate at a regular rate, what should I do? Use class javax.swing.Timer. Be careful: don't confuse it with java.util.Timer.
Are there are common pitfalls that I should be aware of? Here's a worthwhile list of common Java gotchas.
How do I print a newline in a system independent way? Use System.out.println() or call System.getProperty(line.separator) which returns a string containing the line separator. On Unix, it is "\n".
In Mac OS X, how do I get the menu to display at the top of the screen instead of at the top of the frame? Execute with java -Dapple.laf.useScreenMenuBar=true
Is it possible to crash the Java Virtual Machine? Yes. Program Bug.java consistently crashes the JVM on a variety of operating systems.
My gif or jpg image doesn't display.
Be sure that the image is in the appropriate directory. Java
may not warn if it cannot find the file - it may just display nothing.
If you are working in an applet, the getImage()
cannot occur in the init() method.
If your images are large, you may need to use the class
MediaTracker so that the images are downloaded before
you attempt to display them. This is done automatically in our
Turtle graphics library.
