Multiplication Table..

package solutions.java;

/**
 * Created by rchouhan on 11/1/14.
 *
 * For Zero iterations
 * O(N) < Olog(N)
 *
 * replace N with 0; and log(0) = 1
 *
 * For N > 0
 *
 * O(N) < Olog(N) < O(Nlog(N)) < O(N + N) (also O(2N)) (or also O(N^2)) < O(N^N)
 */


public class MultiplicationTable {

    public static void main(String[] args){
        multiplicationTable(3);
        multiplicationTableUntil(3);
    }

    // Time Complexity is O(N)
    static void multiplicationTable(int i){

        for(int j=0; j<=10; j++){
            System.out.println(i +" X " + j +" = " + i*j);
        }
    }

    // Time Complexity is O(N^2)
    static void multiplicationTableUntil(int input){
        for(int i=1; i<= input; i++){
            System.out.println("\nMultiplication Table for " + i + "\n");
            for(int j=0; j<=10; j++){
                System.out.println(i +" X " + j +" = " + i*j);
            }
         }
    }
}

Odd’s and Even’s using..

Write a method to print all odd and even numbers of a given number.

Again this can done using

for loop and
recursions

package solutions.java;

/**
 * Created by rchouhan on 10/30/14.
 */
public class OddAndEvenNumbers {

    public static void main(String[] args){
        System.out.println("\nPrinting all odd numbers for the given input using for loop: "+ getOddSequenceUsingFor(100));
        System.out.println("\nPrinting all even numbers for the given input using for loop: "+ getEvenSequenceUsingFor(100));
        System.out.println("\nPrinting all odd numbers for the given input using recursion: "+ getOddSequenceUsingRecursion(100));
        System.out.println("\nPrinting all even numbers for the given input using recursion: "+ getEvenSequenceUsingRecursion(100));
    }

    // Using FOR loop

    // Time Complexity is O(n) - linear
    private static String getOddSequenceUsingFor(int input){
        String oddValues = "";
        for(int i=1; i<= input; i +=2){
            oddValues += i + ",";
        }
        return oddValues;
    }

    // Time Complexity is O(n) - linear
    private static String getEvenSequenceUsingFor(int input){
        String evenValues = "";
        for(int i=0; i<= input; i +=2){
            evenValues += i + ",";
        }
        return evenValues;
    }


    // Using Recursion

    // Time Complexity is O(n) - linear
    private static String oddValues = "";
    private static int i =1;
    private static String getOddSequenceUsingRecursion(int input){
        if(input < i){
            return oddValues;
        }
        oddValues += i + ",";
        i +=2;

        return getOddSequenceUsingRecursion(input);
    }

    // Time Complexity is O(n) - linear
    private static String evenValues = "";
    private static int e =0;
    private static String getEvenSequenceUsingRecursion(int input){
        if(input < e){
            return evenValues;
        }
        evenValues += e + ",";
        e +=2;

        return getEvenSequenceUsingRecursion(input);
    }
}

Reverse a String

Write a method that reverses a given String.

There are 3 ways that I know how you can reverse a String

1) for-loop
2) recursion
3) StringBuilder (applicable only using Java)

public class ReverseString {

public static void main(String[] args){
  System.out.println("\nReverse a String using StringBuilder: " + 
      reverse("hello"));
  System.out.println("Reverse a String using Recursion: " + 
      reverseUsingRecursion("hello"));
  System.out.println("Reverse a String using For Loop: " + 
      reverseUsingFor("hello"));
}

private static String reverse(String input){
  return new StringBuilder(input).reverse().toString();
}

private static String reverseUsingRecursion(String input){
  // A recursion should always has an exit condition/statement
  if(input == null || input.length() <= 1){
    return input;
  }
  
  // put the first character (i.e charAt(0)) to the end.
  // and recurse with 2nd character (.e subString(1)) onwards

  return reverseUsingRecursion(input.substring(1)) + input.charAt(0);
}

private static String reverseUsingFor(String input){
  if((null == input) || (input.length() <= 1)){
    return input;
  }
  char[] chars = input.toCharArray();
  int rhsIndex = chars.length - 1;
  // iteratively swap until exit condition lhsIndex < rhsIndex is reached
  for(int lhsIndex = 0; lhsIndex < rhsIndex; lhsIndex++){
    char temp = chars[lhsIndex];
    chars[lhsIndex] = chars[rhsIndex];
    chars[rhsIndex--] = temp;
   }
   return new String(chars);
 }
}

Differences Between REST and SOAP

REST SOAP
Representational State Transfer Simple Object Access Protocol
Based of JAX-RS Based on JAX-WS
Supports HTTP/HTTPS verbs (GET,POST,PUT,DELETE) Supports HTTP (POST Only) and also SMTP, JMS
Transport mechanism is purely HTTP Transport mechanism is through RPC asynchronous messaging, based on publish/subscribe same as JMS.
Doesn’t support ACID, Transactions Supports ACID, Atomic Transactions with WS-AT
Doesn’t support Messaging Supports Reliable Messaging with WS-RM
Doesn’t support Security, as URI parameters are not secured. Supports enterprise Security with WS-Security. To attach WS-Security SOAP header, implement a javax.xml.ws.handler.soap.SOAPHandler class and set the handler chain for the binding of the service proxy object
Can be tested on the Browser Tested via SOAP UI
Exposes RESOURCES which represent DATA Exposes OPERATIONS which represent LOGIC
Supports multiple data formats (XML,JSON,TEXT,..) Supports only XML
Stateless Communication. This can be tested by stop the server and checking if the interactions are able to survive Or go back and forth the webpage, after restarting the server to see if the previous content loads Stateful Communication. Stop the server and go back and forth the webpage to see if the state exists.
Point to Point Communication over HTTP Loosely coupled distributed messaging via RPC
No Strong Typing required Strong Typing
REST can be consumed by any client, even a web browser with AJAX & JavaScript. SOAP can be consumed only with specific clients who can call its Operations defined in the WSDL/WADL
REST is lightweight SOAP is heavily weighted.
REST doesn’t require any XML processing & parsing. SOAP requires XML processing via xmlns:s=”http://www.w3.org/2001/XMLSchema
REST consumes less bandwidth, doesn’t require a SOAP header for every message. SOAP consumes more bandwidth, it requires a SOAP header for every message/service call.
REST is Safe (All GET operations are safe), but not secured. SOAP are safe and secured.
REST is Synchronous, because HTTP is Synchronous. SOAP is asynchronous, as its based on messaging service.
REST have no Data Contracts. SOAP have Data Contracts for Request and Responses.
REST can’t be governed, meaning that You will not know who (which client) is consuming the services SOAP is governed, as it has the knowledge of the client who is consuming the service through service registry.
REST cannot generate client side details (aka. artifacts) as it doesn’t have a WSDL. SOAP can generate client side artifacts via WSDL/WADL.
Error handling, success and failure is done by tracking HTTP response code. SOAP can use its exception types and codes for Error Handling and success/failure responses.
There are no tokens sent from client to server. SOAP calls are token based. Server acquires token from Client.
All GET operations of REST can be cached. There is no caching in SOAP.
Performance is Better than SOAP here because of Caching. SOAP performance is good
RESTful Web services can be created using Jersey framework, RESTEasy, apache httpclient, java.net package, JAX-RS, Spring 3 MVC Annotations, apache cfx SOAP Web Services can be implemented by defining XSD and then adding dependencies of either apache cfx, JAX-WS, apache axis, WCF (Windows Communication Foundation), Spring WS