chapter 2: elementary programming shahriar hossain

27
1 Chapter 2: Elementary Programming Shahriar Hossain

Upload: gladys

Post on 05-Jan-2016

46 views

Category:

Documents


0 download

DESCRIPTION

Chapter 2: Elementary Programming Shahriar Hossain. Augmented Assignment Operators. The operators +, -, *, /, and % can be combined with the assignment operator to form augmented operators. OperatorExampleEquivalent +=i += 8i = i + 8 -=f -= 8.0f = f - 8.0 *=i *= 8i = i * 8 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter  2:  Elementary  Programming Shahriar Hossain

1

Chapter 2: Elementary Programming

Shahriar Hossain

Page 2: Chapter  2:  Elementary  Programming Shahriar Hossain

2

Page 3: Chapter  2:  Elementary  Programming Shahriar Hossain

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.

Page 4: Chapter  2:  Elementary  Programming Shahriar Hossain

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.

Page 5: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 6: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 7: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 8: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 9: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 10: Chapter  2:  Elementary  Programming Shahriar Hossain

We discussed

In the last class, we discussed– Type conversion/Type casting– Character variable

10

Page 11: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 12: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 13: Chapter  2:  Elementary  Programming Shahriar Hossain

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;

Page 14: Chapter  2:  Elementary  Programming Shahriar Hossain

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.

Page 15: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 16: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 17: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 18: Chapter  2:  Elementary  Programming Shahriar Hossain

18

Programming Style and Documentation (Chapter 1, section 1.10)

Appropriate Comments Naming Conventions Proper Indentation and Spacing

Lines Block Styles

Page 19: Chapter  2:  Elementary  Programming Shahriar Hossain

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.

Page 20: Chapter  2:  Elementary  Programming Shahriar Hossain

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.

Page 21: Chapter  2:  Elementary  Programming Shahriar Hossain

21

Proper Indentation and Spacing

Indentation– Indent two spaces.

Spacing – Use blank line to separate segments of the code.

Page 22: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 23: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 24: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 25: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 26: Chapter  2:  Elementary  Programming Shahriar Hossain

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

Page 27: Chapter  2:  Elementary  Programming Shahriar Hossain

Java Programming Style

avoid long lines since they're not handled well by many terminals.

27