Date post: | 18-Jan-2015 |
Category: |
Documents |
Upload: | garapatiavinash |
View: | 42 times |
Download: | 0 times |
BITS Pilani, Pilani Campus
BITS PilaniPilani Campus
Dr. Yashvardhan SharmaCSIS Dept., BITS-Pilani
IS F213 Object Oriented Programming
BITS Pilani, Pilani Campus
Exercise 1
Define a class which encapsulates a point in 2 – dimensional space. It has following members X & Y coordinate values.
It supports following operations(i) Individual operation for setting the values for X and
Y coordinates(ii) Computing the distance between two points(iii) Checking two points for equality [ Two points are
equal if they have same values for X and Y coordinates]
(iv) Method for translating the values for X and Y
BITS Pilani, Pilani Campus
UML Representation for Class Point
Point
x: doubley: double
+getX() : double+getY() : double+setX(x: double) : void+setY(y: double) : void+equals(other : Point) : boolean+computeDistance(other : Point) : double+show() : void
class Point{double x; // x – coordinatedouble y; // y –coordinate
public double getX() { return x; }public double getY() { return y; } public void setX(double x) { this.x = x; }public void setY(double y) { this.y = y; }public boolean equals(Point other){return this.x == other. x && this.y == other.y ;} public double computeDistance(Point other){double a = (this.y – other.y) * (this.y – other.y);double b = this.x – other.x) * (this.x – other.x);return Math.sqrt(a+b); }
public void show(){S.O.P(“ x= “+x);S.O.P(“ y= “+y);}} End of Point Class
Attributes
Operations
BITS Pilani, Pilani Campus
Class PointTest
class PointTest{public static void main(String args[ ]){Point P1 = new Point();P1.show();Point P2 = P1;P2.show();System.out.println(P1.equals(P2));System.out.println(P1.computeDistance(P2));}// End of main() Method}// End of PointTest
BITS Pilani, Pilani Campus
How to Create an Instance of a class
(Creating Objects)
Objects are always created/constructed/instantiated dynamically using new opeartor.
Syntax :<ClassName> <object reference> = new ConstructorMethod(<parameters>);
Constructor Method Method having same name as name of class
Examples :
BOX b1 = new BOX(); // Valid iff constructor is unparametrized
BOX b2 = new BOX(10,6,8); // Valid iff constructor is parametrized
BITS Pilani, Pilani Campus
Object Creation Examples
BOX b1 = new BOX();
BOX b2 = b1;
b2
b2 is just another reference for the same object
b1
BOXl b h
ONLY ONE OBJECT IS CREATED IN ABOVE STATEMENTS
BITS Pilani, Pilani Campus
How to Access Class members1. Private members of a class are only visible inside class body.2. Protected members have package scope and are accessible to
subclasses in other packages as well.3. protected members are only accessible to subclasses in same package
or other package. [Does not have Package Scope]4. public fields/methods are accessible from every where.5. Every access to public or friendly access fields of class is only through
objects of that class. (Except static fields which are accessible through class name)
Syntax. <object_reference>.<member_field> ; <object_reference>.<methodname(parameter_list)>;
BITS Pilani, Pilani Campus
class Rectangle{int length;int width;void setData(int l,int w){length = l;width = w;}void printSides(){System.out.println("Length is:"+length);System.out.println("Width is:"+width);}int area() { return length * width ;}} // End of class
class RectangleTest{public static void main(String args[]){Rectangle r1 = new Rectangle();r1.setData(10,8);r1.printSides();System.out.println("Area ="+r1.area());Rectangle r2 = new Rectangle();r1.setData(100,80);r1.printSides();System.out.println("Area ="+r2.area());}} // End of class
/* OutputE:\New Folder\Java>java RectangleTestLength is:10Width is:8Area =80Length is:100Width is:80Area =0*/
Example
BITS Pilani, Pilani Campus
class Rectangle{int length;int width;void setData(int l,int w){length = l;width = w;}void printSides(){System.out.println("Length is:"+length);System.out.println("Width is:"+width);}int area() { return length * width ;}}
class RectangleTest{public static void main(String args[]){Rectangle r1 = new Rectangle();r1.printSides();r1.setData(10,8);System.out.println("Area ="+r1.area());Rectangle r2 = new Rectangle();r2.printSides();r1.setData(100,80);System.out.println("Area ="+r2.area());}}
/*E:\New Folder\Java>java RectangleTestLength is:0Width is:0Area =80Length is:0Width is:0Area =0*/
BITS Pilani, Pilani Campus
Null type in java
• null is a literal belonging to null type.• Any object reference can point to null.• Example : BOX b1 = null; if( b1 == null ) returns true; // iff b1 = null • Object Reference pointing to null can not access any instance
field or instance method (NullPointerException)
BITS Pilani, Pilani Campus
Null type Exampleclass X{ int x; int y;}class Y{ int z;}class nulltest{public static void main(String args[]){X x1 = null;Y y1 = null;System.out.println(x1.x);System.out.println(x1.y);System.out.println(y1.z);}}
D:\Java1>java nulltestException in thread "main" java.lang.NullPointerException at nulltest.main(nulltest.java:17)
BITS Pilani, Pilani Campus
class number{int sum(int a,int b){return a+b;}
float sum(float x,float y){return x+y;}
double sum(double a, double b){return a+b;}int mul(int a,int b){return a*b;} Continued …………..
Method Overloading Example
Signature of this Method
sum(float,float)
Signature of this Method
sum(double,double)
BITS Pilani, Pilani Campus
float mul(float x,float y){return x*y;}
double mul(double a, double b){return a*b;}int div(int a,int b){return a/b;}
int div(float x,float y){return (int) (x/y);}
int div(double a, double b){return (int) (a/b);} Continued……….
Bracket compulsory
Overloaded methods
BITS Pilani, Pilani Campus
int mod(int a,int b){return a%b;}
int mod(float x,float y){return (int) (x%y);}int mod(double a, double b){return (int) (a%b);}}
BITS Pilani, Pilani Campus
class over{public static void main(String args[]){number n1 = new number();System.out.println(n1.sum(10.8f,10.6f));System.out.println(n1.mod(10.8,10.6));System.out.println(n1.div(10.8f,10.6f));System.out.println(n1.mod(10.8f,10.6f));System.out.println(n1.mul(10.8f,10.6f));}}
/* OUTPUTE:\New Folder\Java>java over21.400002010114.48*/
BITS Pilani, Pilani Campus
Constructors
1. If a class has any method with the same name as its class then it is constructor method for that class
2. Used to initialize the objects upon creation
3. In the absence of constructor method we have to specifically add a method for object initialization
4. If no constructor is defined for the class then a default constructor is provided by Java run time environment (Without Parameters).
5. Constructor method has no return type not even void.
6. Code written inside constructor method is automatically executed for every object creation of that class.
BITS Pilani, Pilani Campus
Types of Constructors
1. Unparametrized Constructor2. Parametrized Constructor3. Overloaded Constructor
BITS Pilani, Pilani Campus
Unparametrized Constructor
• If a class does not supply any constructor then JRE supplies a default constructor with no parameters
• Class can have its own constructor of any types.
• If a class supplies its own constructor then default constructor becomes hidden.
BITS Pilani, Pilani Campus
Class With No Constructor
class XYZ{double a,b;void setData(double x, double y){a = x;b = y;}void print(){System.out.println("a="+a);System.out.println("b="+b);}}
class XYZTEST{public static void main(String args[]){XYZ xyz = new XYZ();xyz.print();}}
D:\Java1>java XYZTESTa=0.0b=0.0
BITS Pilani, Pilani Campus
Class With Constructor
class XYZ{double a,b;XYZ(){S.O.P(“Object XYZ created”);}}
class XYZTEST{public static void main(String args[]){XYZ xyz = new XYZ();}}
D:\Java1>java XYZTESTObject XYZ created
Class with Unparametrized Constructor
BITS Pilani, Pilani Campus
class XYZ{double a,b;XYZ(double a, double b){this.a = a;this.b = b;print();}void print(){System.out.println("a="+a);System.out.println("b="+b);}}
class XYZTEST{public static void main(String args[]){// XYZ xyz = new XYZ(); WrongXYZ xyz = new XYZ(10.8,6.5);xyz.print();}}
D:\Java1>java XYZTESTa=10.8b=6.5a=10.8b=6.5
Class with Parametrized Constructor
BITS Pilani, Pilani Campus
class XYZ{double a,b;XYZ(){a = 10;b = 8;print();} XYZ(double a, double b){this.a = a;this.b = b;print();}void print(){System.out.println("a="+a);System.out.println("b="+b);}}
class XYZTEST{public static void main(String args[]){XYZ x1 = new XYZ(); XYZ x2 = new XYZ(10.8,6.5);x1.print();x2.print();}}
D:\Java1>java XYZTESTa=10.0b=8.0a=10.8b=6.5a=10.0b=8.0a=10.8b=6.5
Class with Overloaded Constructor
BITS Pilani, Pilani Campus
Constructor Examples(Parametrized Constructor)class Triangle{double side1,side2,side3;Triangle(double side1,double side2,double side3){this.side1 = side1;this.side2 = side2; this.side3 = side3;System.out.println ("Triangle Created with sides :"+side1 +" " +side2+ " "+side3);}}
class TriangleTest{public static void main(String args[]){// Triangle t1 = new Triangle(); This Line will give compile time error Triangle t1 = new Triangle(10.56,4.56,3.45); Triangle t2 = new Triangle(10,4,9); Triangle t3 = new Triangle(1,4,3);}}
BITS Pilani, Pilani Campus
/* Out PutE:\New Folder\Java>javac TriangleTest.java
E:\New Folder\Java>java TriangleTestTriangle Created with sides :10.56 4.56 3.45Triangle Created with sides :10.0 4.0 9.0Triangle Created with sides :1.0 4.0 3.0*/
BITS Pilani, Pilani Campus
Overloaded Constructorsclass Triangle{double side1,side2,side3;Triangle(double side){side1= side2 = side3 = side;System.out.println("Equilateral Triangle Created with sides :"+side);}Triangle(double side1,double side2){this.side1 = side1; this.side2 = this.side3 = side2;System.out.println("Isoceles Triangle Created with sides :"+side1 +" " +side2);}Triangle(double side1,double side2,double side3){this.side1 = side1; this.side2 = side2; this.side3 = side3;System.out.println("Triangle Created with sides :"+side1 +" " +side2+ " "+side3);}}
BITS Pilani, Pilani Campus
class TriangleTest{public static void main(String args[]){ Triangle t1 = new Triangle(10.56,4.56,3.45); Triangle t2 = new Triangle(10,4); Triangle t3 = new Triangle(13);}}
/* OutputE:\New Folder\Java>java TriangleTestTriangle Created with sides :10.56 4.56 3.45Isoceles Triangle Created with sides :10.0 4.0Equilateral Triangle Created with sides :13.0*/
BITS Pilani, Pilani Campus
OBJECT CONTAINMENT
• When one class contains instance of other class as its instance field then it is known as object containment.
• Object Containment defines one of the important concepts of UML known as composition or aggregation or a-part-of or has a relationship
Player Team Aggregation
Engine Car Composition
BITS Pilani, Pilani Campus
class Line{private Point x;private Point y; private double length;
Line(Point x,Point y){this.x = x;this.y = y;length = x.computeDistance(y);}
Line(double x1, double y1, double x2, double y2){x = new Point(x1,y1);y = new Point(x2,y2);length = x.computeDistance(y);}
Example Object Containment
Line class
Point Lineinstance variables of class Line
Constructor where Line can be created from two points
computeDistance() method of point class
Constructor where Line can be created from 4 double values
BITS Pilani, Pilani Campus
void show(){System.out.println("x="+x);System.out.println("y="+y);System.out.println("Length="+length);}// Introduce a method for computing slope} // End of Line class
Method for printing the values for x and y and length
BITS Pilani, Pilani Campus
E:\New Folder\Java>java LineDemox=Point@256a7cy=Point@720eeb
Length=25.612496949731394
class LineDemo{public static void main(String args[]){Point p1 = new Point();Point p2 = new Point(4,true);Point p4 = new Point(10,8);Point p5 = new Point(-10,-8);Line l2 = new (new Point(),new Point(3,5));Line l1 = new Line(p4,p5);l1.show();}}
Origin pointX =4 , Y =0X =10,Y =8X =-10,Y =-8