1 Chapter 2: Elementary Programming Shahriar Hossain
Jan 05, 2016
1
Chapter 2: Elementary Programming
Shahriar Hossain
2
3
Augmented Assignment Operators
Operator Example Equivalent
+= i += 8 i = i + 8
-= f -= 8.0 f = f - 8.0
*= i *= 8 i = i * 8
/= i /= 8 i = i / 8
%= i %= 8 i = i % 8
The operators +, -, *, /, and % can be combined with the assignment operator to form augmented operators.
4
Increment andDecrement Operators
Operator Name Description++var preincrement The expression (++var) increments var by 1
and evaluates to the new value in var after the increment.
var++ postincrement The expression (var++) evaluates to the original value
in var and increments var by 1. --var predecrement The expression (--var) decrements var by 1
and evaluates to the new value in var after the decrement.
var-- postdecrement The expression (var--) evaluates to the original value
in var and decrements var by 1.
5
Increment andDecrement Operators, cont.
What is the output of the following code segment?
int i=2;int k=++i+i;System.out.println(i);System.out.println(k);
36
6
Increment andDecrement Operators, cont.
What is the output of the following code segment?
int i=2;int k=i+++i; // equivalent to int k=(i++)+i;System.out.println(i);System.out.println(k);
35
More examples (1)
7
What is the output of the following code segment?
int i=0;int a=5;i=++a + ++a + a++;System.out.println(i);System.out.println(a);
208
6 + 7 + 7
More examples (2)
8
What is the output of the following code segment?
int i=0;int a=5;i=a++ + ++a + ++a;System.out.println(i);System.out.println(a);
208
5+7+8
Exercise
9
What is the output of the following code segment?
int a = 5,i;i=++a + ++a + a++;
i=a++ + ++a + ++a;
a=++a + ++a + a++;
System.out.println(a);
System.out.println(i);
//Ans: i=6+7+7=20 then a=8
//Ans: i=8+10+11=29 then a=11
//Ans: a=12+13+13=38
//Ans: a=38
//Ans: i=29
We discussed
In the last class, we discussed– Type conversion/Type casting– Character variable
10
11
Escape Sequences for Special Characters
Description Escape Sequence Unicode
Backspace \b \u0008
Tab \t \u0009
Linefeed \n \u000A
Carriage return \r \u000D
Backslash \\ \u005C
Single Quote \' \u0027
Double Quote \" \u0022
Example
Is this a correct statement?System.out.println("He said "Java is fun" ");
No. The statement above will give a compiler error
The correct statement will beSystem.out.println("He said \"Java is fun\" ");
12
13
Casting between char and Numeric Types
int i = 'a'; // Same as int i = (int)'a';
char c = 97; // Same as char c = (char)97;
14
The String Type The char type only represents one character. To represent a string of characters, use the data type called String. For example, String message = "Welcome to Java"; String is actually a predefined class in the Java library just like the System class and JOptionPane class. The String type is not a primitive type. It is known as a reference type. Any Java class can be used as a reference type for a variable. Reference data types will be thoroughly discussed in Chapter 8, “Objects and Classes.” For the time being, you just need to know how to declare a String variable, how to assign a string to the variable, and how to concatenate strings.
15
String Concatenation // Three strings are concatenatedString message = "Welcome " + "to " + "Java"; // String Chapter is concatenated with number 2String s = "Chapter" + 2; // s becomes Chapter2 // String Supplement is concatenated with character BString s1 = "Supplement" + 'B'; // s1 becomes SupplementB
16
Converting Strings to Integers
The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns “123”. To obtain the input as a number, you have to convert a string into a number. To convert a string into an int value, you can use the static parseInt method in the Integer class as follows: int intValue = Integer.parseInt(intString); where intString is a numeric string such as “123”.
17
Converting Strings to Doubles
To convert a string into a double value, you can use the static parseDouble method in the Double class as follows:
double doubleValue =Double.parseDouble(doubleString);
where doubleString is a numeric string such as “123.45”.
18
Programming Style and Documentation (Chapter 1, section 1.10)
Appropriate Comments Naming Conventions Proper Indentation and Spacing
Lines Block Styles
19
Appropriate Comments
Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses.
Include your name, class section, instructor, date, and a brief description at the beginning of the program.
20
Naming Conventions
Choose meaningful and descriptive names. Class names:
– Capitalize the first letter of each word in the name. For example, the class name ComputeExpression.
21
Proper Indentation and Spacing
Indentation– Indent two spaces.
Spacing – Use blank line to separate segments of the code.
22
Block Styles
Use end-of-line style for braces.
public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } }
public class Test { public static void main(String[] args) {
System.out.println("Block Styles"); } }
End-of-line style
Next-line style
Additional Resource on Programming Styles
Please read the “Java programming style” link in the following site:– http://www.cs.utep.edu/vladik/cs1401.14/
23
Java Programming Style
General advice on how to make your program more readable and less error-prone:– every class should be in a separate file;– use meaningful names for the variables and for the file
names;– use comments;
comments should explain what a piece of code intends to do; in particular, every method -- even a simple one -- should, as a minimum, have comments explaining what exactly it does, and what is the meaning of all its parameters
24
Java Programming Style
comments should not duplicate the text of the program; for example, "initialize the variables" is a good comment, but a comment "assign the value 0 to the variable x" after a line x = 0; does not add any additional meaning to the code;
– always use {} for blocks corresponding to "if", "while", and "for" statements, even if the corresponding block consists of only one statement;
25
Java Programming Style
– use indentation; every time you have a method within a class, a group of
statements within an if-else construction or a loop -- anything with {} -- indent this group of statements;
do not indent too much, since then you will run out of space; Java recommends 4 spaces; our textbook often uses 3; 2 spaces is also OK;
try not to use Tab because Tab may look different on different screens and different editors -- especially if you sometimes use Tab and sometimes, simply add a few blank spaces;
26
Java Programming Style
avoid long lines since they're not handled well by many terminals.
27