Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming I

Similar presentations


Presentation on theme: "Object Oriented Programming I"— Presentation transcript:

1

2 Object Oriented Programming I
( ) Dr. Adel hamdan Part 05 (Week 6) Dr. Adel Hamdan Date Created: / /2011

3 OUTLINE Math Methods This Reference Static

4 Predefined Classes (continued)
Java Programming: From Problem Analysis to Program Design, 4e 4

5 Predefined Classes (continued)
Java Programming: From Problem Analysis to Program Design, 4e 5

6 Predefined Classes (continued)
Java Programming: From Problem Analysis to Program Design, 4e 6

7 Math example import static java.lang.Math.*; public class Math {
public static void main(String[] args) { System.out.println("sqrt( ) = "+ sqrt( ));//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 ) ); } } OUTPUT sqrt( ) = 30.0 ceil( -9.8 ) = -9.0 floor(65.78) 65.0 Log(2=) Log10(2=) log( E ) = 1.0 Max(2,4)= 4 Max(2.6,4.9)= 4.9 Min(2.6,4.9)= 2.6 Pow(2,4)= 16.0 round(2.6)= 3 round(2.1)= 2 cos( 0.0 ) = 1.0 sin( 0.0 ) = 0.0 tan( 0.0 ) = 0.0

8 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.

9 This Example public 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; }

10 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.

11 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

12 Static example public 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() ); } } OUTPUT Employees before instantiation: 0 Employee constructor: Susan count = 1 Employee constructor: no name count = 2 Employee constructor: adel count = 3 Employees after instantiation: via e1.getCount(): 3 via e2.getCount(): 3 via e3.getCount(): 3 via Employee.getCount(): 3

13 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( ) { 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; OUTPUT Employees before instantiation: 0 Employee constructor: Susan count = 1 Employee constructor: no name count = 2 Employee constructor: adel count = 3 Employees after instantiation: via e1.getCount(): 3 via e2.getCount(): 3 via e3.getCount(): 3 via Employee.getCount(): 3


Download ppt "Object Oriented Programming I"

Similar presentations


Ads by Google