280 likes | 543 Views
Chapter 2: Elementary Programming Shahriar Hossain. Augmented Assignment Operators. The operators +, -, *, /, and % can be combined with the assignment operator to form augmented operators. Operator Example Equivalent += i += 8 i = i + 8 -= f -= 8.0 f = f - 8.0 *= i *= 8 i = i * 8
E N D
Augmented Assignment Operators The operators +, -, *, /, and % can be combined with the assignment operator to form augmented 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
Increment andDecrement Operators Operator Name Description ++var preincrement The expression (++var) increments var by 1 and evaluates to the new value in varafter 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 varafter the decrement. var-- postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1.
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); 3 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); 3 5
More examples (1) 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); 6 + 7 + 7 20 8
More examples (2) 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); 5+7+8 20 8
Exercise 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
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 be System.out.println("He said \"Java is fun\" ");
Casting between char and Numeric Types int i = 'a'; // Same as int i = (int)'a'; char c = 97; // Same as char c = (char)97;
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.
String Concatenation // Three strings are concatenated String message = "Welcome " + "to " + "Java"; // String Chapter is concatenated with number 2 String s = "Chapter" + 2; // s becomes Chapter2 // String Supplement is concatenated with character B String s1 = "Supplement" + 'B'; // s1 becomes SupplementB
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”.
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”.
Programming Style and Documentation (Chapter 1, section 1.10) • Appropriate Comments • Naming Conventions • Proper Indentation and Spacing Lines • Block Styles
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.
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.
Proper Indentation and Spacing • Indentation • Indent two spaces. • Spacing • Use blank line to separate segments of the code.
Block Styles Use end-of-line style for braces.
Additional Resource on Programming Styles • Please read the “Java programming style” link in the following site: • http://www.cs.utep.edu/vladik/cs1401.14/
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
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;
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;
Java Programming Style • avoid long lines since they're not handled well by many terminals.