Friday, August 30, 2019

Composition or Has-A relationship in JAVA

Composition or Has-A relationship

          Composition is the design technique to implement has-a relationship in classes. We can use java inheritance or Object composition for code reuse. 

Java composition is achieved by using instance variables that refer to other objects. 



Program in JAVA:(taken simple example to explain composition)

package composition;

public class Book
{

private String title;
private int pages;
int count=0;
public Book(String title, int pages)
{
this.title=title;
this.pages=pages;
}
public void openBook()
{
System.out.println("book is opened start writing...");
}
public void turnPage()

count++;
if(count<=this.pages)
{
System.out.println("turning page...");
}
else
{
System.out.println("pages over...");
}
}
public void colseBook()
{
System.out.println("close the book...");
}
public void bookDetails()
{
                 System.out.println("The title of the book is "+this.title+" and it                               contains "+this.pages+" pages");
}
}
class Pen
{
private String colour;
public Pen(String colour)
{
this.colour=colour;
}
public void write()
{
System.out.println("write the content in the book with "+this.colour+" colour");
}
}
class Arthur
{
String name;
Book b;    //using class as a derived variables
Pen p;      //using class as a derived variables
public Arthur(String name,Book b,Pen p)
{
this.name=name;
this.b=b;
this.p=p;
}
public void writeContent()
{
b.openBook();
b.turnPage();
p.write();
b.colseBook();
}
public void bookDetails()
{
b.bookDetails();
}
}
class Main
{
public static void main(String [] args)
{
Arthur a=new Arthur("kookie",new Book("BTS",29),new Pen("Black"));
a.writeContent();
a.bookDetails();
}
}

Wednesday, August 28, 2019

Abstraction in JAVA

ABSTRACTION


 Abstraction: hiding of method implementation and showing only method signature is called "Abstraction".


Program in JAVA:


package abstraction;

public interface Distance
{
public void go();
}
class Walk implements Distance //class is hided for user
{
public void go()      //implementation is also hided
{
System.out.println("go and save your life..");
}
}
class Helper    // class is used to create an object for an user
{
public static Distance helpMethod()
{
Distance d1 = new Walk();  //here we use upcasting to hide the method
return d1;                              // implementation
}
}
class Main
{
public static void main (String [] args)
{
Distance d2 = Helper.helpMethod();
d2.go();
}
}

Generalization in JAVA

    GENERALIZATION
Program in JAVA:

(other members are eliminated for better understanding..)
package generalization;

public interface Distance
{
public void go();
}
class OnCycle implements Distance
{
public void go()
{
System.out.println("covered 2 km on cycle...");
}
}
class OnBike implements Distance
{
public void go()
{
System.out.println("covered 5 km on bike...");
}
}
class OnCar implements Distance
{
public void go()
{
System.out.println("covered 7 km on car...");
}
}
class Generalization
{
public static void move(Distance d1)  //Generalized method
{
d1.go();            //which uses up casting process
}
public static void main (String [] args)
{
move(new OnCycle());  //Distance d1= new OnCycle();
move(new OnBike());   //Distance d1= new OnBike();
move(new OnCar());    //Distance d1= new OnCar();
}
}

Friday, August 23, 2019

upstream and down stream in java

class A      //names of classes are simplified for better understanding
{
public void t1()
{
System.out.println("in t1 method...");
}
}
class B extends A
{
public void t2()
{
System.out.println("in t2 method....");
}
}
class C extends B
{
public void t3()
{
System.out.println("in t3 method....");
}
}
public class Streams {   //upstream and down stream
public static void main(String [] args)
{
B rv1= new C();//upstram  // (or) B rv1 = (B) new C();
rv1.t1();                             // this is possible because there is
rv1.t2();                            //only one super class for every subclass
A rv2 =(A) rv1; //upstream
rv2.t1();
B rv3=(B) rv2;   //downstream
rv3.t1();
rv3.t2();
C rv4 = (C) rv3;//downstream
rv4.t1();
rv4.t2();
rv4.t3();
}
}

specialization of methods in java

abstract class Shape
{
abstract public void area();
}
class Circle extends Shape
{
final static double pi=3.14;
int r;
public Circle(int r)
{
this.r=r;
}
public void area()
{
System.out.println(pi*r*r);
}
public static void printArea(Circle c1)
{
c1.area();
}
}
class Rectangle extends Shape
{
int l;
int b;
public Rectangle(int l,int b)
{
this.l=l;
this.b=b;
}
public void area()
{
System.out.println(l*b);
}
public static void printArea(Rectangle r1)
{
r1.area();
}
}
class Triangle extends Shape
{
int b,h;
public Triangle(int b,int h)
{
this.b=b;
this.h=h;
}
public void area()
{
System.out.println(0.5*b*h);
}
public static void printArea(Triangle t1)
{
t1.area();
}
}
public class Specialization
{
public static void main (String [] args)
{
Circle.printArea(new Circle(2));
Circle.printArea(new Circle(7));
Rectangle.printArea(new Rectangle(2,3));
Rectangle.printArea(new Rectangle(5,7));
Triangle.printArea(new Triangle(3,4));
Triangle.printArea(new Triangle(7,9));
}
   
}

Monday, August 5, 2019

Sports Tournament

Sports Tournament 


The company XYZ is hosting a cricket tournament for its employees. The teams are to be built by different departments and the format of the tournament will be knockout. The HR lead is busy with preparations and has done all the promotions. The sponsors are very keen for the tournament and have raised queries about it. The sponsors have asked about the number of matches that needs to be played. Here is where the HR executive is unable to help as he made all the preparations but forgot about the basic questions. He badly needs your help to find the number of matches they have to organize.


The HR has provided you with the total number of teams(N) that registered for the tournament. If there is any team left out to be paired then it automatically gets promoted to the next round. Now it is only you who can save the HR from embarrassment by solving the problem.


Input Format
The first line of input consists of the number of test cases T.
Next T lines consist of the number of teams participating in the tournament, N.

Constraints
1<= T <=100
2<= N <=100000

Output Format
For each test case, print the number of matches that have to be played.

Sample TestCase 1
Input
2
4
7
Output
3
6
Explanation
Test Case 1: N = 4 = 2 + 1 = 3
Test Case 2: N = 7 = 3 + 2 + 1 = 6

PROGRAM IN C:

#include <stdio.h> int main() { int cases,arr1[100],k=0,arr2[100]; //printf("no.of cases"); scanf("%d",&cases); for(int x=0;x<cases;x++) arr2[x]=0; for(int i=0;i<cases;i++) { scanf("%d",&arr1[i]); } for(int j=0;j<cases;j++) { while(arr1[j]>1) { if(arr1[j]%2==0) { arr2[k]=arr2[k]+arr1[j]/2; arr1[j]=arr1[j]/2; } else { arr2[k]=arr2[k]+arr1[j]/2; arr2[k]++; arr1[j]=arr1[j]/2; } } k++; } for(int l=0;l<cases;l++) printf("%d\n",arr2[l]); return 0; }