Class 7 - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

Class 7

Description:

Title: Class 1 ~ Chapter 1 Author: Cheryl Masker Created Date: 7/9/1998 5:01:18 PM Document presentation format: On-screen Show (4:3) Company: Collin County Community ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 45
Provided by: Cheryl281
Category:

less

Transcript and Presenter's Notes

Title: Class 7


1
Class 7
2
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

3
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

4
Variables
Variables are like storage locations in the
computers memory.
Storage for the value of the variable
5
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

6
Variable Rules (cont)
  • 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

7
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

8
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.
9
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.
10
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.
11
Intrinsic Data Types
6. long - an 8 byte integer, capable of
representing numbers between -2 and 2
. 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.
63
63
12
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.
13
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()
14
Key JAVA Operators
  • Operator
  • Description
  • Example

( ) Parenthesis (X2)/3
15
Key JAVA Operators
  • Operator
  • Description
  • Example

Multiply X2
16
Key JAVA Operators
  • Operator
  • Description
  • Example

/ Divide X/12
17
Key JAVA Operators
  • Operator
  • Description
  • Example

Modulus 73
18
Key JAVA Operators
  • Operator
  • Description
  • Example

Add X7
19
Key JAVA Operators
  • Operator
  • Description
  • Example

- Subtract X-6
20
Key JAVA Operators
  • Operator
  • Description
  • Example

Assignment operator YX3
21
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

22
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
  • Addition and/or subtraction

23
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

24
How precedence works
x ( 3 8 ) / 3 2 5 3
x
/ 3
2 5 3
11
?
x
3
2
5 3
x
5 3
6
x
6

2
x
8
25
Assignment Statements
  • General syntax
  • identifier value
  • x 5
  • dataType identifier value
  • int x 6
  • identifier formula
  • x 3 y5
  • identifier differentIdentifier
  • x y

26
Using Variables in Output to the console instead
of to a window
System.out.println(Hello World) System.out.prin
tln(5) System.out.print(my age is \n
age) System.out.println( )
String concatenation operator
27
Assignment StatementsExercise 1
The value in number is number _
int number number 5 System.out.println(
The value in number is
number)
5
28
Assignment StatementsExercise 2
The value in number is 5 _
int number number 5 System.out.println(
The value in number is
number)
5
29
Multiple Assignment StatementsTracing Code
exercise 3
int Checking int Miles long Days
Checking -20 Miles 4276 Days 187000

187000
-20
4276
30
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.)
31
Multiple Assignment StatementsTracing Code
Exercise 4
double RegWages, BasePay 18.25 double
RegHours 40.0 double OTWages, OTPay
27.78 double OTHours 10 double TotalWages
32
Basic usage of operators
RegWages BasePay RegHours OTWages
OTPay OTHours TotalWages RegWages
OTWages System.out.println( Wages for this
week are TotalWages )
730.0
277.8
1007.8
PG 66 Pgrm. 2-20
33
Write assignment statements
A) Adds 2 to A and stores the result in B.
B A 2
34
Write assignment statements
B) Multiplies B times 4 and stores the result in
A.
A B 4
35
Write assignment statements
C) Divides A by 3.14 and stores the result in B.
B A / 3.14
36
Write assignment statements
D) Subtracts 8 from B and stores the result in A.
A B - 8
37
Write assignment statements
E) Stores the value 27 in A.
A 27
38
What is this programs output
int Freeze 32, Boil 212 Freeze 0
Boil 100 System.out.print(Freeze \n
Boil \n)
0 100 _
39
What is this programs output
int X 0, Y 2 X Y 4
System.out.println( X \n Y )
8 2 _
40
System.out.print(I am the incredible
computing\nmachine \nand I
will\namaze\n you.)
41
I am the incredible computing machine and I
will amaze you. _
42
What is this programs output
System.out.println( Be careful\n This
might/n be a trick question)
Be careful This might/n be a trick question _
43
What is this programs output
int A, X 23 A X 2
System.out.println( X) System.out.println( A
)
23 1 _
44
Conclusion of Class 7
Write a Comment
User Comments (0)
About PowerShow.com