Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Workshop

Q&A

  • Q. How important is it to put the right number of blank spaces on a line in a Java program?

  • A. It’s completely unimportant as far as the computer is concerned. Spacing is strictly for the benefit of people looking at a computer program—the Java compiler couldn’t care less. You could have written the Saluton program without using blank spaces or used the Tab key to indent lines, and it would compile successfully.

    Although the number of spaces in front of lines isn’t important, you should use consistent spacing and indentation in your Java programs. Why? Because spacing makes it easier for you to see how a program is organized and to which programming block a statement belongs.

    The programs you write must be understandable to other programmers, including yourself when you look at the code weeks or months later to fix a bug or make an enhancement. Consistency in spacing and indentation are part of what’s called a programming style. Good programmers adopt a style and practice it in all their work.

  • Q. A Java program has been described as a class and as a group of classes. Which is it?

  • A. Both. The simple Java programs you create during the next few hours are compiled into a single file with the extension .class. You can run these with the Java Virtual Machine. Java programs also can be made up of a set of classes that work together. This topic is fully explored during Hour 10, “Creating Your First Object.”
  • Q. If semicolons are needed at the end of each statement, why does the comment line // My first Java program goes here not end with a semicolon?

  • A. Comments are completely ignored by the compiler. If you put // on a line in your program, this tells the Java compiler to ignore everything to the right of the // on that line. The following example shows a comment on the same line as a statement:

    System.out.println(greeting); // hello, world!
  • Q. I couldn’t find any errors in the line where the compiler noted an error. What can I do?

  • A. The line number displayed with the error message isn’t always the place where an error needs to be fixed. Examine the statements that are directly above the error message to see whether you can spot any typos or other bugs. The error usually is within the same programming block.

  • Q. How can I visit Antarctica?

  • A. If you’re not willing to become a scientific researcher or a support staffer such as a cook, electrician, or doctor, you can become one of the 10,000 people who visit the frozen continent annually as tourists.

    Flyovers are available from Australia, New Zealand, and South America and cost around $1,000 per person.

    Several cruise ships visit for a trip lasting from 10 days to three weeks, the most expensive of which is around $25,000. Some cruises offer a chance to kayak or hike among penguins, visit icebergs, and even camp overnight.

    The Polar Cruises website at www.polarcruises.com provides more information for prospective Antarctica visitors.

    The British Antarctic Survey offers a piece of advice for visitors: “Do not walk onto glaciers or large snowfields unless properly trained.”

Quiz

Test your knowledge of the material covered in this hour by answering the following questions.

  1. When you compile a Java program, what are you doing?

    1. Saving it to a disk
    2. Converting it into a form the computer can better understand
    3. Adding it to your program collection
  2. What is a variable?

    1. Something that wobbles but doesn’t fall down
    2. Text in a program that the compiler ignores
    3. A place to store information in a program
  3. What is the process of fixing errors called?

    1. Defrosting
    2. Debugging
    3. Decomposing

Answers

  1. B. Compiling a program converts a .java file into a .class file or a set of .class files.
  2. C. Variables are one place to store information; later you learn about others such as arrays and constants. Weebles wobble but they don’t fall down, and comments are text in a program that the compiler ignores.
  3. B. Because errors in a computer program are called bugs, fixing those errors is called debugging. Some programming tools come with a tool called a debugger that helps you fix errors. NetBeans has one of debest debuggers.

Activities

If you’d like to explore the topics covered in this hour a little more fully, try the following activities:

  • You can translate the English phrase “Hello world!” into other languages using the Google Translator at http://translate.google.com. Write a program that enables your computer to greet the world in a language such as French, Italian, or Portuguese.
  • Go back to the Saluton program and add one or two errors. For example, take a semicolon off the end of a line or change the text println on one line to print1n (with a number 1 instead of the letter L). Save the program and try to compile it; then compare the error messages you see to the errors you caused.

To see solutions to these activities, visit the book’s website at www.java24hours.com.

  • + Share This
  • 🔖 Save To Your Account