Class 1 ~ Chapter 1

Download Report

Transcript Class 1 ~ Chapter 1

Class 7
1
Objectives
Identify, declare, and use primitive
data types
 Use variables in programs to hold
data in RAM
 Use assignment statements to store
data with proper identifiers
 Use operators and parentheses
correctly in numeric expressions

2
Introduction
Data are collections of raw facts or
figures
 A program performs operations on
input data to output information
 Input data can come from a variety of
sources
– The program itself
– Users of the program
– External files

3
Variables
Variables are
like storage
locations in
the computer’s
memory.
4
Naming Rules of Variables

First character must be one of the letters
a-z, A-Z, or an underscore ( _ ) or a $

After first character use a-z, A-Z, 0-9,
underscore ( _ ) or $

Any length

Keep them meaningful
5
Variable Rules (con’t)

Case sensitive
– ItemsOrder does not equal
itemsorder

Cannot declare two variables of the
same name in a method

Cannot give a variable the same
name as a method
6
Storing Data

Java is a strongly typed language
– Variables must be declared with a data type
– Variable locations can hold only that data type
 Java has two categories of data types
– Primitive data types hold single data items
• Integers, characters, floating point, and
booleans are primitive types
– Reference data types hold a value that refers to
the location of the data
• All Objects and arrays are reference types
7
Intrinsic Data Types
JAVA has eight intrinsic data
types, which form the basis for all
other data types (i.e., classes):
1. boolean:
- a 1 byte value that is assigned
a value of either true or false
(both are JAVA defined values).
Boolean values have no direct
conversion to integer values,
and are initialized to false.
8
Intrinsic Data Types
2. byte:
- a 1 byte integer, capable of
representing numbers from
numbers from -128 to +127.
It is initialized to 0.
3. char:
- a 2 byte integer that is normally
used to represent character values
using the Unicode system (the
first 128 values of which
correspond to ASCII values).
It is initialized to \u0000.
9
Intrinsic Data Types
4. short:
- a 2 byte integer, able to represent
numbers between -32K and +32K.
It is initialized to 0.
5. int:
- a 4 byte integer, able to represent
numbers between -2 billion and
+2 billion.
It is initialized to 0.
10
Intrinsic Data Types
6. long:
- an 8 byte integer, capable of
representing numbers between
-2 63 and +2 63 .
It is initialized to 0.
7. float:
- a 4 byte IEEE format real number,
giving about 7 decimal digits
of precision.
It is initialized to 0.0f.
11
Intrinsic Data Types
8. double:
- an 8 byte IEEE format real number,
giving about 15 decimal digits
of precision.
It is initialized to 0.0d.
12
Declaring Variables
General form:
1. dataType identifier;
2. dataType identifier, identifier, identifier;
3. dataType identifier = initialValue;
4. dataType identifier = new dataType();
int myAge;
int myAge, yourAge, theirAges;
int myAge = 24;
Person me = new Person();
13
Key JAVA Operators
Operator
()
Description
Parenthesis
Example
(X+2)/3
14
Key JAVA Operators
Operator
*
Description
Multiply
Example
X*2
15
Key JAVA Operators
Operator
/
Description
Divide
Example
X/12
16
Key JAVA Operators
Operator
%
Description
Modulus
Example
7%3
17
Key JAVA Operators
Operator
+
Description
Add
Example
X+7
18
Key JAVA Operators
Operator
-
Description
Subtract
Example
X-6
19
Key JAVA Operators
Operator
=
Description
Assignment operator
Example
Y=X+3
20
Arithmetic Operators

The order of operator precedence is a
predetermined order that defines the
sequence in which operators are
evaluated in an expression
 Addition, subtraction, multiplication, and
division can manipulate any numeric data
type
 When Java performs math on mixed data
types, the result is always the larger data
type
 Casts allow programmers to force a
conversion from one primitive type to
another
21
Numeric Expressions

Numeric expressions evaluate to a number
 Only numeric primitive data types may be used
in a numeric expression
 A value and variable must be separated by an
arithmetic operator
 Unless parentheses supercede, an expression
is evaluated left to right with the following rules
of precedence:
– Multiplication and/or division
– Integer division
– Modular division
22
– Addition and/or subtraction
Parentheses in Expressions

Parentheses may be used to change the
order of operations
– The part of the expression within the
parentheses is evaluated first
 Parentheses can provide clarity in
complex expressions
– Numeric and conditional expressions
should be grouped with parentheses
 Parentheses can be nested
– Java evaluates the innermost
expression first and then moves on to
the outermost expression
23
How precedence works
x = (3+8)/ 3 * 2 + 5 % 3
x = ? 11 / 3
x =
3
* 2 + 5 % 3
x =
6
x =
6
x =
* 2 + 5 % 3
+ 5 % 3
+
2
8
24
Assignment Statements

General syntax:
identifier = value;
x = 5;
dataType identifier = value;
int x = 6;
identifier = formula;
x = 3 * y+5;
identifier = differentIdentifier;
x = y;
25
Using Variables in Output to the
console instead of to a window
System.out.println(“Hello World”);
System.out.println(5);
System.out.print(“my age is \n“ + age);
System.out.println(“
“);
String concatenation operator
26
Assignment
Statements
The value in number is number
_
Exercise 1
int number;
5?
number
number = 5;
System.out.println(“The value in number is ”
+ “number”) ;
27
Assignment
Statements
The value in number is 5
_
Exercise 2
int number;
5?
number
number = 5;
System.out.println(“The value in number is ”
+ number) ;
28
Multiple Assignment Statements
Tracing Code: exercise 3
int Checking;
int Miles;
long Days;
Checking = -20;
Miles = 4276;
Days = 187000;
?
187000
Days
?
-20
?
4276
Checking
Miles
29
System.out.println( “We have made a”
+ “ long trip of ” + Miles + “ miles.”);
System.out.print(“Our checking account”
+ “ balance is ”
+ “Checking”);
System.out.print(“\nExactly ” +
Days + “ days ago Columbus”
+ “ stood on this spot.”);
-20
4276
Checking
Miles
187000
Days
30
Multiple Assignment Statements
Tracing Code: Exercise 4
double RegWages, BasePay = 18.25;
double RegHours = 40.0;
double OTWages, OTPay = 27.78;
double OTHours = 10;
?.?
?.?
double TotalWages;
RegWages
OTWages
?.?
TotalWages
18.25
BasePay
27.78
OTPay
40.0
10
RegHours
OTHours
31
Basic usage of operators
RegWages = BasePay * RegHours;
OTWages = OTPay * OTHours;
TotalWages = RegWages + OTWages;
System.out.println( “Wages for this”
+ “ week are $ ” +
730.0
?.?
?.?
277.8
TotalWages );
RegWages
OTWages
?.?
1007.8
18.25
27.78
40.0
10
TotalWages
BasePay
OTPay
RegHours
OTHours
PG 66 ~ Pgrm. 2-20
32
Write assignment statements
A) Adds 2 to A and stores the
result in B.
B = A + 2;
33
Write assignment statements
B) Multiplies B times 4 and
stores the result in A.
A = B * 4;
34
Write assignment statements
C) Divides A by 3.14 and stores
the result in B.
B = A / 3.14;
35
Write assignment statements
D) Subtracts 8 from B and
stores the result in A.
A = B - 8;
36
Write assignment statements
E) Stores the value 27 in A.
A = 27;
37
What is this program’s output
int Freeze = 32, Boil = 212;
Freeze = 0;
Boil = 100;
System.out.print(Freeze + “\n “ +
Boil + “\n”);
0
100
_
38
What is this program’s output
int X = 0, Y = 2;
X = Y * 4;
System.out.println( “” + X + “\n “ +Y );
8
2
_
39
System.out.print(“I am the incredible” +
“ computing\nmachine ” +
“\nand I will\namaze\n” +
“you.”);
40
I am the incredible computing
machine
and I will
amaze
you.
_
41
What is this program’s output
System.out.println( “Be careful\n” +
“This might/n be a trick ” +
“question”);
Be careful
This might/n be a trick question
_
42
What is this program’s output
int A, X = 23;
A = X % 2;
System.out.println( X);
System.out.println( A );
23
1
_
43
Conclusion
of Class 7
44