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

Tuesday, July 23, 2019

The Hare and the Tortoise race

The Hare and the Tortoise race 



We all have heard the story of the hare and the tortoise. Last time the hare lost the race to the tortoise because of his laziness. Since then the hare has been requesting the tortoise for a rematch numerous times and the tortoise has been always declining his request. Just a couple of days before the tortoise has agreed for a rematch on the condition that he will decide the route of the match.

race between a hare and the tortoise has to happen on the hills of the Nilgiri forest. Nilgiri forest has N hills numbered from 1 to N. Some of these hills are connected by paths called forest paths. Since these paths can have sharp corners which can lead to two animals bumping into each other if they come from opposite directions, so the forest government have made them one-way.
You  are given the time taken by the tortoise and the hare to move along a road for every forest road. Nilgiri has total M forest roads which are unidirectional.

The race can start from any hill but it should also end on the same hill. In other words, the path of the race should be cyclic.
The tortoise is now thinking of the route to take which involves the minimum number of roads and the tortoise is strictly faster than the hare on this route. If there are multiple such routes, the tortoise will choose the one which leads to the tortoise winning by the biggest margin (so that the hare doesn't dare to challenge the tortoise again in future).

It is guaranteed that such a route always exists.

Input Format
The first line contains 2 space-separated integers N and M representing the number of hills and the number of forest roads.

Next follows M lines. The ith line contains 4 integers ui, vi, ti, hi - ui and vi represents the starting and ending hills of a forest road respectively, ti and hi represents the time taken to cross this road by the tortoise and the hare respectively.

Constraints
2 <= N <= 4
2 <= M <= N(N-1)
1 <= ui, vi <= N  (ui != vi)
0 <= ti, hi <= 1000,000,000

Output Format
You have to print two space-separated integers - the first one is the minimum number of roads in the chosen path, and the second one is the biggest margin by which the tortoise can win the race on the chosen route.
Sample TestCase 1
Input
4 5
1 2 2 10
2 3 4 400
3 4 8 100
4 3 2 5
3 1 1 1000

Output
2 95
Explanation

You can see, the best route is 3 ---> 4 ----> 3 which consists of 2 roads only. The margin by which the tortoise wins = (100 + 5) - (8 + 2) = 105 - 10 = 95.



PROGRAM IN C:

#include<stdio.h>
int main()
{
     int arr[10][10],value[10],count=0,q=0,result=0,v=0,array1,array2;
scanf("%d",&array2); scanf("%d",&array1); for(int i=1;i<=array1;i++) { for(int j=1;j<=array2;j++) { scanf("%d",&arr[i][j]); } } for(int k=0;k<array1;k++) { if((arr[k][1]==arr[k+1][2])&&(arr[k][2]==arr[k+1][1])) { value[v]=k; count+=2; v++; } } for(int z=0;z<v;z++)
{ q=value[z]; result=result+(arr[q][4]+arr[q+1][4])-(arr[q][3]+arr[q+1][3]);
} printf("%d %d",count,result); return 0;
}