This chapter explains the basic syntaxes of the Java programming language. I shall assume that you have written some simple Java programs. Otherwise, read "Introduction To Java Programming for First-time Programmers". To be proficient in a programming language, you need to master two things: - The syntax of the programming language: Not too difficult to learn a small set of keywords and syntaxes. For examples, JDK 1.8 has 48 keywords; C11 has 44, and C++11 has 73.
- The Application Program Interface (API) libraries associated with the language: You don’t want to write everything from scratch yourself. Instead, you can re-use the available code in the library. Learning library could be difficult as it is really huge, evolving and could take on its own life as another programming language.
The first few sections are a bit boring, as I have to explain the basic concepts with some details. You may also try the "Exercises on Java Basics". Basic SyntaxesSteps in Writing a Java ProgramThe steps in writing a Java program is illustrated as follows: Step 1: Write the source code boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 6 using a programming text editor (such as Sublime Text, Atom, Notepad++, Textpad, gEdit) or an IDE (such as Eclipse or NetBeans).Step 2: Compile the source code boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 6 into Java portable bytecode boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 8 using the JDK Compiler by issuing command:javac Xxx.java
Step 3: Run the compiled bytecode boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 8 with the input to produce the desired output, using the Java Runtime by issuing command:java Xxx
Java Program TemplateYou can use the following template to write your Java programs. Choose a meaningful "Classname" that reflects the purpose of your program, and write your programming statements inside the body of the "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 0 method. Don't worry about the other terms and keywords now. I will explain them in due course. Provide comments in your program!/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
}
A Sample Program Illustrating Sequential, Decision and Loop ConstructsBelow is a simple Java program that demonstrates the three basic programming constructs: sequential, loop, and conditional. Read "Introduction To Java Programming for First-time Programmers" if you need help in understanding this program. /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
}
The expected outputs are: The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 CommentsComments are used to document and explain your code and your program logic. Comments are not programming statements. They are ignored by the compiler and have no consequences to the program execution. Nevertheless, comments are VERY IMPORTANT for providing documentation and explanation for others to understand your programs (and also for yourself three days later). There are two kinds of comments in Java: - Multi-Line Comment: begins with a
"Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 1 and ends with a "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 2, and can span multiple lines. "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 3 is a special documentation comment. These comment can be extracted to produce documentation. - End-of-Line (Single-Line) Comment: begins with
"Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 4 and lasts till the end of the current line.
I recommend that you use comments liberally to explain and document your code. During program development, instead of deleting a chunk of statements irrevocably, you could comment-out these statements so that you could get them back later, if needed. Statements and BlocksStatement: A programming statement is the smallest independent unit in a program, just like a sentence in the English language. It performs a piece of programming action. A programming statement must be terminated by a semi-colon ( "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 5), just like an English sentence ends with a period. (Why not ends with a period like an English sentence? This is because period crashes with decimal point - it is challenging for the dumb computer to differentiate between period and decimal point in the early days of computing!)For examples, Block: A block is a group of programming statements surrounded by a pair of curly braces "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 6. All the statements inside the block is treated as one single unit. Blocks are used as the body in constructs like class, method, if-else and loop, which may contain multiple statements but are treated as one unit (one body). There is no need to put a semi-colon after the closing brace to end a compound statement. Empty block (i.e., no statement inside the braces) is permitted.For examples, White Spaces and Formatting Source CodeWhite Spaces: Blank, tab and newline are collectively called white spaces. You need to use a white space to separate two keywords or tokens to avoid ambiguity, e.g., Java, like most of the programming languages, ignores extra white spaces. That is, multiple contiguous white spaces are treated as a single white space. Additional white spaces and extra lines are ignored, e.g., Formatting Source Code: As mentioned, extra white spaces are ignored and have no computational significance. However, proper indentation (with tabs and blanks) and extra empty lines greatly improves the readability of the program. This is extremely important for others (and yourself three days later) to understand your programs. For example, the following one-line hello-world program works. But can you read and understand the program? public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
Braces: Java's convention is to place the beginning brace at the end of the line, and to align the ending brace with the start of the statement. Pair-up the { } properly. Unbalanced { } is one of the most common syntax errors for beginners. Indentation: Indent each level of the body of a block by an extra 3 or 4 spaces according to the hierarchy of the block. Don't use tab because tab-spaces is editor-dependent. "Code is read much more often than it is written." Hence, you have to make sure that your code is readable (by others and yourself 3 days later), by following convention and recommended coding style. Variables and TypesVariables - Name, Type and ValueComputer programs manipulate (or process) data. A variable is used to store a piece of data for processing. It is called variable because you can change the value stored. More precisely, a variable is a named storage location, that stores a value of a particular data type. In other words, a variable has a name, a type and stores a value. - A variable has a name (aka identifier), e.g.,
"Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 7, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 8, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 9, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 0 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 1. The name is needed to uniquely identify and reference each variable. You can use the name to assign a value to the variable (e.g., "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 2), and to retrieve the value stored (e.g., "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 3). - A variable has a data type. The frequently-used Java data types are:
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4: meant for integers (whole numbers) such as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 5 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 6."The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7: meant for floating-point number (real numbers) having an optional decimal point and fractional part, such as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 8, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 9, java Xxx
00, or java Xxx
01, where java Xxx
02 or java Xxx
03 denotes exponent of base java Xxx
04.java Xxx
05: meant for texts such as java Xxx
06 and java Xxx
07. java Xxx
05s are enclosed within a pair of double quotes.java Xxx
09: meant for a single character, such as java Xxx
10, java Xxx
11. A java Xxx
09 is enclosed by a pair of single quotes.
- In Java, you need to declare the name and the type of a variable before using a variable. For examples,
- A variable can store a value of the declared data type. It is important to take note that a variable in most programming languages is associated with a type, and can only store value of that particular type. For example, an
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 variable can store an integer value such as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 5, but NOT floating-point number such as java Xxx
15, nor string such as java Xxx
06. - The concept of type was introduced in the early programming languages to simplify interpretation of data made up of binary sequences (
java Xxx
17's and java Xxx
18's). The type determines the size and layout of the data, the range of its values, and the set of operations that can be applied.
The following diagram illustrates three types of variables: "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 and java Xxx
05. An "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 variable stores an integer (or whole number or fixed-point number); a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 variable stores a floating-point number (or real number); a java Xxx
05 variable stores texts.Identifiers (or Names)An identifier is needed to name a variable (or any other entity such as a method or a class). Java imposes the following rules on identifiers: - An identifier is a sequence of characters, of any length, comprising uppercase and lowercase letters
java Xxx
25, digits java Xxx
26, underscore (java Xxx
27), and dollar sign (java Xxx
28). - White space (blank, tab, newline) and other special characters (such as
java Xxx
29, java Xxx
30, java Xxx
31, java Xxx
32, java Xxx
33, java Xxx
34, commas, etc.) are not allowed. Take note that blank and dash (java Xxx
30) are not allowed, i.e., "java Xxx
36" and "java Xxx
37" are not valid names. (This is because blank creates two tokens and dash crashes with minus sign!) - An identifier must begin with a letter
java Xxx
25 or underscore (java Xxx
27). It cannot begin with a digit java Xxx
26 (because that could confuse with a number). Identifiers begin with dollar sign (java Xxx
28) are reserved for system-generated entities. - An identifier cannot be a reserved keyword or a reserved literal (e.g.,
java Xxx
42, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, java Xxx
45, java Xxx
46, java Xxx
47, java Xxx
48, java Xxx
49, java Xxx
50). - Identifiers are case-sensitive. A
java Xxx
51 is NOT a java Xxx
52, and is NOT a java Xxx
53.
Examples: java Xxx
54, java Xxx
55, java Xxx
56, java Xxx
57 are valid identifiers. But java Xxx
58, java Xxx
59, java Xxx
60, java Xxx
61 are NOT valid identifiers.Caution: Programmers don't use blank character in any names (filename, project name, variable name, etc.). It is either not supported (e.g., in Java and C/C++), or will pose you many more challenges. Variable Naming ConventionA variable name is a noun, or a noun phrase made up of several words with no spaces between words. The first word is in lowercase, while the remaining words are initial-capitalized. For examples, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 7, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 8, java Xxx
64, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 1, java Xxx
66, java Xxx
67, java Xxx
68, java Xxx
69, and java Xxx
70. This convention is also known as camel-case.Recommendations- It is important to choose a name that is self-descriptive and closely reflects the meaning of the variable, e.g.,
java Xxx
71 or "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 1, but not java Xxx
73 or java Xxx
74, to store the number of students. It is alright to use abbreviations. - Do not use meaningless names like
java Xxx
75, java Xxx
76, java Xxx
77, java Xxx
78, java Xxx
79, java Xxx
80, java Xxx
73, java Xxx
82, java Xxx
83, java Xxx
84, java Xxx
85, java Xxx
86 (what is the purpose of this exercise?), and java Xxx
87 (What is this example about?). - Avoid single-letter names like
java Xxx
78, java Xxx
79, java Xxx
80, java Xxx
75, java Xxx
76, java Xxx
77, which are easier to type but often meaningless. Exceptions are common names like java Xxx
74, java Xxx
95, java Xxx
96 for coordinates, java Xxx
78 for index. Long names are harder to type, but self-document your program. (I suggest you spend sometimes practicing your typing.) - Use singular and plural nouns prudently to differentiate between singular and plural variables. For example, you may use the variable
java Xxx
98 to refer to a single row number and the variable java Xxx
99 to refer to many rows (such as an array of rows - to be discussed later).
Variable DeclarationTo use a variable in your program, you need to first introduce it by declaring its name and type, in one of the following syntaxes. The act of declaring a variable allocates a storage of size capable of holding a value of the type. SyntaxExampleint sum; double average; String statusMsg;int number, count; double sum, difference, product, quotient; String helloMsg, gameOverMsg; int magicNumber = 99; double pi = 3.14169265; String helloMsg = "hello,"; int sum = 0, product = 1; double height = 1.2, length = 3.45; String greetingMsg = "hi!", quitMsg = "bye!";Take note that: - A variable is declared with a type. Once the type of a variable is declared, it can only store a value belonging to that particular type. For example, an
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 variable can hold only integer (such as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 5), and NOT floating-point number (such as /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 02) or text string (such as java Xxx
06). - Each variable can only be declared once because identifier shall be unique.
- You can declare a variable anywhere inside the program, as long as it is declared before being used.
- The type of a variable cannot be changed inside the program, once it is declared.
- A variable declaration statement begins with a type, and works for only that type. In other words, you cannot declare variables of two different types in a single declaration statement.
- Java is a statically-typed language. This means that the type is resolved at compile time and never changes.
Constants (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 04 variables)Constants are non-modifiable (immutable) variables, declared with keyword /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 04. You can only assign values to final variables ONCE. Their values cannot be changed during program execution. For examples:Constant Naming Convention: Use uppercase words, joined with underscore. For example, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 06, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 07, and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 08.ExpressionsAn expression is a combination of operators (such as /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 09 and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 10) and operands (variables or literals), that can be evaluated to yield a single value of a certain type.For example, Assignment (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 11)An assignment statement evaluates the RHS (Right-Hand Side) and assigns the resultant value to the variable of the LHS (Left-Hand Side). The syntax for assignment statement is: SyntaxExampleint number; number = 9;int sum = 0, number = 8; sum = sum + number;The assignment statement should be interpreted this way: The expression on the RHS is first evaluated to produce a resultant value (called r-value or right-value). The r-value is then assigned to the variable on the left-hand-side (LHS) or l-value. Take note that you have to first evaluate the RHS, before assigning the resultant value to the LHS. For examples, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 12 is Assignment, NOT Equality
In Java, the equal symbol /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 12 is known as the assignment operator. The meaning of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 12 in programming is different from Mathematics. It denotes assignment of the RHS value to the LHS variable, NOT equality of the RHS and LHS. The RHS shall be a literal value or an expression that evaluates to a value; while the LHS must be a variable.Note that /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 15 is valid (and often used) in programming. It evaluates the RHS expression /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 16 and assigns the resultant value to the LHS variable java Xxx
74. On the other hand, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 15 is illegal in Mathematics.While /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 19 is allowed in Mathematics, it is invalid in programming because the LHS of an assignment statement shall be a variable.Some programming languages use symbol " /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 20", "/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 21" or "/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 22" as the assignment operator to avoid confusion with equality.Primitive Types and java Xxx
05In Java, there are two broad categories of data types: - Primitive types (e.g.,
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7), - Reference types (e.g., objects and arrays).
We shall describe the primitive types here. We will cover the reference types (classes and objects) in the later chapters on "Object-Oriented Programming". Built-in Primitive TypesTYPEDESCRIPTIONbyteInteger8-bit signed integer The range is /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 26short16-bit signed integer The range is /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 27int32-bit signed integer The range is /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 28 (≈9 digits, ±2G)long64-bit signed integer The range is /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 29 (≈19 digits)floatFloating-Point Number F x 2E32-bit single precision floating-point number (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 306-7 significant decimal digits, in the range of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 31)double64-bit double precision floating-point number (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 3014-15 significant decimal digits, in the range of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 33)charCharacter Represented in 16-bit Unicode /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 34 to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 35. Can be treated as integer in the range of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 36 in arithmetic operations. (Unlike C/C++, which uses 8-bit ASCII code.)booleanBinary Takes a literal value of either java Xxx
48 or java Xxx
49. The size of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 is not defined in the Java specification, but requires at least one bit.
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39s are used in test in decision and loop, not applicable for arithmetic operations. (Unlike C/C++, which uses integer java Xxx
17 for false, and non-zero for true.)Primitive type are built-into the language for maximum efficiency, in terms of both space and computational efficiency. Java has eight primitive types, as listed in the above table: - There are four integer types: 8-bit
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, 16-bit /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, 32-bit "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 and 64-bit /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45. They are signed integers in 2's complement representation, and can hold a zero, positive and negative integer value of the various ranges as shown in the table. - There are two floating-point types: 32-bit single-precision
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 and 64-bit double-precision "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7. They are represented in scientific notation of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 48 where the fraction (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 49) and exponent (java Xxx
03) are stored separately (as specified by the IEEE 754 standard). Take note that not all real numbers can be represented by /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7. This is because there are infinite real numbers even in a small range of say /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 53, but there is a finite number of patterns in a n-bit representation. Most of the floating-point values are approximated to their nearest representation. - The type
java Xxx
09 represents a single character, such as /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 56, java Xxx
10. In Java, java Xxx
09 is represented using 16-bit Unicode (in UCS-2 format) to support internationalization (i18n). A java Xxx
09 can be treated as an integer in the range of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 36 in arithmetic operations. For example, character /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55 is /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 62 (decimal) or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 63 (hexadecimal); character /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 56 is /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 65 (decimal) or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 66 (hexadecimal); character java Xxx
10 is /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 68 (decimal) or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 69 (hexadecimal). - Java introduces a new binary type called "
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39", which takes a literal value of either java Xxx
48 or java Xxx
49. /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39s are used in test in decision and loop. They are not applicable to arithmetic operations (such as addition and multiplication).
Integers vs. Floating-Point NumbersIn computer programming, integers (such as 123, -456) and floating-point numbers (such as 1.23, -4.56, 1.2e3, -4.5e-6) are TOTALLY different. - Integers and floating-point numbers are represented and stored differently.
- Integers and floating-point numbers are operated differently.
How Integers and Floating-Point Numbers are Represented and Stored in Computer Memory?Integers are represented in a so called 2's complement scheme as illustrated. The most-significant bit is called Sign Bit ( /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 74), where /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 75 represents positive integer and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 76 represents negative integer. The remaining bits represent the magnitude of the integers. For positive integers, the magnitude is the same as the binary number, e.g., if /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 77 (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43), /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 79 is /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 80. Negative integers require 2's complement conversion.Floating-point numbers are represented in scientific form of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 48, where Fraction (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 49) and Exponent (java Xxx
03) are stored separately. For example, to store /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 84; first convert to binary of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 85; then normalize to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 86; we have /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 87 and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 88 which are then stored with some scaling.For details, read "Data Representation - Integers, Floating-Point Numbers and Characters". How Integers and Floating-Point Numbers are Operated?Integers and floating-point numbers are operated differently using different hardware circuitries. Integers are processed in CPU (Central Processing Unit), while floating-point numbers are processed in FPU (Floating-point Co-processor Unit). Integer operations are straight-forward. For example, integer addition is carried out as illustrated: On the other hand, floating-point addition is complex, as illustrated: It is obvious that integer operations (such as addition) is much faster than floating-point operations. Furthermore, integer are precise. All numbers within the range can be represented accurately. For example, a 32-bit "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 can represent ALL integers from /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 90 to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 91 with no gap in between. On the other hand, floating-point are NOT precise, but close approximation. This is because there are infinite floating-point numbers in any interval (e.g., between 0.1 to 0.2). Not ALL numbers can be represented using a finite precision (32-bit /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 or 64-bit "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7).You need to treat integers and Floating-point numbers as two DISTINCT types in programming!Use integer if possible (it is faster, precise and uses fewer bits). Use floating-point number only if a fractional part is required.Data RepresentationRead "Data Representation - Integers, Floating-Point Numbers and Characters" if you wish to understand how the numbers and characters are represented inside the computer memory. In brief, It is important to take note that java Xxx
09 /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 95 is different from "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 java Xxx
18, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42 java Xxx
18, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43 java Xxx
18, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 03, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 03, and java Xxx
05 /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 07. They are represented differently in the computer memory, with different precision and interpretation. They are also processed differently. For examples:/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42 java Xxx
18 is /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 10 (8-bit)./**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43 java Xxx
18 is /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 13 (16-bit)."The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 java Xxx
18 is /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 16 (32-bit)./**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45 java Xxx
18 is /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 19 (64-bit)./**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 03 is /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 22 (32-bit)."The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 03 is /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 25 (64-bit).java Xxx
09 /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 95 is /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 28 (16-bit) (Unicode number 49).java Xxx
05 /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 07 is a complex object (many many bits).
There is a subtle difference between "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 java Xxx
17 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 34 as they have different bit-lengths and internal representations.Furthermore, you MUST know the type of a value before you can interpret a value. For example, this bit-pattern /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 16 cannot be interpreted unless you know its type (or its representation).Maximum/Minimum Values of Primitive Number TypesThe following program can be used to print the maximum, minimum and bit-length of the primitive types. For example, the maximum, minimum and bit-size of "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 are kept in built-in constants /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 37, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 38, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 39.One More Important Type - java Xxx
05Beside the 8 primitive types, another important and frequently-used type is java Xxx
05. A java Xxx
05 is a sequence of characters (texts) such as /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 43. java Xxx
05 is not a primitive type (this will be elaborated later).In Java, a java Xxx
09 is a single character enclosed by single quotes (e.g., /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 56, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 48); while a java Xxx
05 is a sequence of characters enclosed by double quotes (e.g., java Xxx
06).For example, Choice of Data Types for VariablesAs a programmer, YOU need to decide on the type of the variables to be used in your programs. Most of the times, the decision is intuitive. For example, use an integer type for counting and whole number; a floating-point type for number with fractional part, java Xxx
05 for text message, java Xxx
09 for a single character, and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 for binary outcomes.It is important to take note that your programs will have data of DIFFERENT types. Rules of Thumb for Choosing Data Types- For numbers, use an integer type if possible. Use a floating-point type only if the number contains a fractional part. Although floating-point numbers includes integers (e.g.,
/**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 03, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 55, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 56), floating-point numbers are approximation (not precise) and require more resources (computational and storage) for operations. - Although there are 4 integer types: 8-bit
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, 16-bit /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, 32-bit "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 and 64-bit /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45, we shall use "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 for integers in general. Use /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45 only if you have a good reason to choose that particular precision. - Among there are two floating-point types: 32-bit
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 and 64-bit "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, we shall use "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 in general. Use /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 only if you wish to conserve storage and do not need the precision of "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7. java Xxx
09, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 and java Xxx
05 have their specific usage.
Example (Variable Names and Types): Paul has bought a new notebook of "idol" brand, with a processor speed of 2.66GHz, 8 GB of RAM, 500GB hard disk, with a 15-inch monitor, for $1760.55. He has chosen service plan 'C' among plans 'A', 'B', 'C', and 'D', plus on-site servicing but did not choose extended warranty. Identify the data types and name the variables. The possible variable names and types are: Exercise (Variable Names and Types): You are asked to develop a software for a college. The system shall maintain information about students. This includes name, address, phone number, gender, date of birth, height, weight, degree pursued (e.g., B.Sc., B.A.), year of study, average GPA, with/without tuition grant, is/is not a scholar. Each student is assigned a unique 8-digit number as id. You are required to identify the variables, assign a suitable name to each variable and choose an appropriate type. Write the variable declaration statements as in the above example. Literals for Primitive Types and java Xxx
05A literal, or literal constant, is a specific constant value, such as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 5, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 6, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 7616, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 77, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 78, java Xxx
10, java Xxx
06, that is used in the program source. It can be assigned directly to a variable; or used as part of an expression. They are called literals because they literally and explicitly identify their values. We call it literal to distinguish it from a variable.Integer ("The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42) literalsA whole number literal, such as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 5 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 6, is treated as an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 by default. For example,An "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 literal may precede with a plus (java Xxx
29) or minus (java Xxx
30) sign, followed by digits. No commas or special symbols (e.g., java Xxx
28, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 92, or space) is allowed (e.g., /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 93, java Xxx
56 and /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 95 are invalid).You can use a prefix /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55 (zero) to denote an integer literal value in octal, and prefix /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 97 (or /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 98) for a value in hexadecimal, e.g.,From JDK 7, you can use prefix ' /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 99' or 'The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 00' to specify an integer literal value in binary. You are also permitted to use underscore (java Xxx
27) to break the digits into groups to improve the readability. But you must start and end the literal with a digit, not underscore. For example,A /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45 literal outside the "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 range requires a suffix The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 04 or The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 05 (avoid lowercase The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 05, which could be confused with the number one /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 95), e.g., The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 08, The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 09. For example,No suffix is needed for /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42 and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43 literals. But you can only use values in the permitted range. For example,Floating-point ("The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46) literalsA literal number with a decimal point, such as The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 14 and The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 15, is treated as a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 by default. You can also express them in scientific notation, e.g., java Xxx
00, The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 18, where java Xxx
02 or java Xxx
03 denotes the exponent in base of 10. You could precede the fractional part or exponent with a plus (java Xxx
29) or minus (java Xxx
30) sign. Exponent values are restricted to integer. There should be no space or other characters in the number.You are reminded that floating-point numbers are stored in scientific form of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 48, where /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 49 (Fraction) and java Xxx
03 (Exponent) are stored separately.You can optionally use suffix The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 26 or The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 27 to denote "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 literals.You MUST use a suffix of The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 29 or The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 30 for /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 literals, e.g., The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 32. For example,Character (java Xxx
09) Literals and Escape SequencesA printable java Xxx
09 literal (such as letters, digits and special symbols) is written by enclosing the character with a pair of single quotes, e.g., /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 56, The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 36, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55, The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 38, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 48, and The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 40. Special java Xxx
09 literals (such as tab, newline) are represented using so-called escape sequences (to be described later).In Java, java Xxx
09s are represented using 16-bit Unicode. Printable characters for English letters (The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 43, The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 44), digits (The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 45) and symbols (java Xxx
29, java Xxx
30, java Xxx
33, etc.) are assigned to code numbers 32-126 (20H-7EH), as tabulated below (arranged in decimal and hexadecimal).Dec01234567893SP!"#$%&'4()*+,-./01523456789:;6<=>?@ABCDE7FGHIJKLMNO8PQRSTUVWXY9Z[\]^_`abc10defghijklm11nopqrstuvw12xyz{|}~Hex0123456789ABCDEF2SP!"#$%&'()*+,-./30123456789:;<=>?4@ABCDEFGHIJKLMNO5PQRSTUVWXYZ[\]^_6`abcdefghijklmno7pqrstuvwxyz{|}~In Java, a java Xxx
09 can be treated as its underlying integer in the range of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 36 in arithmetic operations. In other words, java Xxx
09 and integer are interchangeable in arithmetic operations. You can treat a java Xxx
09 as an The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 53 you can also assign an integer value in the range of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 36 to a java Xxx
09 variable. For example,Special characters are represented by so-called escape sequence, which begins with a back-slash ( The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 56) followed by a pattern, e.g., The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 57 for tab, The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 58 for newline. The commonly-used escape sequences are:Escape SequenceDescriptionUnicode (Decimal)Unicode (Hex)\tTab90009H\nNewline (or Line-feed)10000AH\rCarriage-return13000DH\"Double-quote (Needed to be used inside double-quoted java Xxx
05)--\'Single-quote (Needed to be used inside single-quoted java Xxx
09, i.e., The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 61)--\Back-slash (Needed as back-slash is given a special meaning)--\uhhhhUnicode number The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 62 (in hex), e.g., The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 63 is 您, The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 64 is 好-hhhhHFor examples, java Xxx
05 Literals and Escape Sequences
A java Xxx
05 is a sequence of characters. A java Xxx
05 literal is composed of zero of more characters surrounded by a pair of double quotes. For examples,You need to use an escape sequence for special non-printable characters, such as newline ( The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 58) and tab (The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 57). You also need to use escape sequence for double-quote (The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 70) and backslash (The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 71) due to conflict. For examples,Single-quote ( The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 72) inside a java Xxx
05 does not require an escape sequence because there is no ambiguity, e.g.,It is important to take note that The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 57 or The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 70 is ONE single character, NOT TWO!Exercise: Write a program to print the following animal picture using The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 76. Take note that you need to use escape sequences to print some characters, e.g., The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 70 for The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 78, The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 71 for The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 56. '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" End-of-Line (EOL)Newline ( The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 81) and Carriage-Return (The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 82), represented by the escape sequence The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 58, and The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 84 respectively, are used as line delimiter (or end-of-line, or EOL) for text files. Take note that Unix and macOS use The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 58 (The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 81) as EOL, while Windows use The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 87 (The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 88)./**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 Literals
There are only two /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 literals, i.e., java Xxx
48 and java Xxx
49. For example,boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; Example on LiteralsThe sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 93 - Local Variable Type Inference (JDK 10)
JDK 10 introduces a new way to declare variables via a new keyword The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 93, for examples,Clearly, you need to initialize the variable, so that the compiler can infer its type. Basic OperationsArithmetic OperatorsJava supports the following binary/unary arithmetic operations: OperatorModeUsageDescriptionExamples+Binary UnaryThe sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 95Addition Unary positiveThe sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 96-Binary UnaryThe sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 97Subtraction Unary negateThe sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 98*BinaryThe sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 99Multiplicationpublic class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
00/Binarypublic class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
01Division1 / 2 ⇒ 0 1.0 / 2.0 ⇒ 0.5%Binarypublic class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
02Modulus (Remainder)public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
03These operators are typically binary infix operators, i.e., they take two operands with the operator in between the operands (e.g., public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
04). However, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 10 and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 09 can also be interpreted as unary "negate" and "positive" prefix operator, with the operator in front of the operand. For examples,Arithmetic ExpressionsIn programming, the following arithmetic expression: must be written as public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
07. You cannot omit the multiplication sign (java Xxx
31) as in Mathematics.Rules on PrecedenceLike Mathematics: - Parentheses
public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
09 have the highest precedence and can be used to change the order of evaluation. - Unary
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 10 (negate) and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 09 (positive) have next higher precedence. - The multiplication (
java Xxx
31), division (java Xxx
32) and modulus (/**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 92) have the same precedence. They take precedence over addition (java Xxx
29) and subtraction (java Xxx
30). For example, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
17 is interpreted as public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
18. - Within the same precedence level (i.e., addition/subtraction and multiplication/division/modulus), the expression is evaluated from left to right (called left-associative). For examples,
public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
19 is evaluated as public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
20, and public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
21 is public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
22.
Type Conversion in Arithmetic OperationsYour program typically contains data of many types, e.g., public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
23 and public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
24 are "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
26 and public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
27 are "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, and public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
29 is a java Xxx
05. Hence, it is important to understand how Java handles types in your programs.The arithmetic operators ( java Xxx
29, java Xxx
30, java Xxx
31, java Xxx
32, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 92) are only applicable to primitive number types: /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, and java Xxx
09.They are not applicable to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39.Same-Type Operands of "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7If BOTH operands are "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 or "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, the binary arithmetic operations are carried in that type, and evaluate to a value of that type, i.e.,public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
52, where public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
53 denotes a binary arithmetic operators such as java Xxx
29, java Xxx
30, java Xxx
31, java Xxx
32, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 92.public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
59public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
60public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
61
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 Division
It is important to take note "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 division produces an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, i.e., public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
65"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, with the result truncated. For example, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
67 ("The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4), but public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
69 (public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
70).Same-Type Operands of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, java Xxx
09: Convert to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4If BOTH operands are /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43 or java Xxx
09, the binary operations are carried out in "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, and evaluate to a value of "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4. A java Xxx
09 is treated as an integer of its underlying Unicode number in the range of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 36. That is,public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
82, where public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
53 denotes a binary arithmetic operators such as java Xxx
29, java Xxx
30, java Xxx
31, java Xxx
32, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 92.public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
89public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
90
Take note that NO arithmetic operations are carried out in /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43 or java Xxx
09.For examples, However, if compound arithmetic operators ( public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
94, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
95, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
96, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
97, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
98) (to be discussed later) are used, the result is automatically converted to the LHS. For example,Mixed-Type Arithmetic OperationsIf the two operands belong to different types, the value of the smaller type is promoted automatically to the larger type (known as implicit type-casting). The operation is then carried out in the larger type, and evaluated to a value in the larger type. /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43 or java Xxx
09 is first promoted to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 before comparing with the type of the other operand. (In Java, no operations are carried out in /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43 or java Xxx
09.)- The order of promotion is:
'__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 06.
For examples, '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 07. Hence, '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 08 '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 09 (You probably don't expect this answer!) '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 10 (Result is an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, need to explicitly cast back to java Xxx
09 '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 13 if desired.) '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 14 '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 15
Summary: Type-Conversion Rules for Binary OperationsThe type-promotion rules for binary operations can be summarized as follows: - If one of the operand is
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, the other operand is promoted to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7; - else if one of the operand is
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46, the other operand is promoted to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46; - else if one of the operand is
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45, the other operand is promoted to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45; - else both operands are promoted to
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4.
Summary: Type-Conversion Rules for Unary OperationsThe type-promotion rules for unary operations (e.g., negate /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 10) can be summarized as follows:- If the operand is
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45 or "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, there is no promotion; - else the operand is
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, java Xxx
09, the operand is promoted to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4.
More on Arithmetic OperatorsModulus (Remainder) OperatorTo evaluate the remainder for negative and floating-point operands, perform repeated subtraction until the absolute value of the remainder is less than the absolute value of the second operand. For example, Exponent?Java does not have an exponent operator. (The '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 34 operator denotes exclusive-or, NOT exponent). You need to use JDK method '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 35 to evaluate java Xxx
74 raises to power java Xxx
95; or write your own code.Overflow/UnderflowStudy the output of the following program: In arithmetic operations, the resultant value wraps around if it exceeds its range (i.e., overflow). Java runtime does NOT issue an error/warning message but produces an incorrect result. On the other hand, integer division produces a truncated integer and results in so-called underflow. For example, '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 38 gives java Xxx
17, instead of '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 40. Again, Java runtime does NOT issue an error/warning message, but produces an imprecise result.It is important to take note that checking of overflow/underflow is the programmer's responsibility. i.e., your job!!! Why computer does not flag overflow/underflow as an error? This is due to the legacy design when the processors were very slow. Checking for overflow/underflow consumes computation power. Today, processors are fast. It is better to ask the computer to check for overflow/underflow (if you design a new language), because few humans expect such results. To check for arithmetic overflow (known as secure coding) is tedious. Google for "INT32-C. Ensure that operations on signed integers do not result in overflow" @ www.securecoding.cert.org. More on Integer vs. Floating-Point NumbersIntegers ( /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45) are precise (exact). But /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 are not precise but close approximation. Study the results of the following program:Always use "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 if you do not need the fractional part, although "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 can also represent most of the integers (e.g., /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 03, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 55, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 56). This is because:"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 is more efficient (faster) than "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 in arithmetic operations.- 32-bit
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 takes less memory space than 64-bit "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7. "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 is exact (precise) in representing ALL integers within its range. "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 is an approximation - NOT ALL integer values can be represented by "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7.
Type CastingIn Java, you will get a compilation "error: incompatible types: possible lossy conversion from '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 59 to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4" if you try to assign a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46, or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45 value of to an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 variable. This is because the fractional part would be truncated and lost. For example,Explicit Type-Casting and Type-Casting OperatorTo assign the a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 value to an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 variable, you need to invoke the so-called type-casting operator - in the form of '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 67 - to operate on the "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 operand and return a truncated value in "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4. In other words, you tell the compiler you consciously perform the truncation and you are fully aware of the "possible lossy conversion". You can then assign the truncated "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 value to the "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 variable. For example,Type casting is an operation which takes one operand. It operates on its operand, and returns an equivalent value in the specified type. The syntax is: There are two kinds of type-casting in Java: - Explicit type-casting via a type-casting operator, as described above, and
- Implicit type-casting performed by the compiler automatically, if there is no loss of precision.
Implicit Type-Casting in AssignmentExplicit type-casting is not required if you assign an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 value to a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 variable, because there is no loss of precision. The compiler will perform the type-casting automatically (i.e., implicit type-casting). For example,,The following diagram shows the order of implicit type-casting performed by compiler. The rule is to promote the smaller type to a bigger type to prevent loss of precision, known as widening conversion. Narrowing conversion requires explicit type-cast to inform the compiler that you are aware of the possible loss of precision. Take note that java Xxx
09 is treated as an integer in the range of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 36. /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 value cannot be type-casted (i.e., converted to non-/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39).Example: Suppose that you want to find the average (in "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7) of the running integers from java Xxx
18 and '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 80. Study the following code:The public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
26 of '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 82 is incorrect. This is because both the public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
24 and '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 80 are "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4. The result of '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 86 is an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, which is then implicitly casted to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 and assign to the "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 variable public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
26. To get the correct answer, you can do either:Compound Assignment OperatorsBesides the usual simple assignment operator ( /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 11) described earlier, Java also provides the so-called compound assignment operators as listed:OperationModeUsageDescriptionExample=Binary '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 92Assignment Assign the LHS value to the RHS variable '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 93+=Binary '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 94 same as: '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 95Compound addition and assignment '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 96 same as: '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 97-=Binary '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 98 same as: '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 99Compound subtraction and assignmentboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 00 same as: boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 01*=Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 02 same as:boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 03Compound multiplication and assignmentboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 04 same as: boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 05/=Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 06 same as:boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 07Compound division and assignmentboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 08 same as: boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 09%=Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 10 same as:boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 11Compound modulus (remainder) and assignmentboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 12 same as: boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 13One subtle difference between simple and compound operators is in /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, java Xxx
09 binary operations. For examples,Increment/DecrementJava supports these unary arithmetic operators: increment ( boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 17) and decrement (boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 18) for all primitive number types (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, java Xxx
09, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, except /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39). The increment/decrement unary operators can be placed before the operand (prefix), or after the operands (postfix). These operators were introduced in C++ to shorthand boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 27 to boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 28 or boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 29.OperatorModeUsageDescriptionExample++ (Increment)Unary Prefix Unary Postfixboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 30Increment the value of the operand by 1.
boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 28orboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 29is the same asboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 33or/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 15-- (Decrement)Unary Prefix Unary Postfixboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 35Decrement the value of the operand by 1.
boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 36orboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 37is the same asboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 38orboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 39The increment ( boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 17) and decrement (boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 18) operate on its sole operand and store the result back to its operand. For example, boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 29 retrieves x, increment and stores the result back to x.In Java, there are 4 ways to increment/decrement a variable: Unlike other unary operator (such as negate ( java Xxx
30)) which promotes /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43 and java Xxx
09 to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, the increment and decrement do not promote its operand because there is no such need.The increment/decrement unary operator can be placed before the operand (prefix), or after the operands (postfix), which may affect the outcome. - If these operators are used by themselves (standalone) in a statement (e.g.,
boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 28; or boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 49), the outcomes are the SAME for pre- and post-operators. See above examples. - If
boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 17 or boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 18 involves another operation in the SAME statement, e.g., boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 52 or boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 53 where there are two operations in the same statement: assignment and increment, then pre- or post-order is important to specify the order of these two operations, as tabulated below: OperatorDescriptionExampleSame As++var (Pre-Increment)Increment var, and return the incremented var for the other operation in the same statement.boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 53boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 55var++ (Post-Increment)Return the old value of var for the other operation in the same statement, then increment var.boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 52boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 57--var (Pre-Decrement)Decrement var, and return the decremented var for the other operation in the same statement.boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 58boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 59var-- (Post-Decrement)Return the old value of var for the other operation in the same statement, then decrement var.boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 60boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 61For examples, Notes: - Prefix operator (e.g.,
boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 62) could be more efficient than postfix operator (e.g., boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 63)?! - What is
boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 64? Try it out!
Relational and Logical OperatorsVery often, you need to compare two values before deciding on the action to be taken, e.g. if mark is more than or equals to 50, print "PASS!". Java provides six comparison operators (or relational operators). All these operators are binary operators (that takes two operands) and return a /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 value of either java Xxx
48 or java Xxx
49.OperatorModeUsageDescriptionExample (x=5, y=8)==Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 68Equal toboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 69!=Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 70Not Equal toboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 71>Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 72Greater thanboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 73>=Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 74Greater than or equal toboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 75<Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 76Less thanboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 77<=Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 78Less than or equal toboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 79Take note that the comparison operators are binary infix operators, that operate on two operands with the operator in between the operands, e.g., boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 80. It is invalid to write boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 81 (non-binary operations). Instead, you need to break out the two binary comparison operations boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 82, boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 83, and join with a logical AND operator, i.e., boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 84, where boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 85 denotes AND operator.Java provides four logical operators, which operate on /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 operands only, in descending order of precedence, as follows:OperatorModeUsageDescriptionExample!Unaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 87Logical NOT&&Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 88Logical AND||Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 89Logical OR^Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 90Logical Exclusive-OR (XOR)The truth tables are as follows: NOT boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 91truefalseResultfalsetrue AND boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 92truefalsetruetruefalsefalsefalsefalse OR boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 93truefalsetruetruetruefalsetruefalse XOR boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 94truefalsetruefalsetruefalsetruefalseExamples: Exercise: Study the following program, and explain its output. Write an expression for all unmarried male, age between 21 and 35, with height above 180, and weight between 70 and 80. Exercise: Given the boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 95, boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 96 (1-12), and boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 97 (1-31), write a /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 expression which returns java Xxx
48 for dates before October 15, 1582 (Gregorian calendar cut-over date).Ans: "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 00Equality Comparison "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 01You can use "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 01 to compare two integers (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45) and java Xxx
09. But do NOT use "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 01 to compare two floating-point numbers (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7) because they are NOT precise. To compare floating-point numbers, set a threshold for their difference, e.g.,You also CANNOT use "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 01 to compare two java Xxx
05s because java Xxx
05s are objects. You need to use "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 14 instead. This will be elaborated later.Logical Operator PrecedenceThe precedence from highest to lowest is: "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 15 (unary), "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 16, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 17, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 18'. But when in doubt, use parentheses!Short-Circuit OperationsThe binary AND ( boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 85) and OR ("Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 20) operators are known as short-circuit operators, meaning that the right-operand will not be evaluated if the result can be determined by the left-operand. For example, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 21givesjava Xxx
49and"Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 23givejava Xxx
48without evaluating the right-operand. This may have adverse consequences if you rely on the right-operand to perform certain operations, e.g. "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 25butboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 62 will not be evaluated.java Xxx
05 and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 09 Concatenation Operator
In Java, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 09 is a special operator. It is overloaded. Overloading means that it carries out different operations depending on the types of its operands.- If both operands are numeric (
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, java Xxx
09), /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 09 performs the usual addition. For examples, - If both operands are
java Xxx
05s, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 09 concatenates the two java Xxx
05s and returns the concatenated java Xxx
05. For examples,"Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" - If one of the operand is a
java Xxx
05 and the other is numeric, the numeric operand will be converted to java Xxx
05 and the two java Xxx
05s concatenated, e.g.,"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2"
We use java Xxx
05 concatenation operator /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 09 frequently in the "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 47 and "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 48 to produce the desired output java Xxx
05. For examples,Flow ControlThere are three basic flow control constructs - sequential, conditional (or decision), and loop (or iteration), as illustrated below. Sequential Flow ControlA program is a sequence of instructions executing one after another in a predictable manner. Sequential flow is the most common and straight-forward, where programming statements are executed in the order that they are written - from top to bottom in a sequential manner. Conditional Flow ControlThere are a few types of conditionals, if-then, if-then-else, nested-if, switch-case-default, and conditional expression. java Xxx
45-then and java Xxx
45-then-java Xxx
46SyntaxExampleFlowchartjava Xxx
0Braces: You could omit the braces "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 6, if there is only one statement inside the block. For example,However, I recommend that you keep the braces to improve the readability of your program, even if there is only one statement in the block. Nested-java Xxx
45SyntaxExampleFlowchartJava does not provide a separate syntax for nested-if (e.g., with keywords like "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 55, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 56), but supports nested-if with nested if-else statements, which is interpreted as below. Take note that you need to put a space between java Xxx
46 and java Xxx
45. Writing "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 56 causes a syntax error.However, for readability, it is recommended to align the nest-if statement as written in the syntax/examples. Take note that the blocks are exclusive in a nested-if statement; only one of the blocks will be executed. Also, there are two ways of writing nested-if, for example, Dangling-java Xxx
46 ProblemThe "dangling- java Xxx
46" problem can be illustrated as follows:The java Xxx
46 clause in the above code is syntactically applicable to both the outer-java Xxx
45 and the inner-java Xxx
45, causing the dangling-java Xxx
46 problem.Java compiler resolves the dangling- java Xxx
46 problem by associating the java Xxx
46 clause with the innermost-java Xxx
45 (i.e., the nearest-java Xxx
45). Hence, the above code shall be interpreted as:Dangling- java Xxx
46 can be prevented by applying explicit parentheses. For example, if you wish to associate the java Xxx
46 clause with the outer-java Xxx
45, do this:Nested-java Xxx
45 vs. Sequential-java Xxx
45Study the following code: "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 75SyntaxExampleFlowchart" "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 75" is an alternative to the "nested-java Xxx
45" for fixed-value tests (but not applicable for range tests). You can use an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, or java Xxx
09 variable as the case-selector, but NOT /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39. JDK 1.7 supports java Xxx
05 as the case-selector.In a "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 87 statement, a "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88 statement is needed for each of the cases. If "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88 is missing, execution will flow through the following case, which is typically a mistake. However, we could use this property to handle multiple-value selector. For example,Conditional Expression ( ... "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 90 ... "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 91 ... )A conditional operator is a ternary (3-operand) operator, in the form of "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 92. Depending on the "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 93, it evaluates and returns the value of "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 94 or "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 95.Conditional expression is a short-hand for "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 96. But you should use it only for one-liner, for readability.Exercises on Getting Started and ConditionalLINK Loop Flow ControlAgain, there are a few types of loops: for, while-do, and do-while. The difference between while-do and do-while lies in the order of the body and test. In while-do, the test is carried out first. The body will be executed if the test is true and the process repeats. In do-while, the body is executed and then the "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 97 is carried out. Take note that the body of do-while is executed at least once (1+); but the body of while-do is possibly zero (0+). Similarly, the for-loop's body could possibly not executed (0+).For-loop is a shorthand for while-do with fewer lines of code. It is the most commonly-used loop especially if the number of repetitions is known. But its syntax is harder to comprehend. Make sure that you understand for-loop by going through the flow-chart and examples. Loop's Index/Counter VariableA loop is typically controlled by an index or counter variable. For example, In the above examples, the variable "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 98 serves as the index variable, which takes on the values java Xxx
18, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 00, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 01, ..., "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 02 for each iteration of the loop. You need to increase/decrease/modify the index variable explicitly (e.g., via "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 03). Otherwise, the loop becomes an endless loop, as the test "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 04 will return the same outcome for the same value of "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 98.Observe that for-loop is a shorthand of while-loop. Both the for-loop and while-loop have the same set of statements, but for-loop re-arranges the statements. For the java Xxx
47-loop, the index variable "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 98 is declared inside the loop, and therefore is only available inside the loop. You cannot access the variable after the loop, as It is destroyed after the loop. On the other hand, for the "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 08-loop, the index variable "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 98 is available inside and outside the loop.For the java Xxx
47-loop, you can choose to declare the index variable inside the loop or outside the loop. We recommend that you declare it inside the loop, to keep the life-span of this variable to where it is needed, and not any longer.[TODO] Animated GIF?? Code Example: Sum and Average of Running IntegersThe following program sums the running integers from a given lowerbound to an upperbound. Also compute their average. Using /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 Flag for Loop ControlBesides using an index variable for loop control, another common way to control the loop is via a /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 flag.Example: Below is an example of using while-do with a /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 flag. The /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 flag is initialized to java Xxx
49 to ensure that the loop is entered.Example: Suppose that your program prompts user for a number between java Xxx
18 to java Xxx
04, and checks for valid input. A do-while loop with a /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 flag could be more appropriate as it prompts for input at least once, and repeat again and again if the input is invalid.java Xxx
47-loop with Comma Separator
You could place more than one statement in the init and update, separated with commas. For example, The test, however, must be a boolean expression that returns a boolean java Xxx
48 or java Xxx
49.Terminating Program"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 22: You could invoke the method "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 22 to terminate the program and return the control to the Java Runtime. By convention, return code of zero indicates normal termination; while a non-zero "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 24 indicates abnormal termination. For example,The "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 25 statement: You could also use a ""The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 25" statement in the "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 0 method to terminate the "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 0 and return control back to the Java Runtime. For example,Exercises on Decision and LoopLINK Formatted Output via ""The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29" (JDK 5)"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 30 and "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 48 do not provide output formatting, such as controlling the number of spaces to print an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 and the number of decimal places for a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7.Java SE 5 introduced a new method called "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29 for formatted output (which is modeled after C Language's "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29). "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29 takes the following form:java Xxx
1Formatting-string contains both normal texts and the so-called Format Specifiers. Normal texts (including white spaces) will be printed as they are. Format specifiers, in the form of " "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 37", will be substituted by the arguments following the "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 38, usually in a one-to-one and sequential manner. A format specifier begins with a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 39 and ends with the "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 40, e.g., "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 41 for integer, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 42 for floating-point number (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7), "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 45 for java Xxx
09 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 47 for java Xxx
05. An optional "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 49 can be inserted in between to specify the field-width. Similarly, an optional "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 50 can be used to control the alignment, padding and others. For examples,"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 51: integer printed in α spaces (α is optional), right-aligned. If α is omitted, the number of spaces is the length of the integer."The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 52: String printed in α spaces (α is optional), right-aligned. If α is omitted, the number of spaces is the length of the string (to fit the string)."The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 53: Floating point number (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7) printed in α spaces with β decimal digits (α and β are optional). If α is omitted, the number of spaces is the length of the floating-point number."The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 56: a system-specific new line (Windows uses "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 57, Unix and macOS "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 58).
Examples:ExampleOutputjava Xxx
2java Xxx
3java Xxx
4java Xxx
5java Xxx
6Take note that "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29 does not advance the cursor to the next line after printing. You need to explicitly print a newline character (via "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 56) at the end of the formatting-string to advance the cursor to the next line, if desires, as shown in the above examples.There are many more format specifiers in Java. Refer to JDK Documentation for the detailed descriptions (@ https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html for JDK 10). (Also take note that "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29 take a variable number of arguments (or varargs), which is a new feature introduced in JDK 5 in order to support "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29)Input From Keyboard via ""The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 63" (JDK 5)Java, like all other languages, supports three standard input/output streams: "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 64 (standard input device), "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 65 (standard output device), and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 66 (standard error device). The "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 64 is defaulted to be the keyboard; while "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 65 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 66 are defaulted to the display console. They can be re-directed to other devices, e.g., it is quite common to redirect "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 66 to a disk file to save these error message.You can read input from keyboard via "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 64 (standard input device).JDK 5 introduced a new class called "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 63 in package "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 73 to simplify formatted input (and a new method "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29 for formatted output described earlier). You can construct a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 63 to scan input from "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 64 (keyboard), and use methods such as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 77, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 78, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 79 to parse the next "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 and java Xxx
05 token (delimited by white space of blank, tab and newline).You can also use method "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 83 to read in the entire line, including white spaces, but excluding the terminating newline.Try not to mix "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 83 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 85 in a program (as you may need to flush the newline from the input buffer).The "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 63 supports many other input formats. Check the JDK documentation page, under module "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 87 ⇒ package "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 73 ⇒ class "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 63 ⇒ Method (@ https://docs.oracle.com/javase/10/docs/api/java/util/Scanner.html for JDK 10).Code Example: Prompt User for Two Integers and Print their SumThe following program prompts user for two integers and print their sum. For examples, java Xxx
7Code Example: Income Tax CalculatorThe progressive income tax rate is mandated as follows: Taxable IncomeRate (%)First $20,0000Next $20,00010Next $20,00020The remaining30For example, suppose that the taxable income is "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 90, the income tax payable is "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 91.Write a program called "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 92 that reads the taxable income (in "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4). The program shall calculate the income tax payable (in "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7); and print the result rounded to 2 decimal places.java Xxx
8Code Example: Income Tax Calculator with SentinelBased on the previous example, write a program called "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 95 which shall repeat the calculations until user enter "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 96. For example,java Xxx
9The "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 96 is known as the sentinel value. (In programming, a sentinel value, also referred to as a flag value, trip value, rogue value, signal value, or dummy data, is a special value which uses its presence as a condition of termination.)Notes: - The coding pattern for handling input with sentinel (terminating) value is as follows:
Code Example: Guess A NumberGuess a number between 0 and 99. Notes: - The above program uses a
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 flag to control the loop, in the following coding pattern:
Exercises on Decision/Loop with InputLINK Input from Text File via ""The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 63" (JDK 5)Other than scanning "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 64 (keyboard), you can connect your "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 63 to scan any input sources, such as a disk file or a network socket, and use the same set of methods "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 77, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 78, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 79, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 83 to parse the next "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, java Xxx
05 and line. For example,To open a file via java Xxx
009, you need to handle the so-called java Xxx
010, i.e., the file that you are trying to open cannot be found. Otherwise, you cannot compile your program. There are two ways to handle this exception: throws or try-catch.To run the above program, create a text file called java Xxx
011 containing:/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 0Formatted Output to Text FileJava SE 5.0 also introduced a so-called java Xxx
012 for formatted output (just like "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 63 for formatted input). A java Xxx
012 has a method called java Xxx
015. The java Xxx
015 method has the same syntax as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29, i.e., it could use format specifiers to specify the format of the arguments. Again, you need to handle the java Xxx
010.Run the above program, and check the outputs in text file " java Xxx
019".Input via a Dialog BoxYou can also get inputs from users via a graphical dialog box, using the java Xxx
020 class. For example, the following program prompts the user to enter the radius of a circle, and computes the area./**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 1Dissecting the Program: - In Line 4, the
java Xxx
021 statement is needed to use the java Xxx
020. - In Line 10, we use the method
java Xxx
023 to prompt users for an input, which returns the input as a java Xxx
05. - Line 11 converts the input
java Xxx
05 to a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, using the method java Xxx
027.
java Xxx
028 (JDK 1.6)
Java SE 6 introduced a new java Xxx
028 class to simplify character-based input/output to/from the system console. BUT, the java Xxx
030 class does not run under IDE (such as Eclipse/NetBeans)!!!To use the new java Xxx
030 class, you first use java Xxx
032 to retrieve the java Xxx
030 object corresponding to the current system console./**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 2You can then use methods such as java Xxx
034 to read a line. You can optionally include a prompting message with format specifiers (e.g., "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 41, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 47) in the prompting message.You can use java Xxx
037 for formatted output with format specifiers such as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 41, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 47. You can also connect the java Xxx
030 to a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 63 for formatted input, i.e., parsing primitives such as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, for example,Example:The java Xxx
030 class also provides a secure mean for password entry via method java Xxx
045. This method disables input echoing and keep the password in a java Xxx
046 instead of a java Xxx
05. The java Xxx
046 containing the password can be and should be overwritten, removing it from memory as soon as it is no longer needed. (Recall that java Xxx
05s are immutable and cannot be overwritten. When they are longer needed, they will be garbage-collected at an unknown instance.)Writing Correct and Good ProgramsIt is important to write programs that produce the correct results. It is also important to write programs that others (and you yourself three days later) can understand, so that the programs can be maintained. I call these programs good programs - a good program is more than a correct program. Here are the suggestions: - Follow established convention so that everyone has the same basis of understanding. To program in Java, you MUST read the "Code Convention for the Java Programming Language".
- Format and layout of the source code with appropriate indents, white spaces and white lines. Use 3 or 4 spaces for indent, and blank lines to separate sections of code.
- Choose good names that are self-descriptive and meaningful, e.g.,
java Xxx
98, java Xxx
051, java Xxx
052, java Xxx
66, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 1. Do not use meaningless names, such as java Xxx
75, java Xxx
76, java Xxx
77, java Xxx
058. Avoid single-alphabet names (easier to type but often meaningless), except common names likes x, java Xxx
95, java Xxx
96 for coordinates and java Xxx
78 for index. - Provide comments to explain the important as well as salient concepts. Comment your code liberally.
- Write your program documentation while writing your programs.
- Avoid unstructured constructs, such as
"Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88 and java Xxx
063, which are hard to follow. - Use "mono-space" fonts (such as Consolas, Courier New, Courier) for writing/displaying your program.
It is estimated that over the lifetime of a program, 20 percent of the effort will go into the original creation and testing of the code, and 80 percent of the effort will go into the subsequent maintenance and enhancement. Writing good programs which follow standard conventions is critical in the subsequent maintenance and enhancement!!! Programming Errors: Compilation, Runtime and Logical ErrorsThere are generally three classes of programming errors: - Compilation Error (or Syntax Error): The program cannot compile. This can be fixed easily by checking the compilation error messages. For examples,
- Runtime Error: The program can compile, but fail to run successfully. This can also be fixed easily, by checking the runtime error messages. For examples,
- Logical Error: The program can compile and run, but produces incorrect results (always or sometimes). This is the hardest error to fix as there is no error messages - you have to rely on checking the output. It is easy to detect if the program always produces wrong output. It is extremely hard to fix if the program produces the correct result most of the times, but incorrect result sometimes. For example, This kind of errors is very serious if it is not caught before production. Writing good programs helps in minimizing and detecting these errors. A good testing strategy is needed to ascertain the correctness of the program. Software testing is an advanced topics which is beyond our current scope.
Debugging ProgramsHere are the common debugging techniques: - Stare at the screen! Unfortunately, nothing will pop-up even if you stare at it extremely hard.
- Study the error messages! Do not close the console when error occurs and pretending that everything is fine. This helps most of the times.
- Insert print statements at appropriate locations to display the intermediate results. It works for simple toy program, but it is neither effective nor efficient for complex program.
- Use a graphic debugger. This is the most effective means. Trace program execution step-by-step and watch the value of variables and outputs.
- Advanced tools such as profiler (needed for checking memory leak and method usage).
- Perform program testing to wipe out the logical errors. "Write test first, before writing program".
Exercises on Decision/Loop with InputLINK Testing Your Program for CorrectnessHow to ensure that your program always produces correct result, 100% of the times? It is impossible to try out all the possible outcomes, even for a simple program for adding two integers (because there are too many combinations of two integers). Program testing usually involves a set of representative test cases, which are designed to catch all classes of errors. Program testing is beyond the scope of this writing. More on Loops - Nested-Loops, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!"88 & java Xxx
063Nested LoopsNested loops are needed to process 2-dimensional (or N-dimensional) data, such as printing 2D patterns. A nested-for-loop takes the following form: Code Examples: Print Square PatternThe following program prompt user for the size of the pattern, and print a square pattern using nested-loops. For example, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 3/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 4This program contains two nested for-loops. The inner loop is used to print a row of " java Xxx
31", which is followed by printing a newline. The outer loop repeats the inner loop to print all the rows.Coding Pattern: Print 2D PatternsThe coding pattern for printing 2D patterns is as follows. I recommend using java Xxx
98 and java Xxx
051 as the loop variables which is self-explanatory, instead of java Xxx
78 and java Xxx
79, java Xxx
74 and java Xxx
95.Code Examples: Print Checker Board PatternSuppose that you want to print this pattern instead (in program called java Xxx
073):/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 5You need to print an additional space for even-number rows. You could do so by adding the following statement before the inner loop. Code Example: Print Multiplication TableThe following program prompts user for the size, and print the multiplication table as follows: /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 6/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 7TRY: - Write programs called
java Xxx
074, which prompts user for the size and prints each these patterns./**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 8Hints: The equations for major and opposite diagonals are java Xxx
075andjava Xxx
076. Decide on what to print above and below the diagonal. - Write programs called
java Xxx
077, which prompts user for the size and prints each of these patterns./**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 9
Exercises on Nested Loops with InputLINK "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88 and java Xxx
063 - Interrupting Loop Flow
The java Xxx
080 statement breaks out and exits the current (innermost) loop.The java Xxx
081 statement aborts the current iteration and continue to the next iteration of the current (innermost) loop."Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88 and java Xxx
063 are poor structures as they are hard to read and hard to follow. Use them only if absolutely necessary.Endless loopjava Xxx
084 is known as an empty for-loop, with empty statement for initialization, test and post-processing. The body of the empty for-loop will execute continuously (infinite loop). You need to use a "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88 statement to break out the loop.Similar, java Xxx
086 and java Xxx
087 are endless loops.Endless loop is typically a mistake especially for new programmers. You need to break out the loop via a "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88 statement inside the loop body..Example ( "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88): The following program lists the non-prime numbers between 2 and an upperbound.Let's rewrite the above program to list all the primes instead. A /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 flag called java Xxx
091 is used to indicate whether the current "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 98 is a prime. It is then used to control the printing.Let's rewrite the above program without using "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88 statement. A "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 08 loop is used (which is controlled by the /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 flag) instead of java Xxx
47 loop with "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88.Example ( java Xxx
063):Example ( "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88 and java Xxx
063): Study the following program.Labeled "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88In a nested loop, the "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88 statement breaks out the innermost loop and continue into the outer loop. At times, there is a need to break out all the loops (or multiple loops). This is clumsy to achieve with /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 flag, but can be done easily via the so-called labeled "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88. You can add a java Xxx
105 to a loop in the form of java Xxx
106. For example,Labeled java Xxx
063In a nested loop, similar to labeled "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88, you can use labeled continue to continue into a specified loop. For example,Again, labeled "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88 and java Xxx
063 are not structured and hard to read. Use them only if absolutely necessary.Example (Labeled "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 88): Suppose that you are searching for a particular number in a 2D array./**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 0java Xxx
05 and java Xxx
09 operationsjava Xxx
09 Arithmetic Operations
Recall that: - In Java, each
java Xxx
09 is represented by a 16-bit Unicode number. For examples,java Xxx
09 /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55 is represented by code number /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 62 (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 63), java Xxx
09 /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 95 by java Xxx
122 (java Xxx
123), java Xxx
09 /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 56 by /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 65 (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 66). java Xxx
09 java Xxx
10 by /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 68 (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 69). Take note that java Xxx
09 /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55 is NOT java Xxx
134, java Xxx
09 /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 95 is NOT java Xxx
137. java Xxx
09s can take part in arithmetic operations. A java Xxx
09 is treated as its underlying "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 (in the range of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 36) in arithmetic operations. In other words, java Xxx
09 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 are interchangeable. java Xxx
144, java Xxx
145, java Xxx
146, java Xxx
147. For examples,- In arithmetic operations,
java Xxx
09 (and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43) is first converted to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4. In Java, arithmetic operations are only carried out in "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46, or "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7; NOT in /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, and java Xxx
09. - Hence,
public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
90, where public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
53 denotes an binary arithmetic operation (such as java Xxx
29, java Xxx
30, java Xxx
31, java Xxx
32 and /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 92). You may need to explicitly cast the resultant "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 back to java Xxx
09. For examples, - Similar,
java Xxx
168. You may need to explicitly cast the resultant "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 back to java Xxx
09. For examples, - However, for compound operators (such as
public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
94, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
95, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
96, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
97, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
98), the evaluation is carried out in "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, but the result is casted back to the LHS automatically. For examples, - For increment (
boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 17) and decrement (boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 18) of java Xxx
09 (and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43), there is no promotion to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4. For examples,
Converting java Xxx
09 to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4You can convert java Xxx
09 /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55 to The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 38 to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 java Xxx
17 to java Xxx
190 by subtracting the java Xxx
09 with the base /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55, e.g., java Xxx
193.That is, suppose java Xxx
77 is a java Xxx
09 between /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55 and The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 38, java Xxx
198 is the corresponding "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 java Xxx
17 to java Xxx
190.The following program illustrates how to convert a hexadecimal character ( The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 45, java Xxx
203 or java Xxx
204) to its decimal equivalent (java Xxx
205), by subtracting the appropriate base java Xxx
09.java Xxx
05 Operations
The most commonly-used java Xxx
05 methods are as follows, suppose that java Xxx
209, java Xxx
210, java Xxx
211 are java Xxx
05 variables:java Xxx
213: return the length of the java Xxx
209.java Xxx
215: return the java Xxx
09 at the java Xxx
217 position of the java Xxx
209. Take note that java Xxx
217 begins at java Xxx
17, and up to java Xxx
221."Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 14: for comparing the contents of java Xxx
210 and java Xxx
211. Take note that you cannot use java Xxx
225 to compare two java Xxx
05s. This is because ""Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 01" is only applicable to primitive types, but java Xxx
05 is not a primitive type.
For examples, To check all the available methods for java Xxx
05, open JDK Documentation ⇒ Select "API documentation" ⇒ Click "FRAMES" (top menu) ⇒ From "Modules" (top-left pane), select ""The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 87" ⇒ From ""The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 87 Packages" (top-left pane), select "java Xxx
232" ⇒ From "Classes" (bottom-left pane), select "java Xxx
05" ⇒ choose "SUMMARY" "METHOD" (right pane) (@ https://docs.oracle.com/javase/10/docs/api/java/lang/String.html for JDK 10).For examples, Converting java Xxx
05 to Primitivejava Xxx
05 to java Xxx
236
You could use the JDK built-in methods java Xxx
237 to convert a java Xxx
05 containing a valid integer literal (e.g., "java Xxx
239") into an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 (e.g., java Xxx
239). The runtime triggers a java Xxx
242 if the input string does not contain a valid integer literal (e.g., "java Xxx
54"). For example,Similarly, you could use methods java Xxx
244, java Xxx
245, java Xxx
246 to convert a java Xxx
05 containing a valid /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43 or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45 literal to the primitive type.java Xxx
05 to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7//**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46
You could use java Xxx
254 or java Xxx
255 to convert a java Xxx
05 (containing a floating-point literal) into a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46, e.g.java Xxx
05 to java Xxx
09
You can use java Xxx
261 to extract individual character from a java Xxx
05, where java Xxx
263 begins at java Xxx
17 and up to java Xxx
265, e.g.,java Xxx
05 to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39
You can use method java Xxx
268 to convert string of "java Xxx
48" or "java Xxx
49" to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 java Xxx
48 or java Xxx
49, e.g.,Converting Primitive to java Xxx
05To convert a primitive to a java Xxx
05, you can:- Use the '
java Xxx
29' operator to concatenate the primitive with an empty java Xxx
277. - Use the JDK built-in methods
java Xxx
278, which is applicable to all primitives. - Use the
java Xxx
279 methods of the respective wrapper class, such as java Xxx
280, java Xxx
281, java Xxx
282, java Xxx
283, etc.
For examples, Formatting java Xxx
05s - java Xxx
285Recall that you can use "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29 to create a formatted string and send it to the display console, e.g.,There is a similar function called java Xxx
285 which returns the formatted string, instead of sending to the console, e.g.,java Xxx
285 has the same form as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29.Code Example: Reverse StringThe following program prompts user a string, and prints the input string in the reverse order. For examples, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 1/**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 2Code Example: Validating Binary StringThe following program prompts user for a string, and checks if the input is a valid binary string, consisting of /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55 and /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 95 only. For example,/**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 3Version 1: With a /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 flag/**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 4Version 2/**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 5This version, although shorter, are harder to read, and harder to maintain. Code Example: Binary to Decimal (Bin2Dec)The following program prompts user for a binary string, and converts into its equivalent decimal number. For example, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 6/**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 7Notes: - The conversion formula is:
/**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 8 - We use
java Xxx
293 to extract each individual java Xxx
09 from the java Xxx
295. The java Xxx
296 begins at zero, and increases from left-to-right. On the other hand, the exponent number increases from right-to-left, as illustrated in the following example:/**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 9 - This code uses
java Xxx
297 as the loop index, and computes the java Xxx
296 for java Xxx
299 using the relationship java Xxx
300. You could also use the java Xxx
296 as the loop index (see next example). - We use the built-in function
java Xxx
302 to compute the exponent, which takes two "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7s and return a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7. We need to explicitly cast the resultant "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 back to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 for java Xxx
307. - There are 3 cases to handle:
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 95 (add to java Xxx
307), /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55 (valid but do nothing for multiply by java Xxx
17) and other (error). We can write the nested-if as follows, but that is harder to read: - You can use
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 63's java Xxx
313 method to read an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 in the desired java Xxx
315. Try reading a binary number (radix of 2) and print its decimal equivalent. For example,
Code Example: Hexadecimal to Decimal (Hex2Dec)The following program prompts user for a hexadecimal string and converts into its equivalent decimal number. For example, The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 0The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 1Notes: - The conversion formula is:
The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 2 - In this example, we use the
java Xxx
316 as the loop index, and compute the exponent via the relationship java Xxx
317 (See the illustration in the earlier example). - You could write a big
java Xxx
318 of 23 java Xxx
319s (The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 45, java Xxx
203, java Xxx
204, and other). But take note how they are reduced to 5 cases.- To convert
java Xxx
323 /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 95 to The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 38 to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 java Xxx
18 to java Xxx
190, we subtract the java Xxx
323 by the base /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55. - Similarly, to convert
java Xxx
323 java Xxx
10 to The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 29 (or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 56 to The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 30) to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 java Xxx
04 to java Xxx
338, we subtract the java Xxx
323 by the base java Xxx
10 (or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 56) and add java Xxx
04.
- You may use
java Xxx
343 to convert the input string to lowercase to further reduce the number of cases. But You need to keep the original java Xxx
05 for output in this example (otherwise, you could use java Xxx
345 directly).
Exercises on java Xxx
05 and java Xxx
09 operationsLINK ArraysSuppose that you want to find the average of the marks for a class of 30 students, you certainly do not want to create 30 variables: java Xxx
348, java Xxx
349, ..., java Xxx
350. Instead, You could use a single variable, called an array, with 30 elements (or items).An array is an ordered collection of elements of the same type, identified by a pair of square brackets java Xxx
351. To use an array, you need to:- Declare the array with a name and a type. Use a plural name for array, e.g.,
java Xxx
352, java Xxx
99, java Xxx
354. All elements of the array belong to the same type. - Allocate the array using
java Xxx
355 operator, or through initialization, e.g.,
When an array is constructed via the java Xxx
355 operator, all the elements are initialized to their default value, e.g., java Xxx
17 for "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Find the sums of the running odd numbers and even numbers from a given lowerbound
* to an upperbound. Also compute their absolute difference.
*/
public class OddEvenSum { // Save as "OddEvenSum.java"
public static void main(String[] args) {
// Declare variables
final int LOWERBOUND = 1;
final int UPPERBOUND = 1000; // Define the bounds
int sumOdd = 0; // For accumulating odd numbers, init to 0
int sumEven = 0; // For accumulating even numbers, init to 0
int absDiff; // Absolute difference between the two sums
// Use a while loop to accumulate the sums from LOWERBOUND to UPPERBOUND
int number = LOWERBOUND; // loop init
while (number <= UPPERBOUND) { // loop test
// number = LOWERBOUND, LOWERBOUND+1, LOWERBOUND+1, ..., UPPERBOUND
// A if-then-else decision
if (number % 2 == 0) { // Even number
sumEven += number; // Same as sumEven = sumEven + number
} else { // Odd number
sumOdd += number; // Same as sumOdd = sumOdd + number
}
++number; // loop update for next number
}
// Another if-then-else Decision
if (sumOdd > sumEven) {
absDiff = sumOdd - sumEven;
} else {
absDiff = sumEven - sumOdd;
}
// OR using one liner conditional expression
//absDiff = (sumOdd > sumEven) ? sumOdd - sumEven : sumEven - sumOdd;
// Print the results
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is: " + sumEven);
System.out.println("The absolute difference between the two sums is: " + absDiff);
}
} 34 for "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7, java Xxx
49 for /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39, and java Xxx
50 for objects. [Unlike C/C++, which does NOT initialize the array contents.]When an array is declared but not allocated, it has a special value called java Xxx
50.Array IndexYou can refer to an element of an array via an index (or subscript) enclosed within the square bracket java Xxx
351. Java's array index begins with zero (java Xxx
17). For example, suppose that java Xxx
352 is an "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 array of 5 elements, then the 5 elements are: java Xxx
369, java Xxx
370, java Xxx
371, java Xxx
372, and java Xxx
373.Array's java Xxx
374To create an array, you need to known the length (or size) of the array in advance, and allocate accordingly. Once an array is created, its length is fixed and cannot be changed during runtime. At times, it is hard to ascertain the length of an array (e.g., how many students?). Nonetheless, you need to estimate the length and allocate an upper bound. Suppose you set the length to 30 (for a class of students) and there are 31 students, you need to allocate a new array (of length 31), copy the old array to the new array, and delete the old array. In other words, the length of an array cannot be dynamically adjusted during runtime. This is probably the major drawback of using an array. (There are other structures that can be dynamically adjusted.) In Java, the length of array is kept in an associated variable called java Xxx
374 and can be retrieved using "java Xxx
376", e.g.,The index of an array is between java Xxx
17 and java Xxx
378.Unlike languages like C/C++, Java performs array index-bound check at the runtime. In other words, for each reference to an array element, the index is checked against the array's java Xxx
374. If the index is outside the range of java Xxx
380, Java Runtime will signal an exception called java Xxx
381. It is important to note that checking array index-bound consumes computation power, which inevitably slows down the processing. However, the benefits gained in terms of good software engineering out-weight the slow down in speed.Array and LoopArrays works hand-in-hand with loops. You can process all the elements of an array via a loop, for example, The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 3Enhanced for-loop (or "for-each" Loop) (JDK 5)JDK 5 introduces a new loop syntax known as enhanced for-loop (or for-each loop) to facilitate processing of arrays and collections. It takes the following syntax: This loop shall be read as "for each element in the array...". The loop executes once for each element in the array, with the element's value copied into the declared variable. The for-each loop is handy to transverse all the elements of an array. It requires fewer lines of code, eliminates the loop counter and the array index, and is easier to read. However, for array of primitive types (e.g., array of "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4s), it can read the elements only, and cannot modify the array's contents. This is because each element's value is copied into the loop's variable, instead of working on its original copy.In many situations, you merely want to transverse thru the array and read each of the elements. For these cases, enhanced for-loop is preferred and recommended over other loop constructs. Code Example: Read and Print ArrayThe following program prompts user for the length and all the elements of an array, and print the array in the form of java Xxx
383. For examples,The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 4The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 5java Xxx
384 (JDK 5)
JDK 5 provides an built-in methods called java Xxx
385, which returns a java Xxx
05 in the form java Xxx
383. You need to java Xxx
388. For examples,Code Example: Horizontal and Vertical HistogramsThe following program prompts user for the number of students, and the grade of each student. It then print the histogram, in horizontal and vertical forms, as follows: The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 6The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 7Notes:- We use two arrays in this exercise, one for storing the grades of the students (of the length
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 1) and the other to storing the histogram counts (of length java Xxx
04). - We use a
java Xxx
04-element "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 arrays called java Xxx
393, to keep the histogram counts for grades of java Xxx
394, java Xxx
395, ..., java Xxx
396. Take note that there are java Xxx
397 grades between java Xxx
398, and the last bin has java Xxx
399 grades (instead of 10 for the rest). The java Xxx
393's index is java Xxx
401, except java Xxx
402 of '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 80.
Code Example: Hexadecimal to Binary (java Xxx
404)The following program prompts user for a hexadecimal string and convert it to its binary equivalence. For example, The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 8The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 9Notes- We keep the binary string corresponding to hex digit
/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55 to The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 30 in an array with indexes of java Xxx
205, used as look-up table. - We extract each
java Xxx
323, find its array index (java Xxx
205), and retrieve the binary string from the array based on the index.- To convert
java Xxx
323 /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 95 to The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 38 to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 java Xxx
18 to java Xxx
190, we subtract the java Xxx
323 by the base /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 55. - Similarly, to convert
java Xxx
323 java Xxx
10 to The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 29 (or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 56 to The sum of odd numbers from 1 to 1000 is: 250000 The sum of even numbers from 1 to 1000 is: 250500 The absolute difference between the two sums is: 500 30) to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 java Xxx
04 to java Xxx
338, we subtract the java Xxx
323 by the base java Xxx
10 (or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 56) and add java Xxx
04.
Code Example: Decimal to Hexadecimal (java Xxx
430)The following program prompts user for an integer, reads as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, and prints its hexadecimal equivalent. For example,public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
0public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
1Notes- We use modulus/divide algorithm to get the hex digits (
java Xxx
205) in reserve order. See "Number System Conversion". - We look up the hex digit
java Xxx
433 from an array using index java Xxx
205.
Exercises on ArraysLINK Multi-Dimensional ArrayIn Java, you can declare an array of arrays. For examples: In the above example, java Xxx
435 is an array of 12 elements. Each of the elements (java Xxx
436 to java Xxx
437) is an 8-element "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 array. In other words, java Xxx
435 is a "12-element array" of "8-element int arrays". Hence, java Xxx
440 gives java Xxx
441 and java Xxx
442 gives java Xxx
443.public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
2To be precise, Java does not support multi-dimensional array directly. That is, it does not support syntax like java Xxx
444 like some languages. Furthermore, it is possible that the arrays in an array-of-arrays have different length.Take note that the right way to view the "array of arrays" is as shown, instead of treating it as a 2D table, even if all the arrays have the same length. For example, public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
3Methods (Functions)Why Methods?At times, a certain portion of code has to be used many times. Instead of re-writing the code many times, it is better to put them into a "subroutine", and "call" this "subroutine" many time - for ease of maintenance and understanding. Subroutine is called method (in Java) or function (in C/C++). The benefits of using methods are: - Divide and conquer: Construct the program from simple, small pieces or components. Modularize the program into self-contained tasks.
- Avoid repeating code: It is easy to copy and paste, but hard to maintain and synchronize all the copies.
- Software Reuse: You can reuse the methods in other programs, by packaging them into library code (or API).
Using MethodsTwo parties are involved in using a method: a caller, who calls (or invokes) the method, and the method called. The process is: - The caller invokes a method and passes arguments to the method.
- The method:
- receives the arguments passed by the caller,
- performs the programmed operations defined in the method's body, and
- returns a result back to the caller.
- The caller receives the result, and continue its operations.
Example: Suppose that we need to evaluate the area of a circle many times, it is better to write a method called java Xxx
445, and re-use it when needed.public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
4The expected outputs are: public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
5In the above example, a reusable method called java Xxx
445 is defined, which receives an argument in "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 from the caller, performs the calculation, and return a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 result to the caller. In the "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 0, we invoke java Xxx
445 methods thrice, each time with a different parameter.Take note that there is a transfer of control from the caller to the method called, and from the method back to the caller, as illustrated. Tracing Method InvocationYou can trace method operations under Eclipse/NetBeans (Refer to the the Eclipse/NetBeans How-to article): - Step Over: Treat the method call as one single step.
- Step Into: Step into the method, so that you can trace the operations of the method.
- Step Out: Complete the current method and return to the caller.
- Set "Breakpoints" inside the method, and "resume" running to the next breakpoint.
Method Definition SyntaxThe syntax for method definition is as follows: public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
6Take note that you need to specify the type of the arguments and the return value in method definition. Calling MethodsTo call a method, simply use java Xxx
451. For examples, to call the above methods:Take note that you need to specify the type in the method definition, but not during invocation. Method Naming ConventionA method's name shall be a verb or verb phrase (action), comprising one or more words. The first word is in lowercase, while the rest are initial-capitalized (called camel-case). For example, java Xxx
445, java Xxx
453, java Xxx
454, java Xxx
455, etc.Another Example: The ""The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 25" statementInside the method body, you could use a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 25 statement to return a value (of the java Xxx
458 declared in the method's signature) to return a value back to the caller. The syntax is:The "java Xxx
459" Return-TypeSuppose that you need a method to perform certain actions (e.g., printing) without a need to return a value to the caller, you can declare its return-value type as java Xxx
459. In the method's body, you could use a "java Xxx
461" statement without a return value to return control to the caller. In this case, the "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 25 statement is optional. If there is no "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 25 statement, the entire body will be executed, and control returns to the caller at the end of the body.Notice that "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 0 is a method with a return-value type of java Xxx
459. "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 0 is called by the Java runtime, perform the actions defined in the body, and return nothing back to the Java runtime.Actual Parameters vs. Formal ParametersRecall that a method receives arguments from its caller, performs the actions defined in the method's body, and return a value (or nothing) to the caller. In the above example, the variable java Xxx
467 declared in the signature of java Xxx
468 is known as formal parameter. Its scope is within the method's body. When the method is invoked by a caller, the caller must supply so-called actual parameters or arguments, whose value is then used for the actual computation. For example, when the method is invoked via "java Xxx
469", java Xxx
470 is the actual parameter, with a value of java Xxx
471.Code Example: Magic NumberThe following program contains a /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 method called java Xxx
473, which returns java Xxx
48 if the given java Xxx
475 contains the digit java Xxx
443, e.g., 18, 108, and 1288. The signature of the method is:public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
7It also provides the "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 0 method to test the java Xxx
478. For example,public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
8public class Hello{public static void main(String[] args){System.out.println("Hello, world!");}}
9Take note of the proper documentation comment for the method. Code Example: "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 Array MethodsThe following program contains various method for "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 array with signatures as follows:It also contains the main() method to test all the methods. For example, '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 0 '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 1Pass-by-Value for Primitive-Type ParametersIn Java, when an argument of primitive type is pass into a method, a copy is created and passed into the method. The invoked method works on the cloned copy, and cannot modify the original copy. This is known as pass-by-value. For example, '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 2Notes: - Although there is a variable called
"Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 98 in both the "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 0 and java Xxx
483 method, there are two distinct copies - one available in "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 0 and another available in java Xxx
483 - happen to have the same name. You can change the name of either one, without affecting the program.
Pass-by-Reference for Arrays and ObjectsAs mentioned, for primitive-type parameters, a cloned copy is made and passed into the method. Hence, the method cannot modify the values in the caller. It is known as pass-by-value. For arrays (and objects - to be described in the later chapter), the array reference is passed into the method and the method can modify the contents of array's elements. It is known as pass-by-reference. For example, Varargs - Method with Variable Number of Formal Arguments (JDK 5)Before JDK 5, a method has to be declared with a fixed number of formal arguments. C-like "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 29, which take a variable number of argument, cannot not be implemented. Although you can use an array for passing a variable number of arguments, it is not neat and requires some programming efforts.JDK 5 introduces variable arguments (or varargs) and a new syntax " java Xxx
487". For example, '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 3Varargs can be used only for the last argument. The three dots ( java Xxx
488) indicate that the last argument may be passed as an array or as a sequence of comma-separated arguments. The compiler automatically packs the varargs into an array. You could then retrieve and process each of these arguments inside the method's body as an array. It is possible to pass varargs as an array, because Java maintains the length of the array in an associated variable java Xxx
374. '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 4Notes: Implicit Type-Casting for Method's ParametersA method that takes a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 parameter can accept any numeric primitive type, such as "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46. This is because implicit type-casting is carried out. However, a method that take a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 parameter cannot accept a "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 value. This is because the implicit type-casting is always a widening conversion which prevents loss of precision. An explicit type-cast is required for narrowing conversion. Read "Type-Casting" on the conversion rules.Method OverloadingIn Java, a method (of a particular method name) can have more than one versions, each version operates on different set of parameters - known as method overloading. The versions shall be differentiated by the numbers, types, or orders of the parameters. Example 1The expected outputs are: '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 5Example 2: ArraysSuppose you need a method to compute the sum of the elements for java Xxx
500, java Xxx
501, java Xxx
502 and java Xxx
503, you need to write all overloaded versions - there is no shortcut.Notes: - Unlike primitives, where
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 would be autocasted to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7 during method invocation, java Xxx
500 is not casted to java Xxx
503. - To handle all the 7 primitive number type arrays, you need to write 7 overloaded versions to handle each array types!
"/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39" MethodsA /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 method returns a /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 value to the caller.Suppose that we wish to write a method called java Xxx
511 to check if a given number is odd. '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 6This seemingly correct code produces java Xxx
49 for java Xxx
513, because java Xxx
514 is "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 96 instead of java Xxx
18. You may rewrite the condition: '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 7The above produces the correct answer, but is poor. For boolean method, you can simply return the resultant /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39 value of the comparison, instead of using a conditional statement, as follow: '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 8Mathematical MethodsJDK provides many common-used Mathematical methods in a class called java Xxx
518. The signatures of some of these methods are:The java Xxx
518 class also provide two constants:To check all the available methods, open JDK API documentation ⇒ select module " "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 87" ⇒ select package "java Xxx
232" ⇒ select class "java Xxx
518" ⇒ choose method (@ https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html for JDK 10).For examples, Exercises on MethodsLINK Command-Line ArgumentsJava's java Xxx
523 method takes an argument: java Xxx
524, i.e., a java Xxx
05 array named java Xxx
526. This is known as "command-line arguments", which corresponds to the augments provided by the user when the java program is invoked. For example, a Java program called java Xxx
527 could be invoked with additional command-line arguments as follows (in a "cmd" shell): '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 9Each argument, i.e., java Xxx
528, java Xxx
529 and java Xxx
530, is a java Xxx
05. Java runtime packs all the arguments into a java Xxx
05 array and passes into the "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 0 method as java Xxx
526. For this example, java Xxx
526 has the following properties:Code Example: java Xxx
527The program java Xxx
527 reads three parameters form the command-line, two integers and an arithmetic operator (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 09, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 10, java Xxx
540, or java Xxx
541), and performs the arithmetic operation accordingly. For example,boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 0boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 1Exercises on Command-Line ArgumentsLINK (Advanced) Bitwise OperationsBitwise Logical OperationsBitwise operators perform operations on one or two operands on a bit-by-bit basis, as follows, in descending order of precedences. OperatorModeUsageDescriptionExample~Unaryjava Xxx
542Bitwise NOT (inversion)&Binaryjava Xxx
543Bitwise AND|Binaryjava Xxx
544Bitwise OR^Binaryboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 90Bitwise XORExampleboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 2Compound operator java Xxx
546, java Xxx
547 and java Xxx
548 are also available, e.g., java Xxx
549 is the same as java Xxx
550.Take note that: java Xxx
551, java Xxx
552 and "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 16 are applicable when both operands are integers ("The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45 and java Xxx
09) or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39s. When both operands are integers, they perform bitwise operations. When both operands are /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39s, they perform logical AND, OR, XOR operations (i.e., same as logical boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 85, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 20 and '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 34). They are not applicable to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7. On the other hand, logical AND (boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 85) and OR ("Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 20) are applicable to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39s only.- The bitwise NOT (or bit inversion) operator is represented as '~', which is different from logical NOT (
java Xxx
569). - The bitwise XOR is represented as
"Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 16, which is the same as logical XOR ( '__'
(oo)
+========\/
/ || %%% ||
* ||-----||
"" "" 34). - The operators' precedence is in this order:
java Xxx
572, java Xxx
551, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 16, java Xxx
552, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 17, java Xxx
577. For example,
Bitwise operations are powerful and yet extremely efficient. [Example on advanced usage.] Bit-Shift OperationsBit-shift operators perform left or right shift on an operand by a specified number of bits. Right-shift can be either signed-extended ( java Xxx
578) (padded with signed bit) or unsigned-extended (java Xxx
579) (padded with zeros). Left-shift is always padded with zeros (for both signed and unsigned).OperatorModeUsageDescriptionExample<<Binaryjava Xxx
580Left-shift and padded with zeros>>Binaryjava Xxx
581Right-shift and padded with sign bit (signed-extended right-shift)>>>Binaryjava Xxx
582Right-shift and padded with zeros (unsigned-extended right-shift)Since all the Java's integers (byte, short, int and long) are signed integers, left-shift << and right-shift >> operators perform signed-extended bit shift. Signed-extended right shift >> pads the most significant bits with the sign bit to maintain its sign (i.e., padded with zeros for positive numbers and ones for negative numbers). Operator >>> (introduced in Java, not in C/C++) is needed to perform unsigned-extended right shift, which always pads the most significant bits with zeros. There is no difference between the signed-extended and unsigned-extended left shift, as both operations pad the least significant bits with zeros. Exampleboolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 3As seen from the example, it is more efficient to use sign-right-shift to perform division by 2, 4, 8... (power of 2), as integers are stored in binary. [More example on advanced usage.] Types and Bitwise OperationsThe bitwise operators are applicable to integral primitive types: /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45 and java Xxx
09. java Xxx
09 is treated as unsigned 16-bit integer. There are not applicable to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 46 and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 7. The java Xxx
551, java Xxx
552, "Hello" + "world" ⇒ "Helloworld"
"Hi" + ", " + "world" + "!" ⇒ "Hi, world!" 16, when apply to two /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39s, perform logical operations. Bit-shift operators are not applicable to /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 39s.Like binary arithmetic operations: /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 42, /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 43 and java Xxx
09 operands are first promoted to "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4.- If both the operands are of the same type (
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4 or /**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45), they are evaluated in that type and returns a result of that type. - If the operands are of different types, the smaller operand (
"The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 4) is promoted to the larger one (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45). It then operates on the larger type (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45) and returns a result in the larger type (/**
* Comment to state the purpose of the program
*/
public class Classname { // Choose a meaningful Classname. Save as "Classname.java"
public static void main(String[] args) { // Entry point of the program
// Your programming statements here!!!
}
} 45).
AlgorithmsBefore writing a program to solve a problem, you have to first develop the steps involved, called algorithm, and then translate the algorithm into programming statements. This is the hardest part in programming, which is also hard to teach because the it involves intuition, knowledge and experience. An algorithm is a step-by-step instruction to accomplice a task, which may involve decision and iteration. It is often expressed in English-like pseudocode, before translating into programming statement of a particular programming language. There is no standard on how to write pseudocode - simply write something that you, as well as other people, can understand the steps involved, and able to translate into a working program. Algorithm for Prime TestingAncient Greek mathematicians like Euclid and Eratosthenes (around 300-200 BC) had developed many algorithms (or step-by-step instructions) to work on prime numbers. By definition, a prime is a positive integer that is divisible by one and itself only. To test whether a number java Xxx
74 is a prime number, we could apply the definition by dividing java Xxx
74 by 2, 3, 4, ..., up to java Xxx
608. If no divisor is found, then java Xxx
74 is a prime number. Since divisors come in pair, there is no need to try all the factors until x-1, but up to java Xxx
610.TRY: translate the above pseudocode into a Java program called java Xxx
611.Algorithm for Perfect NumbersA positive integer is called a perfect number if the sum of all its proper divisor is equal to its value. For example, the number java Xxx
612 is perfect because its proper divisors are java Xxx
18, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 00, and "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 01, and java Xxx
616; but the number java Xxx
04 is not perfect because its proper divisors are java Xxx
18, "The number is " + 5 ⇒ "The number is " + "5" ⇒ "The number is 5"
"The average is " + average + "!" (suppose average=5.5) ⇒ "The average is " + "5.5" + "!" ⇒ "The average is 5.5!"
"How about " + a + b (suppose a=1, b=1) ⇒ "How about 1" + b ⇒ "How about 11" (left-associative)
"How about " + (a + b) (suppose a=1, b=1) ⇒ "How about " + 2 ⇒ "How about 2" 00, and java Xxx
620, and java Xxx
621. Other perfect numbers are java Xxx
622, java Xxx
623, ...The following algorithm can be used to test for perfect number: TRY: translate the above pseudocode into a Java program called java Xxx
624.Algorithm on Computing Greatest Common Divisor (GCD)Another early algorithm developed by ancient Greek mathematician Euclid (300 BC) is to find the Greatest Common Divisor (GCD) (or Highest Common Factor (HCF)) of two integers. By definition, java Xxx
625 is the largest factor that divides both java Xxx
75 and java Xxx
76.Assume that java Xxx
75 and java Xxx
76 are positive integers and java Xxx
630, the Euclidean algorithm is based on these two properties:boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 4For example, boolean done = true;
boolean gameOver = false;
boolean isValid;
isValid = false; 5The Euclidean algorithm is as follows: Before explaining the algorithm, suppose we want to exchange (or swap) the values of two variables java Xxx
74 and java Xxx
95. Explain why the following code does not work.To swap the values of two variables, we need to define a temporary variable as follows: Let us look into the Euclidean algorithm, java Xxx
633, if java Xxx
76 is java Xxx
17. Otherwise, we replace java Xxx
75 by java Xxx
76; java Xxx
76 by java Xxx
639, and compute java Xxx
640. Repeat the process until the second term is java Xxx
17. Try this out on pencil-and-paper to convince yourself that it works. |