CS1150 Principles f Cmputer Science Math Functins, Characters and Strings Yanyan Zhuang Department f Cmputer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Clrad Springs
Mathematical Functins Java prvides many useful methds in the Math class fr perfrming cmmn mathematical functins. (we have used Math.sqrt(), Math.pw(), Math.randm()) 2
The Math Class Math class is different frm Scanner class Dn t create a Math instance } N need fr Math mymath = new Math(); We call Math class methds withut creating an instance Use Math.methdName() directly } duble squarert = Math.sqrt(25); 3
The Math Class Type Math. Yu can see cnstants and methds 4
The Math Class Class cnstants: PI (3.14159 ) E (2.71828 base f natual lg) Class methds: Trignmetric Methds Expnent Methds Runding Methds min, max, abs, and randm Methds 5
min, max, and abs max(a, b)and min(a, b) Returns the maximum r minimum f tw parameters. abs(a) Returns the abslute value f the parameter. randm() Returns a randm duble value in the range [0.0, 1.0). Numbers1.java Examples: Math.max(2, 3) returns 3 Math.max(2.5, 3) returns 3.0 Math.min(2.5, 3.6) returns 2.5 Math.abs(-2) returns 2 Math.abs(-2.1) returns 2.1 Numbers3.java 6
The randm Methd Generates a randm duble value greater than r equal t 0.0 and less than 1.0 (0 <= Math.randm() < 1.0). Examples: (int)(math.randm() * 10) 50 + (int)(math.randm() * 50) Returns a randm integer between 0 and 9. Returns a randm integer between 50 and 99. In general, a + Math.randm() * b Returns a randm number between a and a + b, excluding a + b. 7
Previus example Math.randm() Hw t generate a randm integer between [lwer, upper)? } Example: int lwer=100, upper=120; } randmduble = Math.randm(); // [0.0, 1.0) } randmduble = randmduble * (upper-lwer); // [0.0, 20.0) } randmduble = lwer + randmduble; // [100.0, 120.0) } randmint = (int) randmduble; // cast duble à int Or in ne step } randmint = (int) (lwer + Math.randm() * (upper-lwer)); CS1150 UC. Clrad Springs
Runding Methds duble ceil(duble x) x runded up t its nearest integer. This integer is returned as a duble value. duble flr(duble x) x is runded dwn t its nearest integer. This integer is returned as a duble value. duble rint(duble x) x is runded t its nearest integer. If x is equally clse t tw integers, the even ne is returned as a duble. int rund(flat x) Return (int)math.flr(x+0.5). lng rund(duble x) Return (lng)math.flr(x+0.5). 9
Runding Methds Examples Math.ceil(2.1) returns 3.0 Math.ceil(2.0) returns 2.0 Math.ceil(-2.0) returns 2.0 Math.ceil(-2.1) returns -2.0 Math.flr(2.1) returns 2.0 Math.flr(2.0) returns 2.0 Math.flr(-2.0) returns 2.0 Math.flr(-2.1) returns -3.0 Math.rint(2.1) returns 2.0 Math.rint(2.0) returns 2.0 Math.rint(-2.0) returns 2.0 Math.rint(-2.1) returns -2.0 Math.rint(2.5) returns 2.0 Math.rint(-2.5) returns -2.0 Math.rund(2.6f) returns 3 Math.rund(2.0) returns 2 Math.rund(-2.0f) returns -2 Math.rund(-2.6) returns -3 Numbers2.java 10
Runding Methds Examples Math.rund(x) returns int r a lng (depends n if the argument is a flat r a duble) int x = Math.rund (81.7); // This wn't wrk? Why? // 81.7 is a duble s need t stre result in lng int intresult = Math.rund (81.7f); lng lngresult = Math.rund(81.7); // Returns 82 - an int // Returns 82 - a lng CS1150 UC. Clrad Springs
Expnent Methds exp(duble a) Returns e raised t the pwer f a. lg(duble a) Returns the natural lgarithm f a. lg10(duble a) Returns the 10-based lgarithm f a. pw(duble a, duble b) Returns a raised t the pwer f b. Examples: Math.exp(1) returns 2.71 Math.lg(2.71) returns 1.0 Math.pw(2, 3) returns 8.0 Math.pw(3, 2) returns 9.0 Math.pw(3.5, 2.5) returns 22.91765 Math.sqrt(4) returns 2.0 Math.sqrt(10.5) returns 3.24 sqrt(duble a) Returns the square rt f a. 12
Trignmetric Methds sin(duble a) cs(duble a) tan(duble a) acs(duble a) asin(duble a) atan(duble a) Radians tradians(90) Examples: Math.sin(0) returns 0.0 Math.sin(Math.PI / 6) returns 0.5 Math.sin(Math.PI / 2) returns 1.0 Math.cs(0) returns 1.0 Math.cs(Math.PI / 6) returns 0.866 Math.cs(Math.PI / 2) returns 0.0 tdegrees() 13
Trignmetric Methds Prvide an angle in radians duble sinofzer = Math.sin(0); // 0 is in radian System.ut.println ("Math.sin(0) = " + sinofzer); // Displays 0.0 Examples with a value in degrees duble angleinradians = Math.tRadians(60); // 60 is degree System.ut.println("Sixty degrees = " + angleinradians + " radians"); duble sinofangle = Math.sin(angleInRadians); System.ut.println ("Math.sin(60) = " + sinofangle); CS1150 UC. Clrad Springs
Case Study: Cmputing Angles f a Triangle x2, y2 A = acs((a * a - b * b - c * c) / (-2 * b * c)) c B a C B = acs((b * b - a * a - c * c) / (-2 * a * c)) C = acs((c * c - b * b - a * a) / (-2 * a * b)) A x3, y3 x1, y1 b Write a prgram that prmpts the user t enter the x- and y-crdinates f the three crner pints in a triangle and then displays the triangle s angles. Let s see CmputeAngle.java example. 15
Character Data Type Values: ne single character Use single qute x t represent a character (duble qutes xxx are fr Strings) } char middleinitial = 'M'; } char numcharacter = '4'; // Assigns digit character 4 t numcharacter } System.ut.println(numCharacter); // Displays 4 Placing a character in it is n lnger a char: it is a String (with ne char in it) } char middleinitial = "M"; // Errr - cannt cnvert String t char Example: Char1.java CS1150 UC. Clrad Springs
Base 10, base 2 and base 16 The Decimal Number System is als called "Base 10" There are 10 symbls (0,1,2,3,4,5,6,7,8 and 9) There is n symbl fr "ten". "10" is actually tw symbls put tgether, a "1" and a "0 Hw t get a number in base 10? Example: 23, 123 But dn't have t use 10 as a "Base". Culd use 2 ("Binary"), 16 ("Hexadecimal"), r any number UC. Clrad Springs
Base 10, base 2 and base 16 Binary (base 2) 000, 001, 010, 011, 100, Cmputers can nly recgnize 0/1 s Hexadecimal (base 16) Decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 Hexadecimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Example: 3BD UC. Clrad Springs
Character Data Type char letter = 'A'; (ASCII) Fur hexadecimal digits. char numchar = '4'; (ASCII) char letter = '\u0041'; (Unicde) char numchar = '\u0034'; (Unicde) NOTE: The increment and decrement peratrs can als be used n char variables t get the next r preceding ASCII/Unicde character. Fr example, the fllwing statements display character b. char ch = 'a'; System.ut.println(++ch); 19
What s ASCII A cmputer cannt stre characters The nly thing it can stre and wrk with are bits A bit can nly have tw values: 1 r 0 (an "actual" bit is a blip f electricity that either is r isn't there) American Standard Cde fr Infrmatin Interchange (ASCII): 8-bit character scheme Prvides encding fr 128 characters (0 t 127) Based n rdering f English alphabet CS1150 UC. Clrad Springs
ASCII Cde fr Cmmnly Used Characters Characters Cde Value in Decimal Unicde Value '0' t '9' 48 t 57 \u0030 t \u0039 'A' t 'Z' 65 t 90 \u0041 t \u005a 'a' t 'z' 97 t 122 \u0061 t \u007a Mre infrmatin: http://kunststube.net/encding/ 21
Unicde Frmat Java characters use Unicde, a 16-bit encding scheme t supprt texts in the wrld s diverse languages. Unicde takes tw bytes, preceded by \u, expressed in fur hexadecimal numbers: frm '\u0000' t '\uffff'. S, Unicde can represent 65535 + 1 characters. Unicde \u03b1 \u03b2 \u03b3 fr three Greek letters 22
Escape Sequences fr Special Characters (Cnsidered a Single character) 23
Casting between char and Numeric Types int i = 'a'; // Same as int i = (int)'a'; System.ut.println ( i = " + i); // i = 97 all numeric peratrs can be applied t the char perands char c = 97; // Same as char c = (char)97; System.ut.println ("c = " + c); // c = a Increment and decrement can be used n char variables t get the next r preceding ASCII/Unicde character. char ch = 'a'; System.ut.println(++ch); //shws character b 24
Cmparing and Testing Characters if (ch >= 'A' && ch <= 'Z') System.ut.println(ch + " is an uppercase letter"); else if (ch >= 'a' && ch <= 'z') System.ut.println(ch + " is a lwercase letter"); else if (ch >= '0' && ch <= '9') System.ut.println(ch + " is a numeric character"); all numeric peratrs can be applied t the char perands 25
Methds in the Character Class Methd Descriptin isdigit(ch) Returns true if the specified character is a digit. isletter(ch) Returns true if the specified character is a letter. isletterofdigit(ch) Returns true if the specified character is a letter r digit. islwercase(ch) Returns true if the specified character is a lwercase letter. isuppercase(ch) Returns true if the specified character is an uppercase letter. tlwercase(ch) Returns the lwercase f the specified character. tuppercase(ch) Returns the uppercase f the specified character. Like the Math class - yu dn't create an instance f this class 26
Methds in the Character Class if (Character.isUpperCase(c)) { System.ut.println( c is an uppercase letter"); System.ut.println ("Its lwercase versin is: " + Character.tLwerCase(c)); } Example: Char2.java CS1150 UC. Clrad Springs