Coding Examples
Coding Examples
package com.java2novice.arrays;
import java.util.Arrays;
import java.util.List;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Arrays;
2 4 2 4 5 6 3
My new array elements:
4 2 4
import java.util.Arrays;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Comparator;
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}});
for(String str:strArr){
System.out.println(str);
}
}
}
Output:
C++
JAVA
PERL
PLAY
STRUTS
Program: Example for singleton class using static
block.
Since static blocks will be called only once, we can use static blocks to develop singleton class. Below example
shows how to create singleton classes using static block. To create singleton class, make constructor as private, so
that you cannot create object outside of the class. Create a private static variable of same class type, so that created
object will be pointed to this reference. Now create static block, and create object inside static block. Since static
block will be called only once, the object will be created only once.
package com.java2novice.staticexmp;
class MySingleton{
static{
instance = new MySingleton();
}
private MySingleton(){
System.out.println("Creating MySingleton object...");
}
package com.java2novice.staticexmp;
public class MyStaticVsConstructor {
static {
System.out.println("I am in static block");
System.out.println("Static block will be called first than
constructor!!!");
System.out.println("Static block will be called only once.");
}
public MyStaticVsConstructor(){
System.out.println("I am in constructor");
}
package com.java2novice.enums;
switch (myFruit) {
case GRAPE:
System.out.println("A grape is a non-climacteric fruit.");
break;
case APPLE:
System.out.println("The apple is the pomaceous fruit.");
break;
case MANGO:
System.out.println("The mango is a fleshy stone fruit.");
break;
case LEMON:
System.out.println("Lemons are slow growing varieties of
citrus.");
break;
default:
System.out.println("No desc available.");
break;
}
}
enum Fruits {
GRAPE, APPLE, MANGO, LEMON,GUAVA
}
Output:
A grape is a non-climacteric fruit.
The apple is the pomaceous fruit.
Lemons are slow growing varieties of citrus.
No desc available.
package com.java2novice.enums;
switch (myFruit) {
case GRAPE:
System.out.println("A grape is a non-climacteric fruit.");
break;
case APPLE:
System.out.println("The apple is the pomaceous fruit.");
break;
case MANGO:
System.out.println("The mango is a fleshy stone fruit.");
break;
case LEMON:
System.out.println("Lemons are slow growing varieties of
citrus.");
break;
default:
System.out.println("No desc available.");
break;
}
}
class MyWrapper{
enum Fruit {
GRAPE, APPLE, MANGO, LEMON,GUAVA
}
}
Output:
A grape is a non-climacteric fruit.
The apple is the pomaceous fruit.
Lemons are slow growing varieties of citrus.
No desc available.
package com.java2novice.enums;
enum Fruit {
GRAPE{
public String toString(){
return "A grape is a non-climacteric fruit.";
}
},
APPLE{
public String toString(){
return "The apple is the pomaceous fruit.";
}
},
MANGO{
public String toString(){
return "The mango is a fleshy stone fruit.";
}
},
LEMON{
public String toString(){
return "Lemons are slow growing varieties of citrus.";
}
}
}
package com.java2novice.enums;
enum Department{
ACCOUNT(12),HR(24),FINANCE(100),SECURITY(108);
Department(int id){
deptId = id;
}
package com.java2novice.wrapper.integer;
package com.java2novice.wrapper.integer;
static{
myObj = new MySingleton();
}
private MySingleton(){
if(str.length() == 1){
return str;
} else {
reverse += str.charAt(str.length()-1)
+reverseString(str.substring(0,str.length()-1));
return reverse;
}
}
int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
if(temp == number){
System.out.println("It is a perfect number");
return true;
} else {
System.out.println("It is not a perfect number");
return false;
}
}
package com.java2novice.algos;
import java.util.Arrays;
public MyArrayList(){
myStore = new Object[10];
}
}
public int size(){
return actSize;
}
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Map.Entry;
package com.java2novice.algos;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
int sum = 0;
if(number == 0){
return sum;
} else {
sum += (number%10);
getNumberSum(number/10);
}
return sum;
}
public static void main(String a[]){
MyNumberSumRec mns = new MyNumberSumRec();
System.out.println("Sum is: "+mns.getNumberSum(223));
}
}
package com.java2novice.algos;
int decimal = 0;
int power = 0;
while(true){
if(binary == 0){
break;
} else {
int tmp = binary%10;
decimal += tmp*Math.pow(2, power);
binary = binary/10;
power++;
}
}
return decimal;
}
package com.java2novice.algos;
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for(int i=0;i<arr.length;i++){
boolean isDistinct = false;
for(int j=0;j<i;j++){
if(arr[i] == arr[j]){
isDistinct = true;
break;
}
}
if(!isDistinct){
System.out.print(arr[i]+" ");
}
}
}
package com.java2novice.algos;
System.out.println("\"3256\" == "+convert_String_To_Number("3256"));
System.out.println("\"76289\" == "+convert_String_To_Number("76289"));
System.out.println("\"90087\" == "+convert_String_To_Number("90087"));
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
BufferedReader br = null;
String filePath = args[0];
int topList = 0;
Set<Entries> liSet = new TreeSet<Entries>(new MyComp());
try {
br = new BufferedReader(new FileReader(new File(filePath)));
String line = br.readLine();
topList = Integer.parseInt(line.trim());
while((line = br.readLine()) != null){
line = line.trim();
if(!"".equals(line)){
liSet.add(new Entries(line.length(), line));
}
}
int count = 0;
for(Entries ent:liSet){
System.out.println(ent.line);
if(++count == topList){
break;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public int compare(Entries e1, Entries e2) {
if(e2.length > e1.length){
return 1;
} else {
return -1;
}
}
}
}
int number = 2;
int count = 0;
long sum = 0;
while(count < 1000){
if(isPrimeNumber(number)){
sum += number;
count++;
}
number++;
}
System.out.println(sum);
}
import java.util.HashSet;
import java.util.Set;
return sb.toString();
}
int j = 0;
int i = 1;
//return if the array length is less than 2
if(input.length < 2){
return input;
}
while(i < input.length){
if(input[i] == input[j]){
i++;
}else{
input[++j] = input[i++];
}
}
int[] output = new int[j+1];
for(int k=0; k<output.length; k++){
output[k] = input[k];
}
return output;
}