C A N A D I A N I N T E R N A T I O N A L S C H O O L O F H O N G K O N G MATHEMATICAL FUNCTIONS CHARACTERS, AND STRINGS P1 LESSON 4 P1 LESSON 4.1 INTRODUCTION P1 LESSON 4.2 COMMON MATH FUNCTIONS Java provides many useful methods in the Math class for performing common mathematical functions Math constants Math.PI Math.E 1
P1 LESSON 4.2 COMMON MATH FUNCTIONS Math Class common math functions Trigonometry Exponential Miscellaneous sin(radians) exp(x) ceil(x) cos(radians) log(x) floor(x) tan(radians) log10(x) rint(x) toradian (degrees) power(a,b) round(x) todegree (radians) sqrt(x) max(a,b) asin(a) min(a,b) acos(a) abs(x) atan(a) a character data type represents a single character a string literal must be enclosed in quotation marks a character literal is a single character enclosed in single quotation marks refer to the Computer Science blog for the ASCII Character Table Special Characters escape sequence for special characters Escape Sequence Name Unicode \b backspace \u0008 \t tab \u0009 \n linefeed \u000a \f formfeed \u000c \r carriage return \u000d \\ backslash \u005c \ double quotation \u0022 2
a char can be cast into any numeric type, and vice versa Code Result char ch = (char)0xab0041; ch is character A int i = (int)'a'; iis integer 65 byte b = (byte)'\ufff4; b is byte 0 Methods common methods Method isdigit(ch) isletter(ch) isletterordigit(ch) islowercase(ch) isuppercase(ch) tolowercase(ch) touppercase(ch) Description returns true if ch is a digit returns true if ch is a letter returns true if ch is a digit or a letter returns true if ch is a lowercase letter returns true if ch is a uppercase letter returns the lowercase of ch returns the uppercase of ch a string is a sequence of characters the String type is not a primitive type the String type is a reference type 3
Methods common methods Method length() charat(index) concat(s1) touppercase() tolowercase() trim() Description returns the number of characters in the string returns the character at the specified index of the string concatenates the string with s1 returns a new string with all letters in uppercase returns a new string with all letters in uppercase returns a new string with whitespace characters trimmed on both sides Methods common methods Comparison equals(s1) equalsignorecase() compareto(s1) comparetoignorecase(s1) startswith(prefix) endswith(suffix) contains(s1) Subtring substring(beginindex) substring(beginindex, endindex) index(ch) indexof(s) lastindexof(ch) lastindexof(s) Conversion convert a numeric string to integer int intvalue = Integer.parseInt(intString); convert a numeric string to double double doublevalue = Double.parseDouble(doubleString); convert a number into string String s = number + ; 4
Practice write a program that reads two inputs from the console for your first name and last name concatenates the two inputs with a space in between based on the concatenated string, displays the total number of characters the character at index 1 the string in UPPERCASE P1 LESSON 4.5 CASE STUDY P1 LESSON 4.6 FORMATTING CONSOLE OUTPUT use the System.out.printf method to display formatted output on the console Format Specifier Output %b a Boolean value %c a character %d a decimal integer %f a floating-point number %e a number in scientific notation %s a string 5
P1 LESSON 4 MATH FUNCTIONS Homework Pg. 153 #4.11, 4.12, 4.15, 4.21 6