Chapter 1 Introduction to Java

Download Report

Transcript Chapter 1 Introduction to Java

Lecture #2
By
Dr. Basheer M. Nasef
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef
Chapter2:
Elementary Programming
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef
2
Motivations
In the preceding chapter, you learned how to create,
compile, and run a Java program.
Starting from this chapter, you will learn how to solve
practical problems programmatically. Through these
problems, you will learn Java primitive data types and
related subjects, such as variables, constants, data types,
operators, expressions, and input and output.
L2:CSC210 2014-2015 ©
3
Dr. Basheer M. Nasef
Objectives
 To write Java programs to perform simple calculations.
 To use identifiers to name variables, constants, methods, and classes.
 To use variables to store data.
 To program with assignment statements and assignment expressions.
 To use constants to store permanent data.
 To declare Java primitive data types: byte, short, int, long, float, double,
and char.
L2:CSC210 2014-2015 ©
4
Dr. Basheer M. Nasef
Introducing Programming with an
Example
Computing the Area of a Circle
This program computes the area of
the circle.
ComputeArea
Run
IMPORTANT NOTE: To enable the buttons, you must
store the entire slide file Link_Files files into a directory
(e.g., e:\Link_Files_CSC210) .
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef
5
Trace a Program Execution
public class ComputeArea {
/** Main method */
public static void main(String[] args) {
double radius;
double area;
allocate memory
for radius
radius
no value
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius
"+
radius + " is " + area);
}
}
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef
6
Trace a Program Execution
public class ComputeArea {
/** Main method */
public static void main(String[] args) {
double radius;
double area;
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
memory
radius
no value
area
no value
allocate memory
for area
// Display results
System.out.println("The area for the circle of radius
"+
radius + " is " + area);
}
}
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef
7
Trace a Program Execution
public class ComputeArea {
/** Main method */
public static void main(String[] args) {
double radius;
double area;
assign 20 to radius
radius
area
20
no value
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius
"+
radius + " is " + area);
}
}
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef
8
Trace a Program Execution
public class ComputeArea {
/** Main method */
public static void main(String[] args) {
double radius;
double area;
memory
radius
area
20
1256.636
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
compute area and assign it
to variable area
// Display results
System.out.println("The area for the circle of radius
"+
radius + " is " + area);
}
}
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef
9
Trace a Program Execution
public class ComputeArea {
/** Main method */
public static void main(String[] args) {
double radius;
double area;
memory
radius
area
20
1256.636
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
print a message to the
console
// Display results
System.out.println("The area for the circle of radius
"+
radius + " is " + area);
}
}
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef 10
Obtaining Input
In this course provides two ways of obtaining input.
Using the JDK 1.8 Scanner class .
2. Using JOptionPane input dialogs.
1.
L2:CSC210 2014-2015 ©
11
Dr. Basheer M. Nasef
Getting Input Using Scanner
1. Create a Scanner object
Scanner input= new Scanner(System.in);
2. Use the methods
L2:CSC210 2014-2015 ©
12
Dr. Basheer M. Nasef
Getting Input Using Scanner
For example:
L2:CSC210 2014-2015 ©
13
Dr. Basheer M. Nasef
Getting Input Using Scanner
For example,
System.out.print("Enter a double value: ");
Scanner scanner = new Scanner(System.in);
double d = scanner.nextDouble();
TestScanner
L2:CSC210 2014-2015 ©
14
Run
Dr. Basheer M. Nasef
Identifiers
 An identifier is a sequence of characters that consist
of letters, digits, underscores (_), and dollar signs ($).
 An identifier must start with a letter, an underscore
(_), or a dollar sign ($). It cannot start with a digit.
 An identifier cannot be a reserved word. (See Appendix A,
“Java Keywords,” for a list of reserved words).
 An identifier cannot be true, false, or null.
 An identifier can be of any length.
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef 15
Identifiers
 For example, $2, ComputeArea, area, radius, and
showMessageDialog are legal identifiers,
 whereas 2A and d+4 are not because they do not
follow the rules.
 The Java compiler detects illegal identifiers and
reports syntax errors.
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef 16
Variables
// Compute the first area
radius = 1.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ +
area + " for radius "+radius);
// Compute the second area
radius = 2.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ +
area + " for radius "+radius);
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef 17
Declaring Variables
int x;
// Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a;
L2:CSC210 2014-2015 ©
// Declare a to be a
// character variable;
Dr. Basheer M. Nasef 18
Assignment Statements
x = 1;
// Assign 1 to x;
radius = 1.0;
// Assign 1.0 to radius;
a = 'A';
// Assign 'A' to a;
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef 19
Declaring and Initializing
in One Step
 int x = 1;
 double d = 1.4;
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef 20
Constants
final datatype CONSTANTNAME = VALUE;
final double PI = 3.14159;
final int SIZE = 3;
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef 21
Numerical Data Types
Name
Range
Storage Size
byte
–27 (-128) to 27–1 (127)
8-bit signed
short
–215 (-32768) to 215–1 (32767)
16-bit signed
int
–231 (-2147483648) to 231–1 (2147483647) 32-bit signed
long
–263 to 263–1
(i.e., -9223372036854775808
to 9223372036854775807)
64-bit signed
float
Negative range:
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
32-bit IEEE 754
double
Negative range:
-1.7976931348623157E+308 to
-4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308
64-bit IEEE 754
L2:CSC210 2014-2015 ©
Dr. Basheer M. Nasef 22
Numeric Operators
Name
Meaning
Example
Result
+
Addition
34 + 1
35
-
Subtraction
34.0 – 0.1
33.9
*
Multiplication
300 * 30
9000
/
Division
1.0 / 2.0
0.5
%
Remainder
20 % 3
2
L2:CSC210 2014-2015 ©
23
Dr. Basheer M. Nasef
Integer Division
+, -, *, /, and %
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
5 % 2 yields 1 (the remainder of the division)
L2:CSC210 2014-2015 ©
24
Dr. Basheer M. Nasef
Remainder Operator
Remainder is very useful in programming. For example, an
even number % 2 is always 0 and an odd number % 2 is
always 1. So you can use this property to determine whether
a number is even or odd. Suppose today is Saturday and
you and your friends are going to meet in 10 days. What
day is in 10 days? You can find that day is Tuesday using
the following expression:
Saturday is the 6th day in a week
A week has 7 days
(6 + 10) % 7 is 2
The 2nd day in a week is Tuesday
After 10 days
L2:CSC210 2014-2015 ©
25
Dr. Basheer M. Nasef
Problem: Displaying Time
Write a program that obtains hours and
minutes from seconds.
Run
DisplayTime
L2:CSC210 2014-2015 ©
26
Dr. Basheer M. Nasef
Number Literals
A literal is a constant value that appears directly
in the program. For example, 34, 1,000,000, and
5.0 are literals in the following statements:
int i = 34;
long x = 1000000;
double d = 5.0;
L2:CSC210 2014-2015 ©
28
Dr. Basheer M. Nasef
Scientific Notation
Floating-point literals can also be specified in
scientific notation, for example, 1.23456e+2,
same as 1.23456e2, is equivalent to 123.456, and
1.23456e-2 is equivalent to 0.0123456.
E (or e) represents an exponent and it can be
either in lowercase or uppercase.
L2:CSC210 2014-2015 ©
31
Dr. Basheer M. Nasef
Arithmetic Expressions
3  4 x 10( y  5)(a  b  c)
4 9 x

 9( 
)
5
x
x
y
is translated to
(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
L2:CSC210 2014-2015 ©
32
Dr. Basheer M. Nasef
How to Evaluate an Expression
Though Java has its own way to evaluate an
expression behind the scene, the result of a Java
expression and its corresponding arithmetic expression
are the same. Therefore, you can safely apply the
arithmetic rule for evaluating a Java expression.
3 + 4 * 4 + 5 * (4 + 3) - 1
3 + 4 * 4 + 5 * 7 – 1
3 + 16 + 5 * 7 – 1
(1) inside parentheses first
(2) multiplication
(3) multiplication
3 + 16 + 35 – 1
19 + 35 – 1
54 - 1
53
L2:CSC210 2014-2015 ©
33
(4) addition
(5) addition
(6) subtraction
Dr. Basheer M. Nasef
Example:
L2:CSC210 2014-2015 ©
34
Dr. Basheer M. Nasef
Problem: Converting Temperatures
Write a program that converts a Fahrenheit degree to
Celsius using the formula:
celsius  ( 95 )( fahrenheit 32)
FahrenheitToCelsius
L2:CSC210 2014-2015 ©
35
Run
Dr. Basheer M. Nasef
Shortcut 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
-=
L2:CSC210 2014-2015 ©
36
Dr. Basheer M. Nasef
Increment and
Decrement 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.
L2:CSC210 2014-2015 ©
37
Dr. Basheer M. Nasef
Increment and
Decrement Operators, cont.
int i = 10;
int newNum = 10 * i++;
Same effect as
int i = 10;
int newNum = 10 * (++i);
L2:CSC210 2014-2015 ©
38
int newNum = 10 * i;
i = i + 1;
Same effect as
i = i + 1;
int newNum = 10 * i;
Dr. Basheer M. Nasef
Numeric Type Conversion
Consider the following statements:
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
L2:CSC210 2014-2015 ©
39
Dr. Basheer M. Nasef
Conversion Rules
When performing a binary operation involving
two operands of different types, Java automatically
converts the operand based on the following rules:
1.
If one of the operands is double, the other is
converted into double.
2. Otherwise, if one of the operands is float, the
other is converted into float.
3. Otherwise, if one of the operands is long, the
other is converted into long.
4. Otherwise, both operands are converted into int.
L2:CSC210 2014-2015 ©
40
Dr. Basheer M. Nasef
Type Casting
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is
truncated)
What is wrong?
int x = 5 / 2.0;
range increases
byte, short, int, long, float, double
L2:CSC210 2014-2015 ©
41
Dr. Basheer M. Nasef
Casting between char and
Numeric Types
int i = 'a'; // Same as int i = (int)'a';
char c = 97; // Same as char c = (char)97;
L2:CSC210 2014-2015 ©
42
Dr. Basheer M. Nasef
Problem: Keeping Two Digits After
Decimal Points
Write a program that displays the sales tax with two digits
after the decimal point.
SalesTax
L2:CSC210 2014-2015 ©
43
Run
Dr. Basheer M. Nasef