Top Banner
1 Class 7 Class 7
44

1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

Dec 17, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

1

Class 7Class 7

Page 2: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

2

ObjectivesObjectivesIdentify, declare, and use primitive

data types Use variables in programs to hold

data in RAMUse assignment statements to store

data with proper identifiersUse operators and parentheses

correctly in numeric expressions

Page 3: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

3

IntroductionIntroductionData are collections of raw facts or figuresA program performs operations on input

data to output informationInput data can come from a variety of

sources

– The program itself

– Users of the program

– External files

Page 4: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

4

VariablesVariables

Variables are Variables are like storage like storage locations in locations in the computer’s the computer’s memory.memory.

Page 5: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

5

Naming Rules of VariablesNaming Rules of VariablesFirst 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

Page 6: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

6

Variable Rules (con’t)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

Page 7: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

7

Storing DataStoring 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

Page 8: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

8

Intrinsic Data TypesIntrinsic Data TypesJAVA 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.

Page 9: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

9

Intrinsic Data TypesIntrinsic Data Types2. 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.

Page 10: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

10

Intrinsic Data TypesIntrinsic Data Types4. 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.

Page 11: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

11

Intrinsic Data TypesIntrinsic Data Types6. 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

Page 12: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

12

Intrinsic Data TypesIntrinsic Data Types

8. double:- an 8 byte IEEE format real number, giving about 15 decimal digits of precision. It is initialized to 0.0d.

Page 13: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

13

Declaring VariablesDeclaring VariablesGeneral 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();

Page 14: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

14

Key JAVA OperatorsKey JAVA OperatorsOperator

Description

Example ( )

Parenthesis

(X+2)/3

Page 15: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

15

Key JAVA OperatorsKey JAVA OperatorsOperator

Description

Example *

Multiply

X*2

Page 16: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

16

Key JAVA OperatorsKey JAVA OperatorsOperator

Description

Example /

Divide

X/12

Page 17: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

17

Key JAVA OperatorsKey JAVA OperatorsOperator

Description

Example %

Modulus

7%3

Page 18: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

18

Key JAVA OperatorsKey JAVA OperatorsOperator

Description

Example +

Add

X+7

Page 19: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

19

Key JAVA OperatorsKey JAVA OperatorsOperator

Description

Example -

Subtract

X-6

Page 20: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

20

Key JAVA OperatorsKey JAVA OperatorsOperator

Description

Example =

Assignment operator

Y=X+3

Page 21: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

21

Arithmetic OperatorsArithmetic 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

Page 22: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

22

Numeric ExpressionsNumeric 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

Page 23: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

23

Parentheses in ExpressionsParentheses 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

Page 24: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

24

How precedence worksHow precedence works

x = ( 3 + 8 ) / 3 * 2 + 5 % 3

x = ? / 3

x = 3 * 2

* 2 + 5 % 3

+ 5 % 3

x = + 5 % 36

x =

x =

6 + 2

8

11

Page 25: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

25

Assignment StatementsAssignment StatementsGeneral syntax:

identifier = value;

x = 5;

dataType identifier = value;

int x = 6;

identifier = formula;

x = 3 * y+5;

identifier = differentIdentifier;

x = y;

Page 26: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

26

Using Variables in Output to the Using Variables in Output to the console instead of to a windowconsole 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

Page 27: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

27

number

?

Assignment Assignment StatementsStatements

Exercise 1Exercise 1

5

The value in number is number_

int number;int number;

number = 5;number = 5; System.out.println(“The value in number is System.out.println(“The value in number is ” ” + “number”) ; + “number”) ;

Page 28: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

28

number

?

Assignment Assignment StatementsStatements

Exercise 2Exercise 2

5

The value in number is 5_

int number;int number;

number = 5;number = 5; System.out.println(“The value in number is System.out.println(“The value in number is ” ” + number) ; + number) ;

Page 29: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

29

Checking

?

Multiple Assignment StatementsMultiple Assignment StatementsTracing Code: exercise 3Tracing Code: exercise 3

-20

Days

??

Miles

?4276

187000 int Checking;int Checking; int Miles;int Miles; long Days;long Days; Checking = -20;Checking = -20; Miles = 4276;Miles = 4276; Days = 187000;Days = 187000;

Page 30: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

30

Days

187000

Checking

-20

Miles

4276

System.out.println( “We have made a”System.out.println( “We have made a” + “ long trip of ” + Miles + “ miles.”);+ “ long trip of ” + Miles + “ miles.”); System.out.print(“Our checking account”System.out.print(“Our checking account” + “ balance is ”+ “ balance is ” + “Checking”);+ “Checking”); System.out.print(“\nExactly ” +System.out.print(“\nExactly ” + Days + “ days ago Columbus”Days + “ days ago Columbus” + “ stood on this spot.”); + “ stood on this spot.”);

Page 31: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

31

RegWages

?.?

OTWages

?.?

OTHours

10

RegHours

40.0OTPay

27.78

BasePay

18.25

TotalWages

?.?

double RegWages, BasePay = 18.25;double RegWages, BasePay = 18.25; double RegHours = 40.0;double RegHours = 40.0; double OTWages, OTPay = 27.78;double OTWages, OTPay = 27.78; double OTHours = 10;double OTHours = 10; double TotalWages;double TotalWages;

Multiple Assignment StatementsMultiple Assignment StatementsTracing Code: Exercise 4Tracing Code: Exercise 4

Page 32: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

32

Basic usage of operatorsBasic usage of operators

RegWages

?.?

OTWages

?.?

OTHours

10

RegHours

40.0

OTPay

27.78

BasePay

18.25

TotalWages

?.?

277.8

1007.8

RegWages = BasePay * RegHours;RegWages = BasePay * RegHours; OTWages = OTPay * OTHours;OTWages = OTPay * OTHours; TotalWages = RegWages + OTWages;TotalWages = RegWages + OTWages; System.out.println( “Wages for this”System.out.println( “Wages for this” + “ week are $ ” ++ “ week are $ ” + TotalWages );TotalWages ); 730.0

PG 66 ~ Pgrm. 2-20

Page 33: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

33

Write assignment statementsWrite assignment statements

A) AddsA) Adds 2 2 to to A A and stores the and stores the result inresult in B. B.

B = A + 2;B = A + 2;

Page 34: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

34

Write assignment statementsWrite assignment statements

B) MultipliesB) Multiplies B B times times 4 4 and and stores the result instores the result in A A..

A = B * 4;A = B * 4;

Page 35: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

35

Write assignment statementsWrite assignment statements

C) DividesC) Divides A A byby 3.14 3.14 and stores and stores the result inthe result in B B..

B = A / 3.14;B = A / 3.14;

Page 36: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

36

Write assignment statementsWrite assignment statements

D) SubtractsD) Subtracts 8 8 fromfrom B B and and stores the result instores the result in A. A.

A = B - 8;A = B - 8;

Page 37: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

37

Write assignment statementsWrite assignment statements

E) Stores the valueE) Stores the value 27 27 in in AA..

A = 27;A = 27;

Page 38: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

38

What is this program’s outputWhat is this program’s output

int Freeze = 32, Boil = 212;int Freeze = 32, Boil = 212; Freeze = 0;Freeze = 0; Boil = 100;Boil = 100; System.out.print(Freeze + “\n “ +System.out.print(Freeze + “\n “ + Boil + “\n”);Boil + “\n”);

0100_

Page 39: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

39

What is this program’s outputWhat is this program’s output int X = 0, Y = 2;int X = 0, Y = 2; X = Y * 4;X = Y * 4; System.out.println( “” + X + “\n “ +Y );System.out.println( “” + X + “\n “ +Y );

82_

Page 40: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

40

System.out.print(“I am the incredible” +System.out.print(“I am the incredible” + “ “ computing\nmachine ” +computing\nmachine ” + “ “\nand I will\namaze\n” +\nand I will\namaze\n” + “ “you.”);you.”);

Page 41: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

41

I am the incredible computingmachineand I willamazeyou._

Page 42: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

42

What is this program’s outputWhat is this program’s output System.out.println( “Be careful\n” +System.out.println( “Be careful\n” + “ “This might/n be a trick ” +This might/n be a trick ” + “ “question”); question”);

Be carefulThis might/n be a trick question_

Page 43: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

43

What is this program’s outputWhat is this program’s output

int A, X = 23;int A, X = 23; A = X % 2;A = X % 2; System.out.println( X);System.out.println( X); System.out.println( A );System.out.println( A );

231_

Page 44: 1 Class 7 2 Objectives Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements.

44

Conclusion Conclusion of Class 7of Class 7