introduction to computing concepts

25
Introduction to Computing Concepts Note Set 7

Upload: fagan

Post on 23-Feb-2016

44 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Computing Concepts. Note Set 7. Overview. Variables Data Types Basic Arithmetic Expressions Arithmetic. Memory in a computer. Computer uses a few levels of memory to hold data that it is using in processing. . RAM. 3366. 3368. 3370. 3372. 3374. 3376. Data. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Computing Concepts

Introduction to Computing ConceptsNote Set 7

Page 2: Introduction to Computing Concepts

Overview

•Variables•Data Types•Basic Arithmetic Expressions▫Arithmetic

Page 3: Introduction to Computing Concepts

Memory in a computer•Computer uses a few levels of memory to hold data

that it is using in processing.

3366

3368

3370

3372

3374

3376

RAM

Page 4: Introduction to Computing Concepts

Data•Collection of raw facts or figures•Perform some processing on data in order to obtain

information▫Example Data: Test Grades▫Example Information: Class Average, Standard

Deviation, etc. •Data (and info.) usually has an intrinsic Data Type▫Numerical Data (test grades)? String Data (names)?

99Computer Science

3.1415927 Bob, Sam, Jane

Page 5: Introduction to Computing Concepts

Data in Java•Need some way to store data/info while program is

running/processing•For this, we use variables▫Allow storage a piece of data

Will become “larger” or “smarter” as the semester progresses

▫Each has a particular data type

Page 6: Introduction to Computing Concepts

Data Types•To efficiently use the computer’s memory, each

variable has a data type▫Behind the scenes, tells the compiler and JVM how to

use memory to store a pieces of data E.g. A number is stored differently than an integer

• Java is a Strongly Typed Language▫The languages enforces rules as to what you can do

with the data in variables▫E.g. Won’t necessarily let you do this?

“Bob” + 27

Doesn’t necessarily make sense to add a string and number…

Page 7: Introduction to Computing Concepts

2 Categories of Data Types•Primitive Data Types▫Holds a single data item such as integer, character, or

true/false value•Reference Data Types▫Data type whose value is actually a memory address

Page 8: Introduction to Computing Concepts

Primitive Data TypesData Type Description Example Values

int Stores positive and negative whole numbers in the range of 231 to 231-1

293845000

long Stores positive and negative whole numbers in the range of approx -9*1018 to 9*1018-1

1357924687405*1010

short Stores positive and negative whole numbers in the range from -32768 to 32767

619-23

float stores numbers with up to 6 or 7 decimals(Single Precision)

349.1353.1415

double Stores numbers with up to 14 or 15 decimal places (Double Precision)

3.141592653589792.3

boolean Stores data in only one of two states: True or False true, false

byte Stores positive and negative whole numbers in the range -128 to 127

75-14

char stores any one of the 65,436 single characters from the Unicode character set

‘a’ ‘;’‘M’ ‘.’

Page 9: Introduction to Computing Concepts

Declaring Variables

•declaration statement ▫Line of code that identifies, or declares, the type and

names the identifier or variable.

General Form: SomeDataType SomeIdentifierName;

int grade; long atomsInBody;

double Average;

float price;

Page 10: Introduction to Computing Concepts

Declaring Variables – Identifier Names

• Rules for names of Identifiers1. must start with letter, underscore or dollar sign ($)2. subsequent characters can be letters, underscores, dollar signs, or

digits (0 – 9)3. Any Length (but be reasonable)4. Name should be meaningful

(no variables named x, y, z)5. Remember – Java is Case Sensitive6. Can’t declare 2 variables with the same name in the same method7. Can’t have a variable that is a reserved word or the same name as a

method

General Form: SomeDataType SomeIdentifierName;

Where have we seen these rules before?

Page 11: Introduction to Computing Concepts

In Code

public class TestVariables { public static void main (String [] args) { int grade1 = 100; int grade2, grade3; float average; //Other stuff here

}}

Notice that you can declare the variable and provide an initial value all in one statement.

Page 12: Introduction to Computing Concepts

check point•Declare a variable to hold your GPA

•Declare a variable to hold your age (in years)

Page 13: Introduction to Computing Concepts

Assignment Statements

•Used to store a new value in a variable•uses the assignment operator (=)

userAge = 21;

boolean flag

newUserAge = userAge;

sum = grade1 + grade2 + grade;

float average = sum / 3.0;

Page 14: Introduction to Computing Concepts

Assignment Statements

•= is right associative▫Means always stores what’s on the right side in the variable

on the left side▫Will evaluate expression on right first (step 1), then perform

the assignment

int grade1 = 98;int grade2 = 100;int sum = grade1 + grade2;

Step 1 Evaluates to 198

Step 2 Stores 198 in sum

Page 15: Introduction to Computing Concepts

Variables in Output

•Can use a variable in an output statement

•Can concatenate a string literal and variable with plus sign

userAge = 21;

System.out.println(userAge);

System.out.println(“Age is “ + userAge);

Page 16: Introduction to Computing Concepts

Important Arithmetic Operators in JavaOperation Symbol Example Result

Addition + 3+4 7

Subtraction - 4-3 1

Multiplication * 4 * 3 12

Division / 5.0/2.0 2.5

Integer Division / 5/2 2

Modular Division % 20 % 3 2 (only the

remainder is stored)

Cast (dataType) literal or identifier (int)20.3 20

(truncates decimal)

Page 17: Introduction to Computing Concepts

Important Arithmetic Operators in JavaOperation Symbol Example Result

Addition + 3+4 7

Subtraction - 4-3 -1

Multiplication * 4 * 3 12

Division / 5.0/2.0 2.5

Integer Division / 5/2 2

Modular Division % 20 % 3 2 (only the remainder is stored)

Cast (dataType) literal or identifier (int)20.3 20

(truncates decimal)

Might Be New Math Ideas

Page 18: Introduction to Computing Concepts

check point•What is the result of?▫3 / 2

▫2 / 3

▫5 % 4

▫4 % 5

Page 19: Introduction to Computing Concepts

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

Page 20: Introduction to Computing Concepts

Numeric Expressions•Any Expression that can be evaluated to a number•Can include operators, literal values, variables,

method calls•Only primitive data types may participate in numeric

expressions ▫Any method calls we use in a numeric expression will

return a primitive data type. •A literal value and a variable (or 2 variables) must be

separated by an arithmetic operator

Page 21: Introduction to Computing Concepts

What if multiple operators in 1 expression?

•Follow the order of Precedence▫Unless parentheses dictate otherwise, evaluate

expressions in the following order Multiplication and/or Division Integer Division Modular Division Addition and/or subtraction

▫When multiple operations of the same kind are present, Java performs the left to right

18 / 3 – 2 + 4 * 2

Page 22: Introduction to Computing Concepts

Let’s Evaluate

18 / 3 – 2 + 4 * 2

6 – 2 + 4 * 2

6 – 2 + 8

4 + 8

12

Page 23: Introduction to Computing Concepts

Parentheses in Expressions•Change the order of operations•when found in an expression, the part inside the

parentheses is evaluated first. Then the rest of the expression is evalutated

• If they are nested, then the inner-most set of parentheses is evaluated first

Page 24: Introduction to Computing Concepts

Let’s Evaluate

18 / (3 – 2) + 4 * 2

18 / 1 + 4 * 2

18 + 4 * 2

18 + 8

26

Page 25: Introduction to Computing Concepts

check point – Your Turn!

18 / 3 * 2 + (3 * (2 + 5))