calculations chapter 11 library of math functions, and constants cos, sin, tan, abs, min, max, log,...
TRANSCRIPT
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
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
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
Chart page 211 shows types of data sample values, patterns,
formatted strings
• Characters: # 0 , E $ all possible
• Insert digits or specified characters
“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.
The sin method page 216
• sin method returns the result of the absolute value of parameter x.
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
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”);