Upload
darren-blankenship
View
216
Download
0
Embed Size (px)
Citation preview
Object Oriented Programming I
• (1301108 )• Dr. Adel hamdan• Part 05 (Week 6)• Dr. Adel Hamdan• [email protected]• Date Created: / /2011
OUTLINE
• Math Methods
• This Reference
• Static
4Java Programming: From Problem Analysis to Program Design, 4e
Predefined Classes (continued)
5Java Programming: From Problem Analysis to Program Design, 4e
Predefined Classes (continued)
6Java Programming: From Problem Analysis to Program Design, 4e
Predefined Classes (continued)
Math exampleimport static java.lang.Math.*;public class Math { public static void main(String[] args) { System.out.println("sqrt( 900.0 ) = "+ sqrt( 900.0 ));//return square root System.out.printf( "ceil( -9.8 ) = %.1f\n", ceil( -9.8 ) );//smallest integer that
is not less than x System.out.println("floor(65.78) "+floor(65.78));//return largest integer System.out.println("Log(2=) "+log(2)); System.out.println("Log10(2=) "+log10(2));//natural logarithm base(2) System.out.printf( "log( E ) = %.1f\n", log( E ) );//natural logarithm base(10) System.out.println("Max(2,4)= "+max(2,4));// System.out.println("Max(2.6,4.9)= "+max(2.6,4.9));// System.out.println("Min(2.6,4.9)= "+min(2.6,4.9));// System.out.println("Pow(2,4)= "+pow(2,4));// System.out.println("round(2.6)= "+round(2.6));// System.out.println("round(2.1)= "+round(2.1));// System.out.printf( "cos( 0.0 ) = %.1f\n", cos( 0.0 ) ); System.out.printf( “sin( 0.0 ) = %.1f\n", sin( 0.0 ) ); System.out.printf( “tan( 0.0 ) = %.1f\n", tan( 0.0 ) ); } }
OUTPUTsqrt( 900.0 ) = 30.0ceil( -9.8 ) = -9.0floor(65.78) 65.0Log(2=) 0.6931471805599453Log10(2=) 0.3010299956639812log( E ) = 1.0Max(2,4)= 4Max(2.6,4.9)= 4.9Min(2.6,4.9)= 2.6Pow(2,4)= 16.0round(2.6)= 3round(2.1)= 2cos( 0.0 ) = 1.0sin( 0.0 ) = 0.0tan( 0.0 ) = 0.0
Using the this Keyword
• Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
This Examplepublic class Point { public int x = 0; public int y = 0; //constructor public Point(int a, int b) { x = a; y = b; }}
but it could have been written like this:public class Point { public int x = 0; public int y = 0; //constructor public Point(int x, int y) { this.x = x; this.y = y; }}
Static
• Every object has its own copy of all the instance variables of the class. In certain cases, only one copy of a particular variable should be shared by all objects of a class. A static field called a class variable is used in such cases.
• A class's public static members can be accessed through:– a reference to any object of the class, or – by the member name with the class name and a dot
(.), as in Math.random().• A class's private static class members can be accessed
only through methods of the class.
Static• To access a public static member when no objects of the
class exist (and even when they do), prefix the class name and a dot (.) to the static member, as in Math.PI.
• To access a private static member when no objects of the class exist, a public static method must be provided and the method must be called by qualifying its name with the class name.
• You can initialize static instance variable only when you declare it as followings– private static int count=3;//initialized to 3
• if you do not initialize it; it will be initialized to its default value;– private static int cout;//initialized to 0
Static examplepublic class staticexp { public static void main(String[] args) {System.out.printf( "Employees before instantiation: %d\
n",Employee.getCount() ); // create three Employees; count should be 3 Employee e1 = new Employee( "Susan" ); Employee e2 = new Employee(); Employee e3=new Employee ("adel"); // show that count is 3 after creating Three Employees System.out.println( "\nEmployees after instantiation: " );System.out.printf( "via e1.getCount(): %d\n",e1.getCount() );System.out.printf( "via e2.getCount(): %d\n", e2.getCount() ); System.out.printf( "via e3.getCount(): %d\n", e3.getCount() );} }
OUTPUTEmployees before instantiation: 0Employee constructor: Susan count = 1Employee constructor: no name count = 2Employee constructor: adel count = 3
Employees after instantiation: via e1.getCount(): 3via e2.getCount(): 3via e3.getCount(): 3via Employee.getCount(): 3
Static example (cont)class Employee{
private String firstName; private static int count = 0; // number of objects in memory
public Employee( String first ) { firstName = first; count++; // increment static count of employees System.out.printf( "Employee constructor: %s count = %d\
n", firstName, count ); } public Employee( ) { count++; // increment static count of employees System.out.printf( "Employee constructor: %s count = %d\
n", "no name", count );//System.out.println("no name");//invoke constructor with one
argument }
public String getFirstName() { return firstName; } public static int getCount() { return count; }
}
OUTPUTEmployees before instantiation: 0Employee constructor: Susan count = 1Employee constructor: no name count = 2Employee constructor: adel count = 3
Employees after instantiation: via e1.getCount(): 3via e2.getCount(): 3via e3.getCount(): 3via Employee.getCount(): 3