CSC 160 Chapter 12 Math Numbers and Date

CSC 160 Chapter 12: Math, Numbers, and Date Objects 1

Outline • • The math object Properties of the math object Methods of the math object The number object Properties of the number object Methods of the number object The date object Properties and methods of the date object 2

The Math Object • The Math object is a predefined Java. Script object that is used for mathematical purposes. • Give you the values of certain mathematical constants • Perform certain operations • The Math object is useful when you need to make mathematical calculations. 3

Properties of the Math Object • The following is a list of Math properties • For example, when you want to calculate the circumference of a circle in your script code, you can use the PI property. Property Value -----------------------------------------E Value of Euler’s constant (E): 2. 71828… LN 10 Value of the natural logarithm of 10: 2. 302585… LN 2 Value of the natural logarithm of 2: 0. 693147… LOG 10 E Value of the base 10 logarithm of E: 0. 43429… LOG 2 E Value of the base 2 logarithm of E: 1. 442695… PI Value of PI: 3. 14159… SQRT 2 Value of the square root of 2: 1. 4142… SQRT 1_2 Value of the square root of one half: 0. 7071… 4

Properties of the Math Object (cont’d) <html> <head> <TITLE> Math Properties </TITLE> </head> <body> <script language="Java. Script"> <!-document. write("Value of Euler’s constant (E): " + Math. E + " "); document. write("Value of the natural logarithm of 10: " + Math. LN 10 + " "); document. write("Value of the natural logarithm of 2: " + Math. LN 2 + " "); document. write("Value of the base 10 logarithm of E: " + Math. LOG 10 E + " "); document. write("Value of the base 2 logarithm of E: " + Math. LOG 2 E + " "); document. write("Value of PI: " + Math. PI + " "); document. write("Value of the square root of 2: " + Math. SQRT 2 + " "); document. write("Value of the square root of one half: " + Math. SQRT 1_2 + " "); //--> </script> </body> </html> 5

Methods of the Math Object • The methods of the Math object enable you to perform mathematical calculations. • • • abs() ceil() floor() round() max() min() sqrt() exp() log() pow() random() sin() cos() tan() acos() asin() atan 2() 6

Method abs(x) acos(x) asin(x) atan 2(y, x) ceil(x) cos(x) exp(x) floor(x) log(x) max(x, y) min(x, y) pow(x, y) random() round(x) sin(x) sqrt(x) tan(x) Description Returns the absolute value of a number Returns the arccosine of a number Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians Returns the angle theta of an (x, y) point as a numeric value between -PI and PI radians Returns the value of a number rounded upwards to the nearest integer Returns the cosine of a number Returns the value of Ex Returns the value of a number rounded downwards to the nearest integer Returns the natural logarithm (base E) of a number Returns the number with the highest value of x and y Returns the number with the lowest value of x and y Returns the value of x to the power of y Returns a random number between 0 and 1 Rounds a number to the nearest integer Returns the sine of a number Returns the square root of a number Returns the tangent of an angle 7

Methods of the Math Object: Example 1 <script language="Java. Script"> <!-function calculate() { var num = parse. Float(document. myform. number. value); document. myform. abs. value = Math. abs(num); document. myform. ceil. value = Math. ceil(num); document. myform. floor. value = Math. floor(num); document. myform. round. value = Math. round(num); document. myform. exp. value = Math. exp(num); document. myform. log. value = Math. log(num); document. myform. sin. value = Math. sin(num); document. myform. cos. value = Math. cos(num); document. myform. tan. value = Math. tan(num); document. myform. sqrt. value = Math. sqrt(num); } //--> </script> <form name="myform"> Enter a value <input type="text" name="number" value=""> <p> abs <input type="text" name="abs" value=""> ceil <input type="text" name="ceil" value=""> floor <input type="text" name="floor" value=""> round <input type="text" name="round" value=""> exp <input type="text" name="exp" value=""> log <input type="text" name="log" value=""> sin <input type="text" name="sin" value=""> cos <input type="text" name="cos" value=""> tan <input type="text" name="tan" value=""> sqrt <input type="text" name="sqrt" value=""> <p> <input type= "button" value="calculate" on. Click="calculate(); "> </form> 8

Methods of the Math Object: Example 2 <html> <head> <script language="Java. Script"> <!-function calculate() { var num 1 = parse. Float(document. myform. number 1. value); var num 2 = parse. Float(document. myform. number 2. value); document. myform. max. value = Math. max(num 1, num 2); document. myform. min. value = Math. min(num 1, num 2); document. myform. pow. value = Math. pow(num 1, num 2); } //--> </script> </head> <body> <form name="myform"> Enter a number 1 <input type="text" name="number 1" value=""> Enter a number 2 <input type="text" name="number 2" value=""> <p> max <input type="text" name="max" value=""> min <input type="text" name="min" value=""> pow <input type="text" name="pow" value=""> <p> <input type= "button" value="calculate" on. Click="calculate(); "> </form> </body> </html> 9

Methods of the Math Object: Example 3 <html> <head> <TITLE> Math Method: random </TITLE> <script language="Java. Script"> <!-var r_image = new Array( "school-00. jpg", "school-01. jpg", "school-02. jpg", "school-03. jpg", "school-04. jpg", "school-05. jpg", "school-06. jpg", "school-07. jpg", "school-08. jpg", "school-09. jpg", "school-10. jpg", "school-11. jpg"); // Continue. . . 10

Methods of the Math Object: Example 3 (cont’d) function change_image() { var rand_int = Math. floor(Math. random() * r_image. length); document. myimage. src = r_image[rand_int]; } //--> </script> </head> <body> <H 3> When you move the mouse over or off the picture, a random picture will be shown. </H 3> <img src="school-00. jpg" name="myimage" on. Mouse. Over="change_image(); "> </body> </html> 11

The Number Object • The Number object is a predefined Java. Script object that offers several useful properties and methods for use with numbers. • Its most common use is to access some of its helpful properties that represent certain values. 12

Properties of the Number Object • Any number greater than MAX_VALUE is represented as infinity in Java. Script. • Any number less than MAX_VALUE is represented as negative infinity in Java. Script. Property Value constructor Holds the value of the constructor function that created the object MAX_VALUE Holds a constant number value, representing the largest value before Java. Script interprets a number as infinity (approximately 1. 79 E+308) Holds a constant number value, representing the smallest value before Java. Script interprets a number as negative infinity (approximately 5 E-324) Represents the value of “Not a Number” MIN_VALUE Na. N NEGATIVE_INFINITY Represents the value of negative infinity POSITIVE_INFINITY Represents the value of positive infinity prototype Enable you to add properties to the object if you wish 13

Properties of the Number Object: Examples <html> <head> <TITLE> Properties of the Number Object</TITLE> </head> <body> <script language="Java. Script"> <!-document. write("Max Value: "+ Number. MAX_VALUE + " "); document. write("Min Value: " + Number. MIN_VALUE + " "); document. write("Positive Infinity: " + Number. POSITIVE_INFINITY +" "); document. write("Negative Infinity: " + Number. NEGATIVE_INFINITY +" "); //--> </script> </body> </html> 14

Methods of the Number Object • to. Source() • Returns a string value that represents the source code of the object. • to. String() • Returns a string value for a Number object. • value. Of() • Used by Java. Script internally most often. 15

Methods of the Number Object (cont’d) <html> <head> <TITLE> Methods of the Number Object</TITLE> </head> <body> <script language="Java. Script"> <!-var the_number = 10. 56; document. write("Return value of the to. String() method: " + the_number. to. String() + " "); document. write("Return value of the value. Of() method: " + the_number. value. Of() + " "); //--> </script> </body> </html> 16

The Date Object • The Date object is a predefined Java. Script object that enables you to set certain time values and to get certain time values. • To use the Date object, you need to create an instance of the object to which you can refer. var instance. Name = new Date(); 17

Properties and Methods of the Date Object • Properties Property Value ----------------------------------------------Constructor Holds the value of the constructor function that created the object Prototype Enable you to add properties to the object if you wish • Methods • get. Date() get. Day() get. Hours() get. Minutes() get. Seconds() get. Milliseconds() • get. Time() get. Timezone. Offset() get. Year() get. Full. Year() • set. Date() set. Day() set. Hours() set. Minutes() set. Seconds() set. Milliseconds() • set. Time() set. Year() set. Full. Year() • parse() to. GMTString() to. Local. String() 18

Methods of the Date Object Method Description Date() Returns today's date and time get. Date() Returns the day of the month from a Date object (from 1 -31) get. Day() Returns the day of the week from a Date object (from 0 -6) get. Month() Returns the month from a Date object (from 0 -11) get. Full. Year() Returns the year, as a four-digit number, from a Date object Returns the year, as a two-digit or a four-digit number, from get. Year() a Date object. Use get. Full. Year() instead ! get. Hours() Returns the hour of a Date object (from 0 -23) get. Minutes() Returns the minutes of a Date object (from 0 -59) get. Seconds() Returns the seconds of a Date object (from 0 -59) get. Milliseconds() Returns the milliseconds of a Date object (from 0 -999) Returns the number of milliseconds since midnight Jan 1, get. Time() 1970 Returns the difference in minutes between local time and get. Timezone. Offset() Greenwich Mean Time (GMT) Converts a Date object, according to Greenwich time, to a to. GMTString() string. Use to. UTCString() instead ! 19

Methods of the Date Object: Examples var current_time = new Date(); document. write("Full year: "+current_time. get. Full. Year()+" "); document. write("Year: "+current_time. get. Year()+" "); document. write("Month: "+current_time. get. Month()+" "); document. write("Date: "+current_time. get. Date()+" "); document. write(“Week day: "+current_time. get. Day()+" "); document. write("Hours: "+current_time. get. Hours()+" "); document. write("Minutes: "+current_time. get. Minutes()+" "); document. write("Seconds: "+current_time. get. Seconds()+" "); document. write("Milliseconds: "+current_time. get. Milliseconds()+" "); document. write("Milliseconds passed since 01/01/1970, midnight: "+current_time. get. Time()+ " "); document. write("Time zone Offset: "+current_time. get. Timezone. Offset()+" "); document. write("<h 2> GMT time</h 2>"); document. write(current_time. to. GMTString()+" "); // Continue. . . 20

Methods of the Date Object: Examples (cont’d) document. write("<h 2> Set current date and time to Thursday, 01/01/2009, 10: 20: 40: 150 </h 2>"); current_time. set. Full. Year(2009); current_time. set. Month(0); current_time. set. Date(1); current_time. set. Hours(10); current_time. set. Minutes(20); current_time. set. Seconds(40); current_time. set. Milliseconds(150); document. write("Full year: "+current_time. get. Full. Year()+" "); document. write("Year: "+current_time. get. Year()+" "); document. write("Month: "+current_time. get. Month()+" "); document. write("Date: "+current_time. get. Date()+" "); document. write("Day: "+current_time. get. Day()+" "); document. write("Hours: "+current_time. get. Hours()+" "); document. write("Minutes: "+current_time. get. Minutes()+" "); document. write("Seconds: "+current_time. get. Seconds()+" "); document. write("Milliseconds: "+current_time. get. Milliseconds()+" "); document. write("Time: "+current_time. get. Time()+" "); document. write("GMT Time: "+current_time. to. GMTStrimg()+" "); document. write("Local Time: "+current_time. to. Local. Strimg()+" "); 21

Methods of The Date Object: Weekday <script language="Java. Script"> <!-var current_time = new Date(); var var theyear = current_time. get. Full. Year(); themonth = current_time. get. Month()+1; thedate = current_time. get. Date(); theday = current_time. get. Day(); thehours = current_time. get. Hours(); theminutes = current_time. get. Minutes(); theseconds = current_time. get. Seconds(); var weekday = new Array(7); weekday[0] = "Sunday"; weekday[1] = "Monday"; weekday[2] = "Tuesday"; weekday[3] = "Wednesday"; weekday[4] = "Thursday"; weekday[5] = "Friday"; weekday[6] = "Saturday"; document. write("<h 2> Today's date: </h 2>"); document. write(weekday[theday]+", "+themonth+"/"+thedate+"/"+theyear+" "); document. write("<h 2> Current time: </h 2>"); document. write(thehours+": "+theminutes+": "+theseconds+" "); //--> </script> 22

Methods of The Date Object: Clock <html> <head> <title> Clock </title> <script language="Java. Script"> <!-function theclock() { var current_time = new Date(); var thehours = current_time. get. Hours(); var theminutes = current_time. get. Minutes(); var theseconds = current_time. get. Seconds(); if (thehours < 10) thehours = "0"+thehours; if (theminutes < 10) theminutes = "0"+theminutes; if (theseconds < 10) theseconds = "0"+theseconds; window. status = thehours+": "+ theminutes+": "+theseconds; } window. set. Interval("theclock()", 1000); //--> </script> </head> <body> <h 3> the current time is in the Status Bar at the Bottom! </h 3> </body> </html> 23
- Slides: 23