Part 2

advertisement
Chapter 2: Elementary Programming
Shahriar Hossain
1
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 and
Decrement Operators
Operator
++var
Name
preincrement
var++
postincrement
--var
predecrement
var--
postdecrement
Description
The expression (++var) increments var by 1 and evaluates
to the new value in var after the increment.
The expression (var++) evaluates to the original value
in var and increments var by 1.
The expression (--var) decrements var by 1 and evaluates
to the new value in var after the decrement.
The expression (var--) evaluates to the original value
in var and decrements var by 1.
4
Increment and
Decrement 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 and
Decrement Operators, cont.
What is the output of the following code segment?
int i=2;
int k=i+++i; // equivalent to
System.out.println(i);
System.out.println(k);
int k=(i++)+i;
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++;
6+7+7
System.out.println(i);
System.out.println(a);
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;
5+7+8
System.out.println(i);
System.out.println(a);
20
8
8
Exercise
What is the output of the following code segment?
int a = 5,i;
i=++a + ++a + a++;
//Ans: i=6+7+7=20 then a=8
i=a++ + ++a + ++a;
//Ans: i=8+10+11=29 then a=11
a=++a + ++a + a++;
//Ans: a=12+13+13=38
System.out.println(a); //Ans: a=38
System.out.println(i); //Ans: i=29
9
We discussed
F
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
F
Is this a correct statement?
System.out.println("He said "Java is fun" ");
F
F
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.
Next-line
style
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
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 errorprone:
– 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
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
Download