Protected access modifier in Java

As per the official java documentation from Oracle: “The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.” What this means is: Say, a concrete class Parent in package com.coddicted has a protected method named ...

Add Two Integers without using Arithmetic Operators

Recently came across an interesting question of adding two numbers without using any of the arithmetic operators and thought of giving it a try. The first thought of solving the question was using bit-manipulation operations. The idea was to add the numbers bit-by-bit taking any carry forward (without actually adding them and instead using bit-wise ...

Code Jam Fair and Square

This is a solution to the FairAndSquare problem asked in the qualification round of Google Code Jam-2013. Please read the problem statement very carefully as the correctness of solution depends heavily on how you understand and interpret the problem statement. We present here two solutions to the problem one for the small input and the other for the large ...

Waiting for multiple threads to finish

Description: We need to perform four jobs , out of which three jobs are mutually exclusive and should be performed simultaneously(may be getting data from the database using select query)  and the outcome of three jobs will be served as the input for the fourth job.Hence, the fourth job could not be started untill the first three jobs are completed.   Java package com.example; ...

Triangle Test

 Description:          The following program takes three numbers as input and checks to see if these may form a triangle or not.                                  Primary Inputs:    Three numbers Primary Output:    Determine the type of triangle formed by the input numbers (if any). Platform Used:      JDK 1.6 with Notepad.  Java import java.io.*; class TriangleTest { public static void main(String[] args) { Console con = System.console(); ...

Stack in Java

Sample source code for Stack implementation in Java ===================== MyStack.java ===================== Java public class MyStack { private static int[] stackArr = new int[5]; private static int top = -1; public void push(int element) { if (!this.isFull()) { top++; stackArr[top] = element; } else { System.out.print("\nStack Overflow!!\n"); } } public int pop() { int temp = ...

Selection Sort

Selection Sort
Java implementation of the Selection Sort Algorithm. Java package com.example; public class SelectionSort { public static void main(String[] args) { int[] toSort = { 2, 9, 3, 6, 1, 0, 4, 5, 8, 7 }; System.out.println(); System.out.print("Given Array: "); for (int i = 0; i < toSort.length; i++) { System.out.print(toSort[i] + " "); } // ...

Insertion Sort

Java implementation of the Insertion Sort Algorithm Java package com.coddicted.sort; public class InsertionSort { public static void main(String args[]) { int[] toSort = { 2, 9, 3, 6, 1, 0, 4, 5, 8, 7 }; System.out.print("\nGiven Array:\t"); for (int i = 0; i < toSort.length; i++) { System.out.print(toSort[i] + " "); } int temp; // ...

Bubble Sorting

Java implementation of the Bubble Sort Algorithm Java package com.example; public class BubbleSort { public static void main(String[] args) { int[] toSort = { 2, 9, 3, 6, 1, 0, 4, 5, 8, 7 }; System.out.print("Given Array: "); for (int i = 0; i < toSort.length; i++) { System.out.print(toSort[i] + " "); } int temp; ...

Searching files for the list of keywords

Description : Following program can be used to search the files(java,jsp and js) for the given list of keywords. If present, the absolute path and Line number of occurrence will be written to the text file.   Java package com.example; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.LineNumberReader; import java.util.ArrayList; import java.util.Iterator; public class ContentSearching ...