Chap 03

Information about Chap 03

Published on November 17, 2007

Author: Arundel0

Source: authorstream.com

Content

Slide1:  Chapter 3 Arithmetic Expressions Chapter 3 Topics:  Chapter 3 Topics Overview of Java Data Types Numeric Data Types Declarations for Numeric Expressions Simple Arithmetic Expressions Compound Arithmetic Expressions Additional Mathematical Methods Value-Returning Class Methods Additional String Operations Applications with Multiple Class File Slide3:  Java Data Types reference array interface class primitive integral boolean byte char short int long floating point float double Primitive and Reference Types:  Primitive and Reference Types letter char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; Primitive and Reference Types:  Primitive and Reference Types letter title char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; Primitive and Reference Types:  Primitive and Reference Types letter title book char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; Primitive and Reference Types:  Primitive and Reference Types letter title book char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; ‘J’ Primitive and Reference Types:  Primitive and Reference Types letter title book char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; 2003 “Problem Solving” Memory Location 2003 ‘J’ Primitive and Reference Types:  Primitive and Reference Types letter title book char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; 2003 “Problem Solving” Memory Location 2003 2003 ‘J’ Primitive and Reference Types:  Primitive and Reference Types letter title book char letter; String title; String book; letter = ‘J’; title = “Problem Solving”; book = title; 2003 “Problem Solving” Memory Location 2003 2003 ‘J’ Primitive Data Types in Java:  Primitive Data Types in Java Integral Types can represent whole numbers and their negatives when declared as short, int, or long can represent single characters when declared as char Floating Point Types represent real numbers with a decimal point declared as float or double Samples of Java Data Values:  Samples of Java Data Values int sample values 4578 -4578 0 double sample values 95.274 95. .265 char sample values ‘B’ ‘d’ ‘4’ ‘?’ ‘*’ Exponential (Scientific) Notation:  Exponential (Scientific) Notation 2.7E4 means 2.7 x 10 4 = 2.7000 = 27000.0 2.7E-4 means 2.7 x 10 - 4 = 0002.7 = 0.00027 More About Floating Point Types:  More About Floating Point Types Floating-point types have an integer part and a fractional part, with a decimal point in between; either the integer part or the fractional part, but not both, may be missing EXAMPLES 18.4 500. .8 -127.358 Alternatively, floating point values can have an exponent, as in scientific notation; the number preceding the letter E doesn’t need to include a decimal point. EXAMPLES 1.84E1 5E2 8E-1 -.127358E3 Recall ...:  Recall ... Named constant declaration final double PI = 3.14159; final String HOME = “Texas”; final int TEXAS_TEMP = 95; Variable declaration String address; char initial; int dailyTemp; What is an Arithmetic Expression?:  What is an Arithmetic Expression? An arithmetic expression is a valid arrangement of variables, constants, operators and parentheses An expression can be evaluated to compute a value of a given type The value of the expression 9.3 * 4.5 is 41.85 Division Operator:  Division Operator The result of the division operator depends on the type of its operands If one or both operands has a floating type, the result is a floating point type (float or double); otherwise, the result is an integral type EXAMPLES 11 / 4 has value 2 11.0 / 4.0 has value 2.75 11 / 4.0 has value 2.75 Modulus Operator:  Modulus Operator The modulus operator % when used with integer type operands has an integer type result Its result is the integer type remainder of an integer division EXAMPLE 11 % 4 has value 3 because ) 4 11 2 and Remainder = ? Some Java Operators:  Some Java Operators Precedence Operator Description Higher () Parentheses + Positive - Negative * Multiplication / Division % Modulus (remainder) + Addition - Subtraction Lower = Assignment Precedence:  Precedence Precedence determines which operator is applied first in an expression having several operators Associativity:  Associativity Left to right Associativity means that in an expression having 2 operators with the same priority, the left operator is applied first In Java, the binary operators * , / , % , + , - are all left associative Expression 9 - 5 - 1 means (9 - 5) - 1 4 - 1 3 Evaluate the Expression:  7 * 10 - 5 % 3 * 4 + 9 means (7 * 10) - 5 % 3 * 4 + 9 70 - 5 % 3 * 4 + 9 70 - (5 % 3) * 4 + 9 70 - 2 * 4 + 9 70 - (2 * 4) + 9 70 - 8 + 9 (70 - 8) + 9 62 + 9 71 Evaluate the Expression Parentheses:  Parentheses Parentheses can be used to change the usual order Parts in () are evaluated first Evaluate (7 * (10 - 5) % 3) * 4 + 9 (7 * 5 % 3) * 4 + 9 (35 % 3) * 4 + 9 2 * 4 + 9 8 + 9 17 Slide24:  More Java Operators 8 int age; age = 8; age = age + 1; age 9 age Slide25:  Prefix Form: Increment Operator 8 int age; age = 8; ++age; age 9 age Slide26:  Postfix form: Increment Operator 8 int age; age = 8; age++; age 9 age Slide27:  Decrement Operator 100 int dogs; dogs = 100; dogs--; dogs 99 dogs Which form to use?:  Which form to use? When the increment (or decrement) operator is used in a “stand alone” statement solely to add one (or subtract one) from a variable’s value, it can be used in either prefix or postfix form. dogs--; --dogs; USE EITHER BUT...:  BUT... When the increment (or decrement) operator is used in a statement with other operators, the prefix and postfix forms can yield different results. LET’S SEE HOW. . . Slide30:  int alpha; int num; num = 13; alpha = ++num * 3; “First increment, then use” “Use, then increment”:  int alpha; int num; num = 13; alpha = num++ * 3; 13 13 39 14 “Use, then increment” Integral Types :  Integral Types Type Size in Bits Minimum Value to Maximum Value byte 8 -128 to 127 short 16 -32,768 to 32,767 int 32 -2,147,483,648 to 2,147,483,647 long 64 -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 Sizes of Integral Java Types:  Sizes of Integral Java Types byte 8 bits short 16 bits int 32 bits long 64 bits Using one byte (= 8 bits),:  Using one byte (= 8 bits), HOW MANY DIFFERENT NUMBERS CAN BE REPRESENTED USING 0’s and 1’s? Each bit can hold either a 0 or a 1. So there are just two choices for each bit, and there are 8 bits. 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 28 = 256 0 1 1 0 0 0 1 1 Similarly, using two bytes (= 16 bits),:  Similarly, using two bytes (= 16 bits), 216 = 65536 DIFFERENT NUMBERS CAN BE REPRESENTED. If we wish to have only one number representing the integer zero, and half of the remaining numbers positive, and half negative, we can obtain the 65,536 numbers in the range below : -32768 . . . . 0 . . . . 32767 More about Floating-Point Types:  More about Floating-Point Types In Java floating-point literals like 94.6 without a suffix are of type double by default To obtain another floating point type constant a suffix must be used The suffix F or f denotes float type, as in 94.6F Floating Point Types :  Type Size in Bits Range of Values float 32 +1.4E - 45 to +3.4028235E+38 double 64 +4.9E - 324 to +1.7976931348623157E+308 Floating Point Types Assignment Operator Syntax:  Variable = Expression First, Expression on right is evaluated Then the resulting value is stored in the memory location of Variable on left NOTE: An automatic type conversion occurs after evaluation but before the value is stored if the types differ for Expression and Variable Assignment Operator Syntax What value is stored?:  What value is stored? double a; double b; a = 8.5; b = 9.37; a = b; a b a b 8.5 9.37 ? ? Slide40:  What is stored? ? float someDouble; someDouble someDouble = 12; // implicit type conversion someDouble 12.0 Slide41:  What is stored? ? int someInt; someInt someInt = 4.8; // implicit type conversion someInt 4 Type Casting is Explicit Conversion of Type:  Type Casting is Explicit Conversion of Type int(4.8) has value 4 double(5) has value 5.0 double(7/4) has value 1.0 double(7) / double(4) has value 1.75 Some Expressions:  Some Expressions int age; EXAMPLE VALUE 5 + 8 13 5 / 8 0 6.0 / 5.0 1.2 double (4 / 8) 0.0 double (4) / 8 0.5 What values are stored?:  What values are stored? double loCost; double hiCost; loCost = 12.342; hiCost = 12.348; loCost = (double) ((int) (loCost * 100.0 + 0.5)) / 100.0; hiCost = (double) ((int) (hiCost * 100.0 + 0.5)) / 100.0; Slide45:  Values were rounded to 2 decimal places 12.34 hiCost 12.35 loCost Write a Java expression . . .:  Write a Java expression . . . To find the larger of myAge and yourAge and place it in variable older int older; ? Write a Java expression . . .:  Write a Java expression . . . To find the larger of myAge and yourAge and place it in variable older int older; . . . older = Math.max(myAge, yourAge); class method parameters max is a class method Write a Java expression . . .:  Write a Java expression . . . To find the square root of b2 - 4ac and place it in variable d double a, b, c, d; ? Write a Java expression . . .:  Write a Java expression . . . To find the square root of b2 - 4ac and place it in variable d. double a, b, c, d; . . . d = Math.sqrt(b*b - 4.0*a* c); sqrt is a _________ method Additional String Methods:  Additional String Methods Method length returns an int value that is the number of characters in the string String name = “Donald Duck”; numChars; numChars = name.length(); instance method length is an instance method String Methods Continued:  String Methods Continued Method indexOf searches a string to find a particular substring, and returns an int value that is the beginning position for the first occurrence of that substring within the string Character positions begin at 0 (not 1) The substring argument can be a literal String, a String expression, or a char value If the substring could not be not found, method indexOf returns value -1 String Methods Continued:  String Methods Continued Method substring returns a substring of a string, but does not change the string itsel The first parameter is an int that specifies a starting position within the string The second parameter is an int that is 1 more than the ending position of the substring Remember: positions of characters within a string are numbered starting from 0, not from 1. Slide53:  What value is returned? // Using methods length, indexOf, substring String stateName = “Mississippi”; stateName.length(); ? stateName.indexOf(“is”); ? stateName.substring(0, 4); ? stateName.substring(4, 6); ? stateName.substring(9, 11); ? Inputting Numeric Values:  Inputting Numeric Values If readLine inputs strings, how can we input numbers? We convert the strings to the numbers they represent. “69.34” becomes 69.34 “12354” becomes 12354 Yes, but how? Predefined Numeric Classes:  Predefined Numeric Classes Built-in Type Class int Integer long Long float Float double Double parseInt, parseLong, parseFloat, parseDouble are class methods for translating strings to numeric values Converting Strings to Numbers:  Converting Strings to Numbers int intNumber; System.out.println(“Enter an integer: “); intNumber = Integer.parseInt(inData.readLine()); class method Buffered- method Reader object argument to parseInt method Applications Programming Interface:  Applications Programming Interface Public methods of a class provide the interface between the programmer’s code and the Java class objects. Applications Programming Interface (API) programmer’s code predefined class shared boundary An object of class Double:  An object of class Double Private data and methods . . . doubleValue MAX_VALUE MIN_VALUE parseDouble API Converting a String to a Double Value :  Converting a String to a Double Value double price ; price = Double.parseDouble(inData.readLine()); string converted to double value String object Interacting Classes:  Interacting Classes class Name {…} class Employee {import Name; …} public class Payroll {import Employee; public static void main(…)…{} …)

Related presentations


Other presentations created by Arundel0

Black Holes powerpoint
07. 10. 2007
0 views

Black Holes powerpoint

Physics Presentation
16. 10. 2007
0 views

Physics Presentation

Bangladesh
12. 10. 2007
0 views

Bangladesh

Afrodite Sevasti
13. 10. 2007
0 views

Afrodite Sevasti

Industrial Revolution
22. 10. 2007
0 views

Industrial Revolution

American Imperialism
22. 10. 2007
0 views

American Imperialism

i2 icfa sep05
11. 09. 2007
0 views

i2 icfa sep05

Deok Ryong Yoon
11. 09. 2007
0 views

Deok Ryong Yoon

R1 1
11. 09. 2007
0 views

R1 1

i DMSS
11. 09. 2007
0 views

i DMSS

FinnishChemicalsinfo rmation
15. 10. 2007
0 views

FinnishChemicalsinfo rmation

A Nieto Nuez
26. 11. 2007
0 views

A Nieto Nuez

bio435 660 chap8 pt2
07. 12. 2007
0 views

bio435 660 chap8 pt2

mini3
11. 12. 2007
0 views

mini3

lecture 29
25. 10. 2007
0 views

lecture 29

slidepres
29. 10. 2007
0 views

slidepres

research methods schultz
07. 10. 2007
0 views

research methods schultz

RA2002
24. 10. 2007
0 views

RA2002

Physics with RICH detectors
15. 11. 2007
0 views

Physics with RICH detectors

hogg
03. 01. 2008
0 views

hogg

genrepatrickgilmour
15. 10. 2007
0 views

genrepatrickgilmour

Rabies 2
19. 11. 2007
0 views

Rabies 2

weight control program se
04. 01. 2008
0 views

weight control program se

Water Everywhere
02. 01. 2008
0 views

Water Everywhere

feb17 cacci Soo Young
11. 09. 2007
0 views

feb17 cacci Soo Young

Chang Kyung Sup
11. 09. 2007
0 views

Chang Kyung Sup

AD2002
05. 01. 2008
0 views

AD2002

athans99
16. 02. 2008
0 views

athans99

Neologismen in STAR WARS
19. 02. 2008
0 views

Neologismen in STAR WARS

F02
20. 02. 2008
0 views

F02

2006 National Traffic 101
30. 10. 2007
0 views

2006 National Traffic 101

younginvestigators
27. 02. 2008
0 views

younginvestigators

capacity
29. 02. 2008
0 views

capacity

BTS statement on mesothelioma
11. 03. 2008
0 views

BTS statement on mesothelioma

04 ICG report
20. 03. 2008
0 views

04 ICG report

chapter13part2
07. 04. 2008
0 views

chapter13part2

mesmuses methodology
20. 06. 2007
0 views

mesmuses methodology

Mar02 Workshop Sakallah
20. 06. 2007
0 views

Mar02 Workshop Sakallah

manifesto pldi
20. 06. 2007
0 views

manifesto pldi

lisb overview
20. 06. 2007
0 views

lisb overview

Lesko June01
20. 06. 2007
0 views

Lesko June01

350LEC
09. 04. 2008
0 views

350LEC

bogota final
13. 04. 2008
0 views

bogota final

naccl workshop canada2003
20. 06. 2007
0 views

naccl workshop canada2003

Musy
20. 06. 2007
0 views

Musy

miniboone kektc6
20. 06. 2007
0 views

miniboone kektc6

millennium mix
20. 06. 2007
0 views

millennium mix

MICRO III Aula4
20. 06. 2007
0 views

MICRO III Aula4

MICRO III Aula10
20. 06. 2007
0 views

MICRO III Aula10

MICRO III Aula1
20. 06. 2007
0 views

MICRO III Aula1

morocco
24. 10. 2007
0 views

morocco

wordsplash11th1
14. 04. 2008
0 views

wordsplash11th1

Lucas Foods Case Analysis
16. 04. 2008
0 views

Lucas Foods Case Analysis

opening research data lifecycle
19. 06. 2007
0 views

opening research data lifecycle

Oersted
19. 06. 2007
0 views

Oersted

file45164
17. 04. 2008
0 views

file45164

gutierrez 2002
22. 04. 2008
0 views

gutierrez 2002

thickett
17. 10. 2007
0 views

thickett

rias uk
28. 04. 2008
0 views

rias uk

potomacroundtable Presentation
28. 12. 2007
0 views

potomacroundtable Presentation

how do i know wetland
03. 01. 2008
0 views

how do i know wetland

Oceans2005
07. 11. 2007
0 views

Oceans2005

wblocuniv
11. 09. 2007
0 views

wblocuniv

Secrets Presentation
07. 01. 2008
0 views

Secrets Presentation

Trinidad Tobago
14. 02. 2008
0 views

Trinidad Tobago

MICROIII Aula9
20. 06. 2007
0 views

MICROIII Aula9

MICRO III Aula8
20. 06. 2007
0 views

MICRO III Aula8

Micro III Aula13
20. 06. 2007
0 views

Micro III Aula13

MICRO III Aula11
20. 06. 2007
0 views

MICRO III Aula11

GKim
11. 09. 2007
0 views

GKim

OPSPanama
22. 10. 2007
0 views

OPSPanama

East Asia
25. 03. 2008
0 views

East Asia

NORDU net 2003P Villemoes Talk
19. 06. 2007
0 views

NORDU net 2003P Villemoes Talk

Nygren TPC symposium
19. 06. 2007
0 views

Nygren TPC symposium

M Barbi
20. 06. 2007
0 views

M Barbi

vaitkus
10. 10. 2007
0 views

vaitkus

fpgd040225
29. 10. 2007
0 views

fpgd040225

Future of PR Iran November 2005
24. 02. 2008
0 views

Future of PR Iran November 2005

boj
09. 10. 2007
0 views

boj

Lacroix
15. 11. 2007
0 views

Lacroix

longo2
03. 01. 2008
0 views

longo2

dd259
19. 10. 2007
0 views

dd259

America in World War II
04. 01. 2008
0 views

America in World War II

Digitization Projects
11. 09. 2007
0 views

Digitization Projects

Mikkel Olesen
20. 06. 2007
0 views

Mikkel Olesen

from powerful to personal week2
22. 11. 2007
0 views

from powerful to personal week2

young soo
11. 09. 2007
0 views

young soo

Clayton
22. 10. 2007
0 views

Clayton

PL Asia Slides
11. 09. 2007
0 views

PL Asia Slides