Monday, 18 September 2017

Traverse the matrix in Java
  • A matrix will be in M x N format.
  • So what we need to do is, get value of indexes from 0,0 to m,n. 
  •  Itreate the two dimensional array as follow
take 0(i) and traverse j till j < N
take 1(i) and traverse j till j < N
           .
           .
till M(i) and traverse j till j < N

      PrimeNumberCheck.java  
public class TraverseMatrix { private static final int M = 3; private static final int N = 3; public static void main(String[] args) { TraverseMatrix app = new TraverseMatrix(); int matrix[][] = app.createMatrix(); app.printMatrix(matrix); app.normalTraverse(matrix); } private void normalTraverse(int[][] matrix) { for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { System.out.print(matrix[i][j]); System.out.print(", "); } } } private int[][] createMatrix() { int matrix[][] = new int[M][N]; for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { matrix[i][j] = (int) (Math.random() * 100 + 1); } } return matrix; } private void printMatrix(int[][] matrix) { for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { System.out.print(matrix[i][j]); System.out.print(" "); } System.out.println(" "); } } }

To check the given number is prime or not.
Prime Numbers:
  • A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
  •  It means the remainder of divison of N with numbers from 2 to N/2 should be grater than zero.
Lets take 6  6%2=0, 6%3=0...from 2 to 3(2 to 6/2)
here remainder is zero. So 6 is not prime number.
Lets take 5 now
5%2=1
here remainder is greater than zero. So 5 is prime number.

      PrimeNumberCheck.java  
public class PrimeNumberCheck { public static void main(String[] args) { PrimeNumberCheck app = new PrimeNumberCheck(); System.out.println("Eneter number:"); Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); System.out.println(app.isPrime(number)); } private boolean isPrime(int number) { boolean isPrime = true; for (int i = 2; i <= number / 2; i++) { if (number % i == 0) { return false; } } return isPrime; } } /* ****** INPUT ******** 5 ******* INPUT ******** ****** OUTPUT ******** true ******* OUTPUT ******** */

Friday, 15 September 2017


Order of Execution
  1. Parent Static Block 
  2. Child Static Block 
  3. Parent Normal Block 
  4. Parent Constructor 
  5. Child Normal Block 
  6. Child Constructor
  7. Child method invoked

Animal.java 
public class Animal { static { System.out.println("Animal Static Block "); } public Animal() { System.out.println("Animal Consuctor"); } { System.out.println("Animal Normal Block"); } public void aboutMe() { System.out.println("I am a Animal"); } }
Cow.java
public class Cow extends Animal { static { System.out.println("Cow Static Block "); } public Cow() { System.out.println("Cow Consuctor"); } { System.out.println("Cow Normal Block"); } public void aboutMe() { System.out.println("I am a Cow"); } }
App.java
public class App { public static void main(String[] args) { Cow cow = new Cow(); cow.aboutMe(); } }


  1. Load application Context on Spring?
    1. ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    2. Autowired
      1. @Autowired
      2. private ApplicationContext appContext;
  2. Spring bean scopes: session and globalSession
    1. GlobalSession is something which is connected to Portlet applications. When your application works in Portlet container it is built of some amount of portlets. Each portlet has its own session, but if your want to store variables global for all portlets in your application than you should store them in globalSession. This scope doesn't have any special effect different from session scope in Servlet based applications.
  3. What is the difference between a portlet and a servlet?
    1. Portlets are part of JSR-168 standard that regulates portal containers and components. This is different standard from standards for web containers (and servlets). Though there are definitely strong parallels between these two standards they differ in containers, APIs, life cycle, configuration, deployment, etc.
    2. The main difference between portlet vs. servlet could be that while servlet always responds to single type of action - request, portlet (due to nature of its life cycle and stronger container bindings) has to respond to two types of actions: render and request. There are of course more to it but I found this as the core difference between the two when I studied portal development.
  4. Difference between spring @Controller and @RestController annotation? 
    @Controller
  • @Controller is used to mark classes as Spring MVC Controller. 
  • If we need to return values as JSON or XML then need to add @ResponseBody annotation above the method
  • @RestController 
  • Its methods will automatically converte the response to JSON or XML. No need to add @ResponseBody annotation.

@Controller @ResponseBody public class MyController { } @RestController public class MyRestController { }


public class DeadLockEx { private String str1 = "SPRING"; private String str2 = "HIBERNATE"; public static void main(String[] args) { DeadLockEx app = new DeadLockEx(); app.t1.start(); app.t2.start(); } Thread t1 = new Thread("Thead 1") { public void run() { while (true) { synchronized (str1) { synchronized (str2) { System.out.println(str1 + str2); } } } } }; Thread t2 = new Thread("Thead 2") { public void run() { while (true) { synchronized (str2) { synchronized (str1) { System.out.println(str2 + str1); } } } } }; }



  1. Implementing two interfaces in a class with same method. Which interface method is overridden? 

interface A{ int f(); } interface B{ int f(); } class Test implements A, B{ public static void main(String... args) throws Exception{ } @Override public int f() { // from which interface A or B return 0; } }
  • If a type implements two interfaces, and each interface define a method that has identical signature, then in effect there is only one method, and they are not distinguishable. 
  • If, say, the two methods have conflicting return types, then it will be a compilation error. This is the general rule of inheritance 
  • This is the general rule of inheritance, method overriding, hiding, and declarations, and applies also to possible conflicts not only between 2 inherited interface methods, but also an interface and a super class method, or even just conflicts due to type erasure of generics.
  • Reference
  • https://stackoverflow.com/questions/2801878/implementing-two-interfaces-in-a-class-with-same-method-which-interface-method

Tuesday, 12 September 2017


Given an array of integers, return indices of the two numbers such that they add up to a specific target.
Example:
Given nums = [ 3, 4, 5, 2, 10, 8, 9 ]
target = 19
Because nums[4] + nums[6] = 10 + 9 = 19, return [4, 6]
Approach:
step1. take 3 and then add 3 with all other elements
step2. take 4 and then add 4 with all other elements.Continue same till 9

 
      TwoSum.java  
public class TwoSum { public static void main(String[] args) { int[] arr = { 3, 4, 5, 2, 10, 8, 9 }; int target = 19; TwoSum app = new TwoSum(); int[] rslt = app.twosum(arr, target); System.out.print(rslt[0] + ", " + rslt[1]); } private int[] twosum(int[] arr, int target) { int[] rslt = new int[2]; // take 3 and then add 3 with all other // take 4 and then add 4 with all other elemnts.Continue same till 9 for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (j != i) { // don't add the same indexes int sum = arr[i] + arr[j]; if (sum == target) { rslt[0] = i; rslt[1] = j; break; } } } } return rslt; } } /* ****** INPUT ******** { 3, 4, 5, 2, 10, 8, 9 } 19 ******* INPUT ******** ****** OUTPUT ******** 4, 6 ******* OUTPUT ******** */

Search This Blog

Contact us

Name

Email *

Message *