Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

Summary 61

FIGURE 5.1
The output of the PlanetWeight
program.

As in otherprograms you have created, the PlanetWeightprogram uses a
main()block statement for all its work. This statement can be broken into
the following four sections:



  1. Lines 3–5: The person’s weight is set initially to 205.

  2. Lines 7–9: Mercury weight loss is calculated.

  3. Lines 11–13: Weight loss on the Moon is determined.

  4. Lines 15–17: Jupiter weight gain is calculated.


Line 4 creates the weightvariable and designates it as an integer variable
with int. The variable is given the initial value 205 and used throughout
the program to monitor the person’s weight.


The next line is similar to several other statements in the program:


System.out.println(weight);


The System.out.println()command displays a string that is contained
within its parenthesis marks. On Line 3, the System.out.print()
command displays the text “Your weight on Earth is”. There are several
System.out.print()and System.out.println()statements in the program.


The difference between them is that print()does not start a new line after
displaying the text, whereas println()does.


Summary


Now that you have been introduced to variables and expressions, you can
give a wide range of instructions to your computer in a program. With the
skills you have developed during this hour, you can write programs that
accomplish many of the same tasks as a calculator, handling sophisticated
mathematical equations with ease. You’ve also learned that a trip to the
Moon is a particularly effective diet plan.


Numbers are only one kind of thing that can be stored in a variable. You
also can store characters, strings of characters, and special trueor false
values called booleanvariables. The next hour expands your knowledge of
Stringvariables and how they are used.

Free download pdf