String Aggregation Techniques

==============================================
STEP1:--CREATE TABLE EMP (DEPTNO NUMBER, ENAME VARCHAR2(20));

STEP2:-- SELECT * FROM EMP;

  DEPTNO ENAME
---------- ----------
    20 SMITH
    30 ALLEN
    30 WARD
    20 JONES
    30 MARTIN
    30 BLAKE
    10 CLARK
    20 SCOTT
    10 KING
    30 TURNER
    20 ADAMS
    30 JAMES
    20 FORD
    10 MILLER


STEP3: NO WE WRITE A QUERY FOR BELLOW FORMAT OUTPUT.


Desired Output:

  DEPTNO EMPLOYEES
---------- --------------------------------------------------
    10 CLARK,KING,MILLER
    20 SMITH,FORD,ADAMS,SCOTT,JONES
    30 ALLEN,BLAKE,MARTIN,TURNER,JAMES,WARD


STEP4: QUERY

FOR 11G

SELECT deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename) AS employees
FROM   emp
GROUP BY deptno;


FOR 10G

SELECT deptno, wm_concat(ename) AS employees
FROM   emp
GROUP BY deptno;


Age in Years and months and days:

===================================

select
trunc
( months_between( to_date('14-05-2014','DD-MM-YYYY'), to_date('24-10-1986','DD-MM-YYYY') ) /12 ) Years,
mod
( trunc( months_between( to_date('14-05-2014','DD-MM-YYYY'), to_date('24-10-1986','DD-MM-YYYY') ) ), 12 ) months,
to_date(
'14-05-2014','DD-MM-YYYY') -
add_months(to_date(
'24-10-1986','DD-MM-YYYY'),trunc( months_between( to_date('14-05-2014','DD-MM-YYYY'),
to_date(
'24-10-1986','DD-MM-YYYY') ) )) days
from
dual ;








Interface:


Interfaces are core part of java programming language.

An interface is a contract  of what the classes can do. When a class implements a certain interface, it promises to provide implementation to all the abstract methods declared in the interface.
Interface defines a set of common behaviors. The classes implement the interface agree to these behaviors and provide their own implementation to the behaviors. This allows you to program at the interface, instead of the actual implementation. One of the main usage of interface is provide a communication contract between two objects. If you know a class implements an interface, then you know that class contains concrete implementations of the methods declared in that interface, and you are guaranteed to be able to invoke these methods safely. In other words, two objects can communicate based on the contract defined in the interface, instead of their specific implementation.

Secondly, Java does not support multiple inheritance (whereas C++ does). Multiple inheritance permits you to derive a subclass from more than one direct superclass. This poses a problem if two direct superclasses have conflicting implementations. (Which one to follow in the subclass?). However, multiple inheritance does have its place. Java does this by permitting you to "implements" more than one interfaces (but you can only "extends" from a single superclass). Since interfaces contain only abstract methods without actual implementation, no conflict can arise among the multiple interfaces. (Interface can hold constants but is not recommended. If a subclass implements two interfaces with conflicting constants, the compiler will flag out a compilation error.)

Interfaces provide a way to achieve abstraction in java and used to define the contract for the subclasses to implement.
For example, lets say we want to create a drawing consists of multiple shapes, here we can create an interface Shape and define all the methods that different types of Shape objects will implement. For simplicity purpose, we can keep only two methods – draw() to draw the shape and getArea() that will return the area of the shape.

Java Interface Example

Based on above requirements, our Shape interface will look like this.
 
Shape.java












package com.journaldev.design;
 
public interface Shape {
 
    //implicitly public, static and final
    public String LABLE="Shape";
     
    //interface methods are implicitly abstract and public
    void draw();
     
    double getArea();
}

Important Points about Interfaces in Java

  • interface is the code that is used to create an interface in java.
  • We can’t instantiate an interface in java.
  • Interface provides absolute abstraction, in last post we learned about abstract classes in java to provide abstraction but abstract classes can have method implementations but interface can’t.
  • Interfaces can’t have constructors because we can’t instantiate them and interfaces can’t have a method with body.
  • By default any attribute of interface is public, static and final, so we don’t need to provide access modifiers to the attributes but if we do, compiler doesn’t complain about it either.
  • By default interface methods are implicitly abstract and public, it makes total sense because the method don’t have body and so that subclasses can provide the method implementation.
  • An interface can’t extend any class but it can extend another interface. public interface Shape extends Cloneable{} is an example of an interface extending another interface. Actually java provides multiple inheritance in interfaces, what is means is that an interface can extend multiple interfaces.
  • implements keyword is used by classes to implement an interface.
  • A class implementing an interface must provide implementation for all of its method unless it’s an abstract class. For example, we can implement above interface in abstract class like this:
     
    ShapeAbs.java











    package com.journaldev.design;
     
    public abstract class ShapeAbs implements Shape {
     
        @Override
        public double getArea() {
            // TODO Auto-generated method stub
            return 0;
        }
     
    }
  • We should always try to write programs in terms of interfaces rather than implementations so that we know beforehand that implementation classes will always provide the implementation and in future if any better implementation arrives, we can switch to that easily.

Interface Implementation Example

Now lets see some implementation of our Shape interface.
 
Circle.java
 


















package com.journaldev.design;
 
public class Circle implements Shape {
 
    private double radius;
 
    public Circle(double r){
        this.radius = r;
    }
     
    @Override
    public void draw() {
        System.out.println("Drawing Circle");
    }
     
    @Override
    public double getArea(){
        return Math.PI*this.radius*this.radius;
    }
 
    public double getRadius(){
        return this.radius;
    }
}
 
 
Notice that Circle class has implemented all the methods defined in the interface and it has some of its own methods also like getRadius(). The interface implementations can have multiple type of constructors. Lets see another interface implementation for Shape interface.



Rectangle.java





















package com.journaldev.design;
 
public class Rectangle implements Shape {
 
    private double width;
    private double height;
     
    public Rectangle(double w, double h){
        this.width=w;
        this.height=h;
    }
    @Override
    public void draw() {
        System.out.println("Drawing Rectangle");
    }
 
    @Override
    public double getArea() {
        return this.height*this.width;
    }
 
}
Notice the use of override annotation, learn about annotations in java and why we should always use override annotation when overriding a method in java.
Here is a test program showing how to code in terms of interfaces and not implementations.
ShapeTest.java



















package com.journaldev.design;
 
public class ShapeTest {
 
    public static void main(String[] args) {
         
        //programming for interfaces not implementation
        Shape shape = new Circle(10);
         
        shape.draw();
        System.out.println("Area="+shape.getArea());
         
        //switching from one implementation to another easily
        shape=new Rectangle(10,10);
        shape.draw();
        System.out.println("Area="+shape.getArea());
        }
 
}
Output of the above programs is:




Drawing Circle
Area=314.1592653589793
Drawing Rectangle
Area=100.0

Java Interface Benefits

  • Interface provides a contract for all the implementation classes, so its good to code in terms of interfaces because implementation classes can’t remove the methods we are using.
  • Interfaces are good for starting point to define Type and create top level hierarchy in our code.
  • Since a java class can implements multiple interfaces, it’s better to use interfaces as super class in most of the cases.

Java Interface Disadvantages

Although interfaces provide a lot of advantages but it has some disadvantages too.
  • We need to chose interface methods very carefully at the time of designing our project because we can’t add of remove any methods from the interface at later point of time, it will lead compilation error for all the implementation classes. Sometimes this leads to have a lot of interfaces extending the base interface in our code that becomes hard to maintain.
  • If the implementation classes has its own methods, we can’t use them directly in our code because the type of Object is an interface that doesn’t have those methods. For example, in above code we will get compilation error for code shape.getRadius(). To overcome this, we can use typecasting and use the method like this:


        Circle c = (Circle) shape;
    c.getRadius();
    Although class typecasting has its own disadvantages.
Thats all I have for interfaces in java and since we use it a lot in code as well as design patterns we should be aware of its features. Make sure you use interfaces in designing the system and as a contract between the client and the subclasses implementing the interfaces.

Update: Java 8 has changed the definition of interfaces with the introduction of default methods and static methods implementation in interfaces. For more details, please read Java 8 interface default methods.




Java/Java script Questions—

1.) Design Editable Html Table using Java Script.
Column Header
Name
Age
Gender
Email

Inline editable Table User Can Enter Name, Age and Email Input Text Box. Gender should be Combo Box. Combo Box Options is Male, Female and Both.
Add Button: On Click All Editable input type will be displayed and user will be able to enter the values.
Edit Button: On Click All Editable input type will be displayed and user will be able to enter the value.
Save Button: On Click input type will be hidden and value will be displayed.


editableHtmlTabel.html


        
                </span><span class="pln">Add/Remove dynamic rows in HTML table</span><span class="tag">
                
        

       

Editable Html Table using Java Script


       

               
type="button" value="Add Row" onClick="addRow1()" />
               
type="button" value="Save Row" onClick="addToTable('myTable')" />

               
type="button" value="Delete Row" onClick="deleteRow('myTable')" />
               
type="button" value="Edit Row" onClick="edt('myTable')" />
               
type="button" value="Update Row" onClick="savedit('myTable')" />


               
id="myTable" border="1" width="50%">
                       

                               

                               

                               

                               

                               

                       
id="name" width="10%">
                                        Select
                               
id="name" width="30%">
                                        Name
                               
id="age" width="20%">
                                        Age
                               
id="gender" width="10%">
                                        Gender
                               
id="email" width="30%">
                                        Email
                               


                       


                       

               


               
type="hidden" id="crw">

       




2.) Create Dialog Box in Java Script as Displayed in Pic 1. On click of show me button hide and show this dialog.


A).
dialogBox.html


 
     
 
  
      
      

  


3.) Write simple steps for spring soap Web Services Including Client Side and Server Side.
Server use is for making Request and Response .

A)
Server side:

  1. Design a SEI, service endpoint interface, which is basically a java interface with web-service annotations. This is not mandatory, it's just a point of good design from basic OOP.

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
/*this annotation stipulates the style of your ws, document or rpc based. rpc is more straightforward and simpler. And old.*/
@SOAPBinding(style = Style.RPC)
public interface MyService{
@WebMethod String getString();

}

  1. Implement the SEI in a java class called a SIB service implementation bean.
 
import javax.jws.WebService;

//this binds the SEI to the SIB
@WebService(endpointInterface = "com.yours.wsinterface")

public class MyServiceImpl implements MyService {
public String getResult() { return "result"; }

@Override
public String getString() {
       return "result";
}
}


  1. Expose the service using an Endpoint import javax.xml.ws.Endpoint;
 
public class MyServiceEndpoint{
 
public static void main(String[] params){
  Endpoint endPoint =  EndPoint.create(new MyServiceImpl());
//supply yourdesired url to the publish method to actually expose the //service.
  endPoint.publish("http://localhost:9001/myService"); 
               }
}

Client Side:
By using wsimport tool we need to generate the stub  then based on SEI in stub we need to communicate with server.
--------------

4.) Please write the simple java code for the below design:
*******
*****
***
*
** *
*****
*******
Piramid.java


public class Piramid {

       public static void main(String[] args) {
              for (int i = 7; i > 0; i = i - 2) {
                     for (int j = 0; j < i; j++) {
                           System.out.print("*");
                     }
                     System.out.println("");

                     if (i == 1) {
                           for (int m = 1; m <= 7; m = m + 2) {

                                  for (int j = 0; j < m; j++) {
                                         if (m != 1) {
                                                System.out.print("*");
                                         }
                                  }
                                  if (m != 1) {
                                         System.out.println("");
                                  }
                           }
                     }
              }

       }

}


5.) Design Html form with input component using HTML design and Java Script.
Form will have four input component and three actions as mentioned below.
Input Component
1) Name: HTML text box where user can enter the name. Put proper validation.
2) Age: HTML text box where user can enter the age. Put required validation as well.
3) Gender: Add two radio buttons for Male and Female and Male should be default.
4) Email: Add a text box and put proper validation.
5) Country: Add a list box of country. Default should be Country India.

Action
1) Add Button: On click on add button all the input component will be enabled and user can type.
2) Edit Button: On click on Edit button all input component will be enabled with existing data and user can change it.
3) Save Button: On click on save button all the entered data will the stored in a temporary array and show the first record as selected from the array.
a) Show all the data from temporary array in a HTML table.
b) Table will show the record in sorted order on Name.

Column Header of HTML Table
Name
Age
Gender
Email
Country


------------ Same as 1 question (Editable table) , with javascript validations




6.) Write a java program and print below mentioned table in which suppresses the duplicate record of the first two columns from left which have same value but third column’s value will not be suppressed. Please use the Arrays or Map.
Test 23 Gaya
Test 23 Gaya
Market 17 Chaplin
District 15 MP
Village 45 UP
Village 45 MP
Village 67 UP
Village 67 MP
Village 46 MP
Output:
Test  23  Gaya,Gaya
Market  17  Chaplin
District  15  MP
Village  45  MP,UP
Village  67  MP,UP
Village  46  MP


import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class SuppressDuplicate {

       public static void main(String[] args) {
             
              Map map = new LinkedHashMap();
              ArrayList arr = new ArrayList();

        arr.add(new Employe("Test", 23, "Gaya"));
        arr.add(new Employe("Test", 23, "Gaya"));
        arr.add(new Employe("Market", 17, "Chaplin"));
        arr.add(new Employe("District", 15, "MP"));
        arr.add(new Employe("Village", 45, "UP"));
        arr.add(new Employe("Village", 45, "MP"));
        arr.add(new Employe("Village", 67, "UP"));
        arr.add(new Employe("Village", 67, "MP"));
        arr.add(new Employe("Village", 46, "MP"));

        Iterator itr = arr.iterator();

        while (itr.hasNext()) {

                Employe emp = itr.next();
                String key = emp.getName()+emp.getAge();

              if(map.containsKey(key)){
                     String state = emp.getState();
                     Employe mapEmp = map.get(key);
                     emp.setState(state+","+mapEmp.getState());
                     map.put(key,emp);
              }else{
                     map.put(key, emp);
              }
        }
        for(Map.Entry e: map.entrySet()){
              Employe em = e.getValue();
              System.out.println(em.getName()+"  "+em.getAge()+"  "+em.getState());
        }

       }

}

public class Employe {

    private String name;

    private int age;

    private String state;

    public Employe(String name, int age, String state) {

            super();

            this.name = name;

            this.age = age;

            this.state = state;

    }

    public String getName() {

            return name;

    }

    public void setName(String name) {

            this.name = name;

    }

    public int getAge() {

            return age;

    }

    public void setAge(int age) {

            this.age = age;

    }

    public String getState() {

            return state;

    }

    public void setState(String state) {

            this.state = state;

    }

}



2.) Create Dialog Box in Java Script as Displayed in Pic 1. On click of show me button hide and show this
dialog.
Dialog_box.html
Custom JavaScript Dialog Box
dialog_box.css
body {margin:0; font-family:Verdana, Arial, Helvetica; font-size:14px}
#content {padding:20px}
#dialog {position:absolute; width:425px; padding:50px; z-index:200; background:#fff}
#dialog-header {display:block; position:relative; width:411px; padding:3px 6px 7px; height:14px; font-size:14px; font-weight:bold}
#dialog-title {float:left}
#dialog-close {float:right; cursor:pointer; margin:3px 3px 0 0; height:11px; width:11px; background:url(dialog_close.gif) no-repeat}
#dialog-content {display:block; height:160px; padding:6px; color:#666666; font-size:13px}
#dialog-mask {position:absolute; top:0; left:0; min-height:100%; width:100%; background:#FFF; opacity:.75; filter:alpha(opacity=75); z-index:100}
.success {background:#fff url(images/success_bg1.jpg) bottom right no-repeat; border:1px solid #60a174; border-top:none}
.successheader {background:#3c7f51; color:#FFFFF; border:1px solid #60a174; border-bottom:none}

#buttons {height:30px; width:100px; background:#3c7f51}
#button {height:30px; width:100px;}

dialog_box.js
// global variables //
var TIMER = 5;
var SPEED = 10;
var WRAPPER = 'content';

// calculate the current window width //
function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
function topPosition() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(title,message,type,autohide) {
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = 'Name:

Address:


  ';
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
}

// hide the dialog box //
function hideDialog() {
  var dialog = document.getElementById('dialog');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0)", TIMER);
}

// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + SPEED;
  } else {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}