Академический Документы
Профессиональный Документы
Культура Документы
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
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
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);
radius
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 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); } }
final class
final class ABC { .. } class a extends ABC { } // Wrong / Invalid
final methods
final class ABC { final void show().. } class a extends ABC { void show() } // Wrong / Invalid
If it is declared as private final void show() then a can have method void show()
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(); } }
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; }
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