calculations chapter 11 library of math functions, and constants cos, sin, tan, abs, min, max, log,...

8
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants .PI, .E Use care with type of variables or literals as parameters Example: cos can only be passed a double

Upload: iris-baker

Post on 04-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with

CalculationsChapter 11

Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp

Constants .PI, .EUse care with type of variables or literals as parameters

Example: cos can only be passed a double

Page 2: Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with

Formatting numbers

• Create an instances of class DecimalFormat (supply a desired pattern call method ‘format’) – int i = 123;– DecimalFormat formatter = new

DecimalFormat (“###”);– textField.setText(formatter.format(i));– // gives 123 in textField

Page 3: Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with

DecimalFormat (“0000”); …

• Produces 0123// given int i = 123• int i = 123456; … (“###,##);• produces 123,456• double i= 12.34; … (“###.##); …• Produces 12.34• double number = 12300000; … (“0.###E0”)

Produces 123E7• double money = 12.34; …(“$###.##”) Produces

$12.34

Page 4: Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with

Chart page 211 shows types of data sample values, patterns,

formatted strings

• Characters: # 0 , E $ all possible

• Insert digits or specified characters

Page 5: Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with

“Money” page 212

• Calculates compound interest. User enters integer amount and interest rate, program calculates interest over a period of year.

• Class “Interest” page 214-215

• Case Study “Iteration” loops that search for a solution until found to sufficient accuracy.

Page 6: Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with

The sin method page 216

• sin method returns the result of the absolute value of parameter x.

Page 7: Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with

Graphics pages 217-218-219

• y = ax3 + bx2 + cx + d

• a,b,c and d input via slider

• Output scaled to fit within a panel 200 x 200 pixels ( -5.0 to +5.0) One unit x or y is 20 pixels

• Program calculates x value from x pixel value, y value, the value of the function

• y pixel value from the y value

Page 8: Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with

Exceptions

• Overflow of ints (how to handel exceptions see chapter 16 for detail)

• Avoid exceptions by checking for them– if ( length > 1000 ) {

• answerTextField.setText (“value too large”);