HKProgrammingWorld

HKProgrammingWorld Think Java...Think HK,
Learning is Simplified. Believe Us...Success is Yours. This page is created This page is created and maintaining by Mr.
(488)

Hari Krishna, Naresh i Technologies for sharing Java and its related Technologies knowledge. Through this page we will share the activities we do in hkprogrammingworld.in website. It is an online educational portal for Java Beginners to become Java experts.Through this website you can learn Java easily.

04/01/2025
Hi buddies
12/01/2023

Hi buddies

29/09/2022

Custom Collection Development
Developing Our Own ArrayList
==========================

public class NITCollection {

private Object[] elementData;
private int elementCount;

public NITCollection() {
elementData = new Object[10];
}

public void add(Object obj) {
if(size() == capacity()) {
grow();
}

elementData[elementCount] = obj;
elementCount++;

}

private void grow() {
Object[] nextArray = new Object[capacity() * 2];

for(int i=0; i=0;
}

public int indexOf(Object obj) {

if(obj == null) {
for(int i=0; i=0; i--) {
if(obj.equals(elementData[i])) {
return i;
}
}
return -1;
}

}


public Object get(int index) {
checkIndex(index);

return elementData[index];
}


private void checkIndex(int index) {
if(index =elementCount)
throw new IndexOutOfBoundsException(index);
}

public Object remove(int index) {
checkIndex(index);

Object ele = elementData[index];

for(; index=index; i--) { //moving elements
elementData[i+1] = elementData[i]; //size to up to passed index
}

elementData[index] = obj; //inserting argument object in the given location
elementCount++; //increasing size

}

public Object set(int index, Object obj) {
checkIndex(index);

Object ele = elementData[index];

elementData[index] = obj;

return ele;
}


public String toString() {

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");

for(int i=0; i String -> overridden -> content
System.out.println(col.contains(5)); //5.equals(ele) in loop -> Integer -> overridden -> content
System.out.println(col.contains(true));//true.equals(ele)->Boolean-> Overridden-> content
System.out.println(col.contains(null));//null.equals(ele)->NPE
System.out.println(col.contains(new Sa(5,6))); //false ->Sa(5,6).equals(ele)->!Overridden->ref
System.out.println(col.contains(s3)); //true ->s3.equals(ele)->!Overridden->ref
System.out.println();

System.out.println(col.indexOf("a"));
System.out.println(col.indexOf("A"));
System.out.println(col.indexOf(5));
System.out.println(col.indexOf(true));
System.out.println(col.indexOf(null));
System.out.println(col.indexOf(new Sa(5,6)));
System.out.println(col.indexOf(s3));
System.out.println();

System.out.println(col.lastIndexOf("a"));
System.out.println(col.lastIndexOf("A"));
System.out.println(col.lastIndexOf(new Sa(5,6)));
System.out.println(col.lastIndexOf(s3));
System.out.println();

System.out.println(col);
System.out.println(col.get(0));
System.out.println(col.get(5));
System.out.println(col.get(10));
System.out.println(col.get(12));

Object obj = col.get(9);
System.out.println(obj);

Sa sa = (Sa)obj;
System.out.println(sa.getX());
System.out.println(sa.getY());

sa.setX(23);
sa.setY(24);

System.out.println(sa.getX());
System.out.println(sa.getY());

System.out.println(obj);
System.out.println(col);

obj = col.get(0);
String str = (String)obj;
str.toUpperCase();
System.out.println(col);
System.out.println();

System.out.println(col.capacity());
System.out.println(col.size());
//System.out.println(col.get(-1));
//System.out.println(col.get(15));
System.out.println();

System.out.println(col);//[a, b, 5, 6.7, p, true, null, a, 5, Sa(23, 24), a, Sa(7, 8), Sa(9, 10)]
col.remove(0);
System.out.println(col);//[b, 5, 6.7, p, true, null, a, 5, Sa(23, 24), a, Sa(7, 8), Sa(9, 10)]
col.remove(4);
System.out.println(col);//[b, 5, 6.7, p, null, a, 5, Sa(23, 24), a, Sa(7, 8), Sa(9, 10)]
col.remove(9);
System.out.println(col);//[b, 5, 6.7, p, null, a, 5, Sa(23, 24), a, Sa(9, 10)]

System.out.println("6.7 removed: "+ col.remove(6.7));
System.out.println(col);//[b, 5, p, null, a, 5, Sa(23, 24), a, Sa(9, 10)]

System.out.println("null removed: "+ col.remove(null));
System.out.println(col); //[b, 5, p, a, 5, Sa(23, 24), a, Sa(9, 10)]

System.out.println("\"b\" removed: "+ col.remove(new String("b")));
System.out.println(col); //[5, p, a, 5, Sa(23, 24), a, Sa(9, 10)]

System.out.println("Sa(23,24) removed: "+ col.remove(new Sa(23, 24)));
System.out.println(col); //[5, p, a, 5, Sa(23, 24), a, Sa(9, 10)]

col.remove(5);
System.out.println(col); //[5, p, a, 5, Sa(23, 24), Sa(9, 10)]

col.remove((Integer)5);
System.out.println(col); //[p, a, 5, Sa(23, 24), Sa(9, 10)]

//col.remove('p');
col.remove((Character)'p');
System.out.println(col); //[a, 5, Sa(23, 24), Sa(9, 10)]

System.out.println();
System.out.println(col.capacity());
System.out.println(col.size());
System.out.println(col);
col.add(1, "X");
System.out.println(col);
System.out.println(col.capacity());
System.out.println(col.size());
System.out.println();

NITCollection col2 = new NITCollection();
for(int i=1; i

27/05/2022
interrupt() method terst case program=========================class MyThread13 extends Thread {  public void run() { Sys...
17/04/2022

interrupt() method terst case program
=========================
class MyThread13 extends Thread {

public void run() {

System.out.println("run start");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("IE raised");
}
System.out.println("run end");
}
}
public class Test23 {
public static void main(String[] args) throws InterruptedException {
System.out.println("main start");

MyThread13 mt = new MyThread13();
mt.interrupt();

mt.start();
//mt.interrupt();

Thread.sleep(2000);

//mt.interrupt();

System.out.println("main end");
}
}

//run() method ex*****on flow test cases===============================class MyRunnable implements Runnable { public voi...
16/04/2022

//run() method ex*****on flow test cases
===============================
class MyRunnable implements Runnable {
public void run(){
System.out.println("From MyRunnable.run() ");
}
}
class MyThread extends Thread {
MyThread(){
super();
}

MyThread(Runnable target){
super(target);
}


public void run(){
System.out.println("From MyThread.run() ");
super.run();
}
}
class Test04_RunExecution{
public static void main(String[] args) {
/*
Thread th1 = new Thread();
th1.start();

MyThread mt = new MyThread();
mt.start();

MyRunnable mr = new MyRunnable();
mr.start();

Thread th2 = new Thread(mr);
th2.start();

MyThread mt = new MyThread();
Thread th3 = new Thread(mt);
th3.start();

Thread th4 = new MyThread();
th4.start();

Thread th5 = new MyRunnable();
th5.start();

Runnable r = new MyRunnable();
r.start();

Thread th6 = new Thread(r);
th6.start();

MyRunnable mr2 = new MyRunnable();
MyThread mt2 = new MyThread(mr2);
mt2.start();

MyThread mt3 = new MyThread();
MyThread mt4 = new MyThread(mt3);
mt4.start();
*/
// MyThread mt5 = new MyThread(mt5);
// mt5.start();

mt6 = new MyThread(mt6);
mt6.start();

}
static MyThread mt6;
}

Storing primitive values, array object and class object by using another class and its object memory diagram============...
18/03/2022

Storing primitive values, array object and class object by using another class and its object memory diagram
==========================================
class Example {
int i1 = 5;
double d1 = 6.7;
char ch = 'a';
long[] la = {8, 9};
String s = "Hari";
}

class Sample {
float f1 = 4.5F;
boolean bo = true;
char[] ch = {'p', 'q'};
Example e1 = new Example();
}

class Test11_ClassItsAdv{
public static void main(String[] args) {

//Adv #1: We can store multiple values of different type as one group with name
Sample s1 = new Sample();

System.out.println(s1);
System.out.println(s1.f1);
System.out.println(s1.bo);
System.out.println(s1.ch);
System.out.println(s1.e1);
System.out.println(s1.e1.i1);
System.out.println(s1.e1.d1);
System.out.println(s1.e1.ch);
System.out.println(s1.e1.la);
System.out.println(s1.e1.la[0]);
System.out.println(s1.e1.la[1]);
System.out.println(s1.e1.s);
System.out.println();

//Adv #2: We can pass multiple values of different type as arguemtn with parameter name
m1(s1);
System.out.println();

//Adv #3: We can return multiple values of different type from a method with single return type
Sample s2 = m2();
System.out.println(s2);
System.out.println(s2.f1);
System.out.println(s2.bo);
System.out.println(s2.ch);
System.out.println(s2.e1);
System.out.println(s2.e1.i1);
System.out.println(s2.e1.d1);
System.out.println(s2.e1.ch);
System.out.println(s2.e1.la);
System.out.println(s2.e1.la[0]);
System.out.println(s2.e1.la[1]);
System.out.println(s2.e1.s);
System.out.println();
}

static void m1(Sample s){
System.out.println(s);
System.out.println(s.f1);
System.out.println(s.bo);
System.out.println(s.ch);
System.out.println(s.e1);
System.out.println(s.e1.i1);
System.out.println(s.e1.d1);
System.out.println(s.e1.ch);
System.out.println(s.e1.la);
System.out.println(s.e1.la[0]);
System.out.println(s.e1.la[1]);
System.out.println(s.e1.s);
}

//Adv #3: We can return multiple values of different type from a method with single return type
static Sample m2(){
return new Sample();
}
}

Assignments on Array Programming[Part-1]===========================================You must develop below program by usi...
15/03/2022

Assignments on Array Programming[Part-1]
===========================================
You must develop below program by using both tradition approach by using for loop and by using Java 8v Stream API approach
===========================================
1. Develop a program to create an array of integers, display all values on console with their position
starts with 1. Support if an array has values 3, 4, 5, 6, 7 you must display values as
Value 1: 3
Value 2: 4
Value 3: 5
Value 4: 6
Value 5: 7

2. Develop a program to create an array of integers, display only even indexes elements
Support if an array has values 3, 4, 5, 6, 7 you must display values "even index" values 3 5 7
0 1 2 3 4

3. Develop a program to create an array of integers, display only even elements
Support if an array has values 3, 4, 5, 6, 7 you must display values "even elements" 4 6
0 1 2 3 4

4. Develop a program to create an array of integers, display only the elements divisible by 3
Support if an array has values 3, 4, 5, 6, 7 you must display values "/ 3 " 3, 6
0 1 2 3 4

5. Develop a program to create an array of integers, search for the element 2,
if available display "the value 2 is available"
if no available display "the value 2 is not available"

Support if an array has values 3, 4, 5, 6, 7 you must display
O/P: the value 2 is not available

Support if an array has values 3, 4, 5, 2, 6 you must display
O/P: the value 2 is available

6. Develop a program to create an array of integers, search for the element 2,
if available display "the value 2 is available"
if no available display "the value 2 is not available"

Support if an array has values 3, 4, 5, 2, 6 you must display
O/P: the value 2 is available

Support if an array has values 3, 4, 5, 6, 7 you must display
O/P: the value 2 is not available

7. Develop a program to create an array of integers, search for the element 2,
if available display "its index number" to tell that value 2 is present in array at so and so location
if not available display "-1" to tell that value 2 is not present in array


Support if an array has values 3, 4, 5, 2, 6 you must display
O/P: 3

Support if an array has values 3, 4, 5, 6, 7 you must display
O/P: -1

7. Develop a program to create an array of integers, REMOVE the element 2, FROM THE ARRAY

Support if an array has values 3, 4, 5, 2, 6 after remove 2 array must be
O/P: [3, 4, 5, 2, 6]
O/P: [3, 4, 5, 6]

8. Develop a program to create an array of integers, INSERT the element 2 in this ARRAY at 3rd index

Support if an array has values 3, 4, 5, 6 after inserting 2 array must be
O/P: [3, 4, 5, 6]
O/P: [3, 4, 5, 2, 6]

9. Develop a program to create an array of integers, REPALCE the element 2 with 9

Support if an array has values 3, 4, 5, 2, 6 after repalcing 2 array must be
O/P: [3, 4, 5, 2, 6]
O/P: [3, 4, 5, 9, 6]

10. Develop a program to create an array of integers, SORT the elements in array in Ascending order

Support if an array has values 3, 4, 5, 2, 6 after sorting array must
O/P: [3, 4, 5, 2, 6]
O/P: [2, 3, 4, 5, 6]

class Test18_ArrayRules { public static void main(String[] args) {  //1. Declaring an array (1D and 2D) //2. Creating an...
14/03/2022

class Test18_ArrayRules {
public static void main(String[] args) {

//1. Declaring an array (1D and 2D)
//2. Creating an array object (1D and 2D with three syntaxes)
//3. Initializing and modifying array object
//4. Reading array object values

//================== Rules on declaring an array ========================

//Rule #1: We can place [] after DT, before VN, after VN,
//but we can not place before DT

int[] ia1;
int []ia2; //=> int[] ia2;
int ia3[];
//[]int ia4;
int[]ia5;

int[][] ia6;
int [][]ia7; //int[][] ia7;
int ia8[][];
//[][]int ia9;
int[][]ia10;
int[] ia11[];
int[] []ia12[]; //int[][] ia12[];

//What is the difference in placing [] after DT and after variable name?
int[] ia13; //DT is int[] and VT int[]
int ia14[]; //DT is int and VT int[]

int[] ia15, ia16; //DT is int[] and both ia15 and ia16 are int[]
int ia17[], ia18; //DT is int and ia17 is int[] and ia18 is int

ia17 = new int[5];
ia18 = 7;

//ia17 = 7;
//ia18 = new int[5];

//Q1) What will be happened if we place [] before variable name?
//It will be attached to DT

//Q2) Can we create array type variable and normal type variable in a single statement?
//A) Yes, but by data type will be same

int ia19, ia20[];
int ia21[], ia22[];
int[] ia23, ia24;

//Rule #2: we can not palce [] before second variable onwards
// we are allowed to place only before first variable

int []ia25;
int []ia26, ia27; //both are array type variables, because [] will be attached to DT

//ia25 = 10;
//ia26 = 11;

//int ia28, []ia29; //CE:

int[][] ia30, ia31; //both are 2d arrays
int[] ia32[], ia33; //ia32 is 2D and ia33 is 1D
int[] ia34, ia35[]; //ia34 is 1D and ia35 is 2D

int[] []ia36, ia37; //both are 2D arrays
//int[] ia38, []ia39; //CE:

//Rule #3: Like in C langauge, in Java we can not specify size in array declaration side
// size is allowed only in object creation side
//int[5] ia40;
int[] ia41;

//======================= Ruels on creating an array object ===================
//DT[] vN = new DT[size];
//DT[] vN = {v1, v2, v3, ....};
//DT[] vN = new DT[]{v1, v2, v3, ....};

//============ Rule based on DT[] vN = new DT[size]; array object creation=============

//Rule #4: We must specify size of the array in array object creation side
//int[] ia42 = new int[]; //CE: array dimension is missing

//Rule #5: Array size is int type, so we must pass eithe int or char or byte or shor type values only
//if we pass other types like long, float, double, boolean or String we will get CE: i c t
int[] ia43 = new int[5]; //array with 5 locations
int[] ia44 = new int['a']; //array with 97 locations

//int[] ia45 = new int[5L]; //CE
//int[] ia45 = new int[5F]; //CE
//int[] ia45 = new int[5D]; //CE
//int[] ia45 = new int[true]; //CE
//int[] ia45 = new int["a"]; //CE

//You create array object of any type, its size must be int or its lesser range values
//long[] la = new long[5L];
//String[] sa = new String["abc"];
//Example[] ea = new Example[5.7];
//boolean[] ba = new boolean[true];

boolean[] ba = new boolean[5];

//boolean[3] ba = new boolean[5];
//boolean[5] ba = new boolean[5];

//Rule #6: Size must be +ve int or its lesser range value
//if we pass -ve value as size, we won't get CE, but we will get RE: NASE
//int[] ia46 = new int[-5];

//Rule #7: like PDT, their array types are not compatible
//if we assign one PDT array to another PDT array, we will get CE:

int i1 = 'a';
long l1 = i1;
//boolean bo = l1;

int[] ia47 = new int[5];
//long[] ia48 = new int[5];
//int[] ia49 = new char[5];

//============ Rule based on DT[] vN = {v1, v2, v3, ...}; array object creation=============

//Rule #8: inside {}, we are allowed to place values of either same type or lesser type of this array type

int[] ia50 = {}; //0 locations array, empty array
int[] ia51 = {3}; //1 location array, with value 3
//int[] ia52 = {3, 4L};
int[] ia53 = {3, 'a'};

//============ Rule based on DT[] vN = new int[]{v1, v2, v3, ...}; array object creation=============

//Rule #9: We can not specify size here in this syntax
int[] ia54 = new int[]{}; //size=0, number of values we specified in {}
int[] ia55 = new int[]{3}; //size=1, number of values we specified in {}
//int[] ia55 = new int[3]{}; //CE: array creation with both dimension expression and initialization is illegal

//Note: because this sytax is combination of both 1st and 2nd syntax those two syntaxes
//rules are also applied on this syntaxes. That means we must applied total 6 rules from #4 to #9

//================== Rules on MD array =======================
//Rule #10: In MD array creation parent array size is mandatory
//if we donot mension child array size no CE, child arrays are not created.

//int[][] ia56 = new int[][];
int[][] ia57 = new int[3][2];
int[][] ia58 = new int[3][];
//int[][] ia59 = new int[][2];

//Rule #11:
int[][] ia59 = {}; //0 location parent array

int[][] ia591 = {{}}; //1 parent array and 1 child array
//1 location parent array and zero locations child array

int[][] ia60 = {{3}}; //1 parent array and 1 child array
//1 location parent array and 1 location child array with the value 3

int[][] ia61 = {{3, 4}}; //1 parent array and 1 child array
//1 location parent array and 2 locations child array with the values 3 and 4

int[][] ia62 = {{3, }}; //1 location parent array and 1 location child array with the value 3
System.out.println(ia62[0][0]); //3
//System.out.println(ia62[0][1]); //RE: AIOOBE

//int[][] ia63 = {{3, ,}}; //1 location parent array and 1 location child array with the value 3

int[][] ia64 = {{3, 0,}}; //1 location parent array and 1 location child array with the value 3
System.out.println(ia64[0][0]); //3
System.out.println(ia64[0][1]); //0
//System.out.println(ia64[0][2]); //RE:

int[][] ia65 = {{3}, };
System.out.println(ia65[0]); //[I
//System.out.println(ia65[1]); //RE:

int[][] ia66 = {{3}, {}}; //2 locatons with 2 child arrays
System.out.println(ia66[0]); //[I
System.out.println(ia66[1]); //[I

int[][] ia67 = {{3}, null}; //2 locations with 1 child array
System.out.println(ia67[0]); //[I
System.out.println(ia67[1]); //null
System.out.println(ia67[0][0]); //3
System.out.println(ia67[1][0]); //RE:

//3 locations with 0 and 2 locations with child array
//int[][] ia68 = {{} , , {} };
int[][] ia69 = {{} , null , {} };

//int[][] ia70 = {3, 4}; //2d array with int values. not allowed
//int[][] ia71 = {{3}, 4}; //2d array with int[] and int value, not allowed
int[][] ia72 = {{3}, null}; //2d array with int[] value and null, allowed

int[][] ia73 = {null, null}; //2d array with 2 locations with two nulls, no child arrays
int[][] ia74 = {{}, {}}; //2d array with 2 locations with two child arrays, empty child arrays, no locations
int[][] ia75 = {{}, null, {}}; //2d array with 2 locations with two child arrays, empty child arrays, no locations

}
}

/* * Types of arrays *    Java supports three types of arrays * 1. One dimensional array * 2. Multi dimensional array [2...
12/03/2022

/*
* Types of arrays
* Java supports three types of arrays
* 1. One dimensional array
* 2. Multi dimensional array [2D, 3D, ..., n D]
* 3. Jogged array [multi dimensional array with diff size child arrays]
*/

class Test15_TyoeOfArrays{
public static void main(String[] args){

//array of values
int[] ia1 = new int[5];
int[] ia2 = {3, 4, 5, 6, 7};

//array of arrays or 2D array
//DT[][] vn = new DT[parentSize][childSize];
// M O

int[][] iaa = new int[3][2];
//parent array locations -> 3
//child array locations -> 2

//how many parent arrays -> 1
//how many child arrays -> 3 with 2 locations

System.out.println(iaa);
System.out.print(" "+iaa[0] +" ----> "); System.out.println(iaa[0][0] +" " + iaa[0][1]);
System.out.print(" "+iaa[1] +" ----> "); System.out.println(iaa[1][0] +" " + iaa[1][1]);
System.out.print(" "+iaa[2] +" ----> "); System.out.println(iaa[2][0] +" " + iaa[2][1]);
System.out.println();

long time1, time2;
time1 = System.currentTimeMillis();
for(int i=0;i

Fail-Fast and Fail-Safe cursor objects==========================import java.util.ArrayList;import java.util.Collections;...
11/03/2022

Fail-Fast and Fail-Safe cursor objects
==========================
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;

/*
* The cursor that does not allow concurrent modifications on a collection object
* is called fail-fast cursor. Enumeration and Iterator are FF cursor on CF collections
*
* The cursor that allows concurrent modifications on a collection object
* is called fail-safe cursor. Iterator on concurrent collection given in
* java.util.concurrent package is fail safe cursor. because these collections
* are by default thread safe means concurrent collections
*
* Enumeration on Vector through legacy implementation with elements() method is
* fail safe, because Vector is thread safe, but Enumeration on Vector through
* Collections.enumeration(v) is fail fast because it implicitly uses Iterator
*
* Iterator on Vector is also fail fast
*
*/
public class Test24_FFnFS {
public static void main(String[] args) {

//-------------------------------------------------------------------------------
ArrayList al = new ArrayList();
al.add("a");
al.add("b");
al.add("c");
System.out.println(al);

Iterator itr = al.iterator();
al.add("d");
//System.out.println(itr.next()); //ITR on CF AL is FF hence we got CME
//-------------------------------------------------------------------------------
CopyOnWriteArrayList cowal = new CopyOnWriteArrayList();
cowal.add("a");
cowal.add("b");
cowal.add("c");
System.out.println(cowal);

itr = cowal.iterator();
cowal.add("d");
System.out.println(itr.next()); //ITR COWAL is FS hence we did not get CME
System.out.println(cowal);
System.out.println();
//-------------------------------------------------------------------------------

Vector v = new Vector();
v.add("a");
v.add("b");
v.add("C");
System.out.println(v);

Enumeration e = v.elements(); //Enumeration through elements() is FS
v.add("d"); //on Vector, because Vector is thred safe
System.out.println(e.nextElement());
System.out.println(v);

itr = v.iterator();
v.add("e");
//System.out.println(itr.next()); //Iterator is FF on Vector
//even though it Thread Safe

e = Collections.enumeration(v);
v.add("f");
System.out.println(e.nextElement()); //Enumeration with Collection.enumeration(v)
//is FF because implicitly it uses Iterator


}
}

Address

Ameerpet
Hyderabad
500016

Alerts

Be the first to know and let us send you an email when HKProgrammingWorld posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Contact The Business

Send a message to HKProgrammingWorld:

Share