(Solved) : Pick Topic Elementsofjava Section Feel Good Review Take Code Segment Reading Write Segmen Q44002992 . . .
Pick a topic from the ElementsOfJava section that you feel isgood review for you. Take the code segment from the reading orwrite your own segment that demonstrates the lesson and embed itinto a main() Java program so it works. Please comment thepage and segment you are reviewing. Use JavaDoc comments.
- Attach the .java source file and the
- copyAndPaste the output into WriteSubmissionBox
Primitive Types
S1.16 A whole number without a decimal point, such as 0, 1, or-2, is called an integer. A number with a decimal point, such as3.14159, -8.63, or 5.0, is called a floating-point number. Noticethat 5.0 is a floating-point number, not an integer. If a numberhas a fractional part, even if the fractional part is zero, it is afloating-point number.
All the Java primitive types appear inside the cover of thisbook. Notice that there are four types for integers—namely byte,short, int, and long. The only difference among the various integertypes is the range of integers they can store and the amount ofcomputer memory they use. If you cannot decide which integer typeto use, use the type int. It has a large enough range for mostpurposes and does not use as much memory as the type long.
Java has two types for floating-point numbers, float and double.If you cannot decide between the types float and double, usedouble. It allows a wider range of values and is used as a defaulttype for floating-point numbers.
You use the primitive type char for single characters, such asletters, digits, or punctuation. For example, the followingdeclares the variable symbol to be of type char, stores thecharacter for uppercase A in symbol, and then displays thatvalue—the A—on the screen:
char symbol;
symbol = ‘A’; System.out.println(symbol);
Notice that we enclose the character A in single quotes. Againnote that uppercase letters and lower- case letters are differentcharacters. For example, ‘a’ and ‘A’ represent two differentcharacters.
Finally, the primitive type boolean has two values, true andfalse. You can use a variable of type boolean to store the answerto a true/false question such as “Is myTime less thanyourTime?”
Constant
S1.17 A variable can have its value changed; its value varies. Aliteral number like 2 cannot change. It is always 2. It is never 3.Values like 2 or 4.8 are called constants, or literals, becausetheir values do not change.
You write constants of integer types with an optional plus signor minus sign, but without commas or decimal points. Floating-pointconstants have an optional plus sign or a minus sign and no commas.You can write a floating-point constant in one of two ways. One waylooks like the everyday way of writing numbers. For example, 9.8,−3.14, and 5.0 are floating-point constants, because they contain adecimal point. The second way is to include a multiplier that is apower of 10. You use the letter e to represent both themultiplication sign and the 10. For example, the number 8.65 * 108appears in Java as 8.65e8 (or in the less convenient form865000000.0). The two forms, 8.65e8 and 865000000.0, are equivalentin a Java program. Similarly, the number 4.83 * 10-4, which isequal to 0.000483, can be written as 4.83e−4 in Java.
The e stands for “exponent,” since it is followed by a numberthat is thought of as an expo- nent of 10. The number before the ecan be a number with or without a decimal point. The number afterthe e cannot contain a decimal point.
Other types of literal expressions are also called constants.You write constants of type char by placing the character withinsingle quotes. For example, ‘Y’ is a constant of type char. Astring constant is a sequence of characters enclosed in doublequotes, as in “Java”.
Assignment Statements
S1.18 You can use an assignment statement to give a value to avariable. For example, if answer is a variable of type int and wewant to give it the value 42, we could use the following assignmentstatement:
answer = 42;
An assignment statement always consists of a single variable onthe left-hand side of an equal sign and an expression on theright-hand side followed by a semicolon. The expression can beanother variable, a constant, or a more complicated expression madeup by combining operators, such as + and *, with variables andconstants. The value of the expression is assigned to the variableon the left of the equal sign.
For example, the following are all examples of assignmentstatements:
amount = 3.99;
firstInitial = ‘B’;
score = numberOfCards + handicap;
Here we assume that amount is a variable of type double,firstInitial is of type char, and the rest of the variables are oftype int. If the variable numberOfCards has the value 7 andhandicap has the value 2, the value of the variable score is 9.
The equal sign, =, which is called the assignment operator, doesnot mean equality. You can think of the assignment operator assaying, “Make the value of the variable equal to what fol- lows.”For example, in the statement
eggsPerBasket = eggsPerBasket − 2;
the variable eggsPerBasket occurs on both sides of theassignment operator. This statement subtracts 2 from the presentvalue of eggsPerBasket and assigns the new value to eggsPerBasket.In effect, the statement decreases the value of eggsPerBasket by2.
S1.19 A variable that has been declared but that has not yetbeen given a value by the program is uninitialized. Such a variablemight literally have no value, or it might have some default value.For example, an integer variable might have a default value ofzero, and a reference variable might have a default value of null,which is a predefined constant in Java. However, your program willbe clearer if you explicitly give the variable a value, even if youare simply reassigning it the default value. (The exact details ondefault values have been known to change and should not be countedon.)
One easy way to ensure that you do not have an uninitializedvariable is to initialize it within the declaration. Simply combinethe declaration and an assignment statement, as in the follow- ingexamples:
int count = 0;
double taxRate = 0.075;
char grade = ‘A’;
int balance = 1000, newBalance;
Note that a single declaration, such as the last statement, caninitialize some variables and not others.
Sometimes the compiler may complain that you have failed toinitialize a variable. In most cases, this is indeed true.Occasionally, the compiler is mistaken about this. However, thecompiler will not compile your program until you convince it thatthe variable in question is initialized. To make the compilerhappy, initialize the variable when it is declared, even if thevariable will be given a different value before you use it foranything. In such cases, you cannot argue with the compiler.
Expert Answer
Answer to Pick a topic from the ElementsOfJava section that you feel is good review for you. Take the code segment from the readin…
OR