Thursday, July 25, 2013

Variables with a Fixed Set of Integer Values

You will usually would like variables which will have values solely from a predefined fastened set. for instance, suppose you wish to outline associate degree whole number variable with the name weekday, which can store associate degree whole number worth representing each day of the week. The variable ideally must be restricted to seven potential values, one for every of Monday through Sunday. this is often a state of affairs wherever a facility known as associate degree enumeration may be a natural alternative. you'll outline associate degree enumeration in Java for this case with the subsequent declaration statement:

enum Day

This defines a replacement kind, Day, for variables which will store only 1 or alternative of the values such that between the braces. The names Monday, Tuesday, and then on through to Sunday square measure known as enumeration constants, and that they establish the sole values that square measure allowed for variables of kind Day. In fact, these names can correspond to whole number values, ranging from zero during this case, however they're not constant as whole number variables as a result of they exist solely inside the context of the enumeration, Day. Note the absence of a punctuation at the tip of the definition of the Day enumeration. as a result of you're shaping a kind here, no punctuation is needed once the closing brace. I used a capital D at the start of the kind name, Day,because by convention, sorts that you simply outline begin with a capital. The names for the enumeration constants would sometimes be written starting with a minuscule letter, however during this case I used a capital at the start as a result of that’s however the times of the week square measure sometimes written. you'll even as well write the enumeration constants with a minuscule letter.

Importing the Math Class Methods

It would be a lot more convenient if you were able to avoid having to qualify the name of every method in the Math class that you use with the class name. The code would be a lot less cluttered if you could write floor(radius) instead of Math.floor(radius) for example. Well, you can. All you need to do is put the following statement at the beginning of the source file:

import static java.lang.Math.*; // Import static class members

This statement makes the names of all the static members of the Math class available for use in your Java program code without having to qualify them with the class name. This includes constants such as PI as well as static methods. You can try this statement in the PondRadius example. With this statement at the beginning of the source file, you will be able to remove the qualification by the class name Math from all the members of this class that the program uses.

In the statement indicates that all static names are to be imported. If you wanted to import just the names from the Math class that the PondRadius program uses, you would write:

import static java.lang.Math.floor; // Import floor
import static java.lang.Math.sqrt; // Import sqrt
import static java.lang.Math.round; // Import round
import static java.lang.Math.PI; // Import PI

These statements import individually the four names from the Math class that the program references. You could use these four statements at the beginning of the program in place of the previous import statement that imports all the static names.

Automatic Type Conversions in Assignments

When the type of the result of an arithmetic expression on the right of an assignment operator differs from the type of the variable on the left, an automatic cast will be applied to the result as long as there is no possibility of losing information in Java program. If you think of the basic types that we have seen so far as being in the sequence

byte => short => int => long => float => double

then an automatic conversion will be made as long as it is upwards through the sequence of types, that is, from left to right. If you want to go in the opposite direction, from type double to type float or long, for example, then you must insert an explicit cast into your code for the result of the expression on< the right of the assignment operator.

Wednesday, July 24, 2013

Explicit Casting

It may well be that the default treatment of mixed expressions listed in the preceding section is not what you want. For example, suppose you have defined a double variable result; and two variables, three and two, of type int with the values 3 and 2, respectively. If you compute the value of result with the statement

result = 1.5 + three/two;

the value stored will be 2.5, since three/two will be executed as an integer operation and will produce the result 1. You may have wanted the term three/two to produce the value 1.5 in Java program so the overall result would be 3.0. You could do this using an explicit cast:

result = 1.5 + (double)three/two;

This causes the value stored in three to be converted to type double before the divide operation takes place. Then rule 1 applies for the divide operation, and the operand two is also converted to type double before the divide operation is executed.

Fixing the Value of a Variable

Sometimes you will declare and initialize a variable with a value that should never change. For example:

int feet_per_yard = 3;
double mm_per_inch = 25.4;

Both these values should be fixed. There are always 3 feet to a yard, and an inch will always be 25.4 millimeters. Although they are fixed values for which you could use a literal in calculations, it is very convenient to store them in a variable because using suitable names makes it clear in your program what they mean. If you use the value 3 in your program code it could mean anything - but the name feet_per_yard leaves no doubt as to what it is.

However, ideally you’d like to prevent these variables from varying if possible. Accidental changes to the number of feet in a yard could make the results of your program suspect to say the least. Java provides you with a way to fix the value of any variable by using the final keyword when you declare it. For example:

final int FEET_PER_YARD = 3; // Constant values
final double MM_PER_INCH = 25.4; // that cannot be changed

The final keyword specifies that the value of a variable is final and must not be changed. The compiler will check your code for any violations of this and flag them as errors. I’ve used uppercase letters for the names of the variables here because it is a convention in Java to write constants in this way. This makes it easy to see which variables are defined as constant values. Obviously, any variable you declare as final must have an initial value assigned, as you can’t specify it later.

Now that you know how to declare and initialize variables of the basic types, you are nearly ready to write a program. You just need to look at how you express the calculations you want carried out, and you store the results.