1 / 27

Chapter 2: Elementary Programming Shahriar Hossain

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

gladys
Download Presentation

Chapter 2: Elementary Programming Shahriar Hossain

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2: Elementary ProgrammingShahriar Hossain

  2. 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

  3. 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.

  4. 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

  5. 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

  6. 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

  7. 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

  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

  9. We discussed • In the last class, we discussed • Type conversion/Type casting • Character variable

  10. 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

  11. 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\" ");

  12. Casting between char and Numeric Types int i = 'a'; // Same as int i = (int)'a'; char c = 97; // Same as char c = (char)97;

  13. 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.

  14. 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

  15. 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”.

  16. 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”.

  17. Programming Style and Documentation (Chapter 1, section 1.10) • Appropriate Comments • Naming Conventions • Proper Indentation and Spacing Lines • Block Styles

  18. 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.

  19. 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.

  20. Proper Indentation and Spacing • Indentation • Indent two spaces. • Spacing • Use blank line to separate segments of the code.

  21. Block Styles Use end-of-line style for braces.

  22. 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.

More Related