Вы находитесь на странице: 1из 37

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

Example Object Containment

class Line { Point Line Point x; Point y; double length; Line(Point x,Point y) { x instance variable of class this.x = x; Line is of type Point this.y = y; length = x.computeDistance(y); } void show() { System.out.println("x="+x); System.out.println("y="+y); System.out.println("Length="+length); } // Introduce a method for computing slope }

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(); } }

Output

E:\New Folder\Java>java LineDemo x=Point@256a7c y=Point@720eeb Length=25.612496949731394

Method Overloading Example


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

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); }

Bracket compulsory

Continued.

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); } }

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)); } } /* OUTPUT E:\New Folder\Java>java over 21.400002 0 1 0 114.48 */

Strings in Java(Introduction)
Strings in java are implemented as objects of class String ( a final class in java lang package) and StringBuffer class String objects are immutable objects i.e once created you can not change the contents of the strings. Whenever an attempt is made to change the contents of a String reference another instance will be created with modified values

Examples : 1. String name = Pankaj; 2. String s1 = new String(Vyas); 3. String course =Object Oriented Programming; 4. String s2 = s1; 5. String s2 = new String(s2);

Use of static keyword in Java


There are three different uses of static keyword in java. 1. static instance variables 2. static methods 3. static classes Note : static field/methods of a class can be accessed/referenced even without creating the objects of that class. Syntax : <classname> . <fieldname> OR <classname> . <methodname>

static instance variables/fields


Any Field/Attribute/instance field of a class can be declared as static. Declaring an instance field as static makes it class variable that belongs to a whole class not an individual object. For a static fields of any class, all the objects of that class share a common copy. Static field is just like a global variable for a class that is allocated memory once and all objects of that class share that common copy. Static field/instance variables are allocated space in the heap area.

Example (Static Fields)


class circle { circle c1 = new circle(); static double PI=3.14156; circle c2 = new circle(); double radius; double area() {Return PI * radius * radius;} PI double perimeter() {Return 2*PI*radius;} }
c1 radius c2 MEMORY MAP

radius

Example (Static Fields)


MEMORY MAP

class A { static int a = 10; double b,c; . }

A a1 = new A(); A a2 = new A();

a1

a2

Static Methods
static methods can use only static data static methods can be called/accessed/referenced even without creating the objects that class. Syntax <class name> . < method name(parameter list)> static method can not call non static methods. Examples: Math.sqrt (all math functions) Static method can declare local variables but from outside it can access only static data(fields/methods)

Static Method Example


class num { int a,b,c; static int d = 10; static double e = 20.56; num(int a,int b,int c) { this.a = a; this.b =b; this.c =c; } static int sum(int a , int b) { //System.out.println(a=+a+b=+b+c=+c); System.out.println(d=+d+e=+e); return 40; }

static double sum(double a , double b) { System.out.println(d=+d+e=+e); return 40.56; } static void pr() { System.out.println(This is method pr); } void print() { System.out.println(This is method print);
pr(); // call to static method from a non static method ---- Vaild

System.out.println(a=+a+b=+b+c=+c); System.out.println(d=+d+e=+e); } }

Use of final keyword in java


1. final keyword in java can be used for following (i) class declaration (ii) variable declartion (iii) method decalaration 2. final keyword for class means class cannot be inherited. Final classes can not have subclasses. 3. final keyword used with variable declaration makes it constant whose value cannot be changed. Final variables should be initialized to some values at the time of declaration. 4. final keyword used with method definition means method can not be overridden by subclasses ( Makes sense only when inheritance is used)

final class
final class ABC { .. } class a extends ABC { } // Wrong / Invalid

final instance variable

final methods
final class ABC { final void show().. } class a extends ABC { void show() } // Wrong / Invalid

final int x = 40; final double x = 3.4; final double PI = 3.14

If it is declared as private final void show() then a can have method void show()

Final Keyword Example1


class ABC { //final int a; final int a = 10; int b; double c; //final static int d; static int d; ABC(int x,double c,int d) { b=x; this.c = c; this.d =d ;} void show() { System.out.println("a="+a); System.out.println("b="+b); System.out.println("c="+c); System.out.println("d="+d); } }

class ABCDemo { public static void main(String args[]) { ABC a1 = new ABC(10,8,5); ABC a2 = new ABC(-10,18,15); ABC a3 = new ABC(67,80.56,50); ABC a4 = new ABC(76,-6.45,95); a1.show(); } }

Use of toString() Method


1. toString() method is defined by Object class(Supermost super class for all java classes 2. toString() defines the String form of any object. 3. Syntax : public String toString() { } 4. Any class can override this method to provide String form of any object. 5. If it is not overridden then this method will be called from Object class.

class box { int l,b,h; box(int a,int b,int c) {l=a;this.b=b;this.h=c;} void show() { System.out.println("length="+l); System.out.println(Width="+b); System.out.println(height="+h); } }

class boxtest { public static void main(String args[]) { box b1 = new box(10,20,30); box b2 = new box(4,5,6); System.out.println(10); System.out.println(10.25+10); System.out.println(b1); System.out.println(b2); } }

/* Out Put E:\New Folder\Java>java boxtest 10 20.25 box@256a7c Output Generated box@720eeb By calling toString() from Object */

class box { int l,b,h; box(int a,int b,int c) {l=a;this.b=b;this.h=c;} void show() { System.out.println("length="+l); System.out.println("Width="+b); System.out.println("height="+h); } public String toString() { return "Hello How are You"; } }

class boxtest1 { public static void main(String args[]) { box b1 = new box(10,20,30); box b2 = new box(4,5,6); System.out.println(10); System.out.println(10.25+10); System.out.println(b1); System.out.println(b2); } ? }

/* Out Put E:\New Folder\Java>java boxtest1 10 20.25 Hello How are You Hello How are You */ Out Put generated By
toString() defined by box class

Replace toString() method as follows public String toString() { return Length: +l+ +Width: +b+ +Height: +h; }

OUTPUT 10 20.25 Length:10 Width:20 Height:30 Length:4 Width:5 Height:6

Problem : Define a class which encapsulates time of a day in 24 hour format. Support overloaded constructors.Support the following operations : 1. Add/Subtract No of seconds from a given time 2. Add two times and return a new Time 3. Compare two time objects either passing two time parameters explicitly or one implicitly 4. Support methods for checking two time objects for equality (either implicitly or explicitly)

Class Time { int hh,mm,ss; Time(){ } Time(int hh){ .} Time(int hh,int mm) { . } Time(int hh,int mm,int ss) { .. } Time(Time other) { . } Time addSecs(int seconds){ .. } Time subSecs(int seconds){ . } int compareTo(Time other) { .. } static int compareTo(Time first,Time second) { .. } boolean isEqual(Time other) { } static boolean isEqual(Time first,Time other) { } }

class Time { int hh,mm,ss; Time(){ hh=mm=ss=0; } Time(int hh) { this.hh=hh;mm=ss=0; } Time(int hh,int mm) { int m1 = mm/60; int m2 = mm%60; int h1 = hh+m1; int h2 = h1 % 24; this.hh=h2;this.mm=m2;ss=0; }

Continued ..

Time(int hh,int mm,int ss) { int s1 = ss/60; int s2 = ss%60; int m1 = (mm+s1)/60; int m2 = (mm+s1)%60; int h1 = hh+m1; int h2 = h1 % 24; this.hh=h2;this.mm=m2;ss=s2; } Time(Time other) { this.hh = other.hh; this.mm =other.mm; this.ss = other.ss;} continued .

Time addSecs(int seconds) { int x = (this.ss+seconds)/60; int x1 = (this.ss+seconds) % 60; int y = (this.mm+x) / 60; int y1 = (this.mm+x) % 60; int z = (this.hh+y)/24; int z1 = (this.hh+y) % 24; return new Time(z1,y1,x1); }

int compareTo(Time other) { if(this.hh > other.hh) return 1; if(this.hh == other.hh && this.mm > other.mm ) return 1; if(this.hh == other.hh && this.mm == other.mm && this.ss > other.ss) return 1; if(this.hh < other.hh) return -1; if(this.hh == other.hh && this.mm < other.mm ) return -1; if(this.hh == other.hh && this.mm == other.mm && this.ss < other.ss) return -1; return 0; } static int compareTo(Time t1,Time t2) { if(t1.hh > t2.hh ) return 1; if(t1.hh == t2.hh && t1.mm > t2.mm ) return 1; if(t1.hh == t2.hh && t1.mm == t2.mm && t1.ss > t2.ss) return 1; if(t1.hh < t2.hh) return -1; if(t1.hh == t2.hh && t1.mm < t2.mm ) return -1; if(t1.hh == t2.hh && t1.mm == t2.mm && t1.ss < t2.ss) return -1; return 0; } .Continued

boolean isEqual(Time other) { if( this.compareTo(other) == 0) return true; else return false; } static boolean isEqual(Time t1,Time t2) { if(t1.compareTo(t2) == 0) return true; else return false; } public String toString() { return hh+":"+mm+":"+ss; } } // End of Time Class Continued

class TimeTest { public static void main(String args[]) { Time t1 = new Time(7888,455663,44443); Time t2 = new Time(0,45,43); Time t3 = new Time(7,55,43); Time t4 = new Time(t2); System.out.println(t1); if(Time.isEqual(t4,t2)) System.out.println("Times are Equal"); else System.out.println("Times are Uneqal"); if(Time.compareTo(t4,t2) == 1) System.out.println("Time t4 > t2"); else if(Time.compareTo(t4,t2) == -1) System.out.println("Time t4 < t2"); else System.out.println("Time t4 = t2"); }
Call to static Method compareTo

} // End of TimeTest

Final Keyword Example2


class A { final int a; } class ABC { public static void main(String args[]) { A a1 = new A(); final int a=10; System.out.println("a="+a); } }

Вам также может понравиться