Download presentation

Presentation is loading. Please wait.

1
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data types *Assignment *Expressions

2
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Numeric Data and Operations *So far, our programs have used only String data *It is common to use computers for numeric calculations *We can write literal numbers into our programs - just write the number *We need a way to store numeric data that can change as the program runs

3
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Variables *In mathematical expressions, we often use x and y to represent unknown values and to make a placeholder for many different values y = x 2 + 5 *A variable is used in a similar way in a program A variable has a Java identifier for a name.

4
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Variables *In the last chapter, we declared variables whose location stored the address of an object *For numeric data, a declaration sets aside memory locations to store the actual data. These memory locations are called variables, and x and y are the names we associate with the memory locations. *As before, we need to say what kind of data the variable represents.

5
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Variables *A variable has three properties: A memory location to store the value. The type of data stored in the memory location. The name used to refer to the memory location.

6
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Variable Declarations *The syntax for declaring variables is ; *where is a sequence of identifiers separated by commas. *Every variable we use in a program must be declared before it can be used.

7
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Primitive Types *There are two different kinds of variable in Java Object types - variable stores a reference to the actual data Primitive types - variable stores the actual value of the data Numeric data - six types Boolean data - boolean (Chapter 5) Character data - char (Chapter 9)

8
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Numeric Types *There are six numeric data types in Java: We need to be able to use two basic kinds of numbers. We have several types for each kind of number to allow for different ranges of values. *Integers have discrete values (whole numbers). byte short int long *Real numbers are needed for values that have fractional parts. float double

9
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Assignment *First we need to declare a variable. int a; * We assign a value to a variable by using an assignment statement. a = 5; *At the time a variable is declared, it can also be initialized. int count = 10, height = 34; *Do not confuse mathematical equality and assignment. *The following is not valid Java code: 4 + 5 = x;

10
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Variables for Objects and Numbers *The only difference between a variable for numbers and a variable for objects is the contents in the memory locations. For numbers, a variable contains the numeric value itself. For objects, a variable contains an address where the object is stored.

11
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Memory diagram for numeric data *For numeric data, the actual value is stored in the memory location associated with the variable

12
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Object Variables *For objects, the location (memory address) for the object is stored in the memory location associated with the variable

13
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Comparison of Numeric and Object Variables

14
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Variables *We use the new command to create an object. Objects are called reference data types, because the contents are addresses that refer to memory locations where the objects are actually stored. *Numerical data are called primitive data types. We don't need to use new with primitive types. You do have to give the primitive variable a value.

15
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Assignment *Numeric Data*Objects

16
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Arithmetic Expressions OperationOperatorExpressionResult Addition+x + y17 Subtraction-y - z5 Multiplication*x * y70 Division/x / z2 Modulo (remainder) %x % y3 Assume x=10, y=7, z=2

17
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Division and Modulo *Division works differently for integer and floating point types for floating point types you get a floating point result (what your calculator would give you) for integers, you get an integer result integer division and modulo together are what you first learned when you learned division in grade school 27 divided by 6 is 4 with a remainder of 3 so 27 / 6 gives a result of 4 and 27 % 6 gives a result of 3

18
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Examples: What is 3 % 2 6 % 2 8 / 3 8 % 3 5 / 7 5 % 7 j * (i / j) + i % j

19
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Evaluation order *In an expression with more than one binary operator, we need rules to tell us what order to do them in What is 5 + x * 16 / y + 5 ? *Precedence rules tell us which of two different operations should get done first What is 5 + 4 * 3 ? *Associativity rules tell us which order operations of the same type get done in What is 16 / 2 / 2 ?

20
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Precedence rules *for arithmetic operators and parentheses OrderGroupOperatorRule firstSubexpression()innermost first left to right Unary operation+, -single operand multiplicative operators *, /, %left to right lastadditive operators +, -left to right

21
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Evaluation of Expressions *Subexpression evaluation x + 3 * y

22
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Examples : What is 3 + 5 / 7 (3 + 5) / 7 3 * 3 + 3 % 2 3 + 2 / 5 + -2 * 4 1 / 2 * ( 3 + 5) 7 * 5 / 4 * 2 *How would you take a length in inches and convert it to whole feet and inches? e.g. 56 in. = 5 ft. 6 in.

23
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Mixed-mode Arithmetic *What happens when the operands in your expression have different types? x = 3.45 / 2 *The hardware supports only single- type operations *The value of a variable has to be stored in the appropriate format. *Type conversions are used to convert mixed-type expressions to single type expressions

24
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Type Conversions *Widening conversions happen automatically promote a value from one type into another which can represent a larger range of values converting an int to a double *Narrowing conversions have to be programmed explicitly information will be lost converting a double to an int cast operator is the name of the type of the result enclosed in parentheses (int)2.34 fractional part will be truncated

25
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Rules for arithmetic promotion *Unary Operators 1.byte and short operands are converted to int *Binary Operators 1. If either operand has type double, the other will be converted to a double 2. Otherwise, if either operand has type float, the other will be converted to a float 3. Otherwise, if either operand has type long, the other will be converted to a long 4. Otherwise, both operands are converted to int will be converted to an int

26
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Examples: What is (3 + 6) / 5 (3 + 6) / (double) 5 1.0 / 2 * ( 3 + 6) (int) 1.999999

27
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Constants *If we want a variable to remain fixed, we use a constant. *A constant is declared in a manner similar to a variable, but with the additional reserved word final. *final double PI = 3.14159; *final int MONTHS_IN_YEAR = 12;

28
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Literal Constants *If a literal constant contains a decimal point, it is of type double by default. *To designate a literal constant of type float, append a letter f or F to the number: 2 * PI * 345.79F

29
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Examples: What is *1.602 x 10 -19 *6.02 x 10 23 *2.38 x 10 14

30
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Scientific Notation *Numbers in scientific notation, such as mantissa x 10 exponent *are expressed in Java using the syntax E 12.40e+209 29.009E-102

31
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. The Math Class *The Math class in the java.lang package contains class methods for commonly used mathematical functions. *Some methods available in the Math class: sqrt, abs, round sin, cos, asin, … * Math has the constant Math.PI in it

32
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Using the Math Class *Math.PI is a public constant that contains the value of (see Ch3Circle3.java) area = 2 * Math.PI * radius * radius; *Use Math.sqrt() to get the square root of a number root = Math.sqrt( b * b - 4 * a * c) / (2.0 * a); *Use Math.round() to round a double to the nearest long int percent = (int)(Math.round(0.01 * score));

33
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Ch3PoleHeight.java *Want to calculate the height of a flag pole from a distance.

34
COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Take another measurement

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google