Coding_Decoding-Java

Coding_Decoding-Java Instead of mug up the facts , try to understand the facts, behavior of Java and packages Hi.. I will provide the answer. I an unable to do more post.

Dear Readers, Please share your knowledge, ask questions which belong to core java , hibernate, springs, or struts. but you guys can ask questions, definitely I will provide answer.

: Garun Mishra

28/07/2018

Write a java code to left and right rotation of a string.
package com.test.String;

public class RotateString{
public static void main(String[] args) {
String s1 = "abcd";
String s2 = "bcda";
rotateStringRight(s1,s1.length());
rotateStringLeft(s1,s1.length());
}
private static void rotateStringLeft(String s1, int n) {
int i =1;
System.out.println("Rotating string '" + s1 + "' left ")
while (i

06/08/2016

Question: Write a java code to reverse a String without using of Loop.(don't use built-in methods)

Note: You must prefer the Recursive concept. By using Recursive concept we can write this code along with substring().
package program.garun;
public class RevStringWithoutLoopAndBuiltinMethod {
public static void main(String[] args) {
RevStringWithoutLoopAndBuiltinMethod obj = new RevStringWithoutLoopAndBuiltinMethod();
String name = "Garun Mishra";
System.out.println("String Before Reverse : "+name);
System.out.println("After Reverse : ");
obj.reverseMe(name);
}
void reverseMe(String s) {
if (!s.isEmpty()) {
reverseMe(s.substring(1));
System.out.print(s.substring(0, 1));
} } }

Output :
String Before Reverse : Garun Mishra
After Reverse :
arhsiM nuraG

Hope will be helpful:
Thanks

05/08/2016

To select which method will call, the one who has String parameter or the one who has Object parameter when your calling by pass null value?
Example:
package garun.constructor;
public class MehodOverloadDemo{
void display(Object obj)
{
System.out.println("Object constructor");
}
void display(String str)
{
System.out.println("String constructor");
}
public static void main(String args[])
{
MehodOverloadDemo movd = new MehodOverloadDemo();
movd.display(null);

} }

//Output: String constructor
Question: Why String object will called? Why not for Object class.

Solution: Java Compiler tries to find out the most specific method Possible to call. As we know that String is subclass of Object class.
Here null can be String type or Object type. Because of String is most specific, so that String will call.
Actual Structure:
Class Object
{…………}

Final class String extends Object
{
// attributes of Object class
// String has their own attribute
}
So that , String is more specific than Object that’s why String will call first.
Another example:
Test2.java
public class Test2{}
Test3.java
public class Test3 extends Test2{

}
Test.java
public class Test{

public static void print(Object obj){
System.out.println("Object");
}

public static void print(Test2 obj){
System.out.println("test 2");
}

public static void print(Test3 obj){
System.out.println("Test 3");
}


public static void main(String[]args){
Test.print(null);
}

}
When we compile the program then output will be:
“Test 3”, because of test3 has child most parameter. So test3 will be most specific for the Java Compiler.

Hope This will be helpful
Thanks.
Garun Kumar Mishra
https://www.facebook.com/garunkmishra/

Instead of mug up the facts , try to understand the facts, behavior of Java and packages

05/07/2016

Try these programs that i found some miner's difference that some times ignored and that the cause of fails in interviews.
Please try these programs:

package garun.mishra;

public class Test {

public static void main(String[] args)
{
/*
Program 1:
String x = new String("xyz");
String y = "abc";
x = x + y;
System.out.println("String = "+x);
// Four object will create for String class
output: Sstring = abcxyz
*/
/*
Program 2
int result = 0;
short s = 42;
Long x = new Long("42");
System.out.println("result = " + x);
Long y = new Long(42);
System.out.println("result = " + y);
Short z = new Short("42");
Short x2 = new Short(s);
Integer y2 = new Integer("42");
Integer z2 = new Integer(42);

if (x == y)
result = 1;
if (x.equals(y) )
result = result + 10;
if (x.equals(z) )
result = result + 100;
if (x.equals(x2) )
result = result + 1000;
if (x.equals(z2) )
result = result + 10000;
System.out.println("result = " + result);
Output: 10
*/
/*
Program 3:
int result = 0;
Boolean b1 = new Boolean("TRUE");
Boolean b2 = new Boolean("true");
Boolean b3 = new Boolean("tRuE");
Boolean b4 = new Boolean("false");

if (b1 == b2) // false because it checks by reference
result = 1;
if (b1.equals(b2) ) // True So result = 0+10 = 10
result = result + 10;
if (b2 == b4) // False because of reference are different
result = result + 100;
if (b2.equals(b4) ) // False because of values are different
result = result + 1000;
if (b2.equals(b3) )
// True Because of contents are same. No matter they are in captal letter
//or small letter because of String constructors are case insensitive. So 10000+10 = 10010
result = result + 10000;

System.out.println("result = " + result);
Output: 10010
*/

/*
Program 4:
int result = 0;
Test oc = new Test();
Object o = oc;

if (o == oc)
result = 1;
if (o != oc)
result = result + 10;
if (o.equals(oc) )
result = result + 100;
if (oc.equals(o) )
result = result + 1000;
System.out.println("result = " + result); // 1101

Explaination: Even though o and oc are reference variables of different types, they are both referring to the same object.
This means that == will resolve to true and that the default equals() method will also resolve to true.

*/

/* Program 5 :
String x = "xyz";
x.toUpperCase(); // Line 2
String y = x.replace('Y', 'y');
y = y + "abc";
System.out.println(y);

Output: xyzabc
Explaination:
Line 2 creates a new String object with the value "XYZ",
but this new object is immediately lost because there is no reference to it.
Line 3 creates a new String object referenced by y. This new String object has the value "xyz"
because there was no "Y" in the String object referred to by x.
Line 4 creates a new String object, appends "abc" to the value "xyz", and refers y to the result
*/
/*
Program 6:
double value = -9.0;
System.out.println( Math.sqrt(value));
Output: NaN(Not a Number)
Eplaination: The sqrt() method returns NaN (not a number) when it's argument is less than zero.

*/
/*
Program 7:
String a = "ABCD";
String b = a.toLowerCase();
b.replace('a','d');
b.replace('b','c');
System.out.println(b);

// Output: abcd
Explaination :
String objects are immutable, they cannot be changed, in this case we are talking about the replace method which
returns a new String object resulting from replacing all occurrences of oldChar in this string with newChar.
b.replace(char oldChar, char newChar);
But since this is only a temporary String it must either be put to use straight away i.e.System.out.println(b.replace('a','d'));
Or a new variable must be assigned its value i.e. String c = b.replace('a','d');
*/
}
}

: Happy to Help you (Garun Mishra)

15/12/2015

Write a java code to find out the IP and MAC Address to current System?

package garun.mishra;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class GetYourOwnSystemMACAddress {

/**
* args
*/
public static void main(String[] args)
{
try {
InetAddress ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());

Enumeration networks = NetworkInterface.getNetworkInterfaces();
while(networks.hasMoreElements()) {
NetworkInterface network = networks.nextElement();
byte[] mac = network.getHardwareAddress();

if(mac != null) {
System.out.print("Current MAC address : ");

StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e){
e.printStackTrace();
}

}

}

-----------------------------------------------------------------
Output :
Current IP address : 192.168.1.3
Current MAC address :
Current MAC address : FC-AA-14-D0-E0-CB
Current MAC address : 00-00-00-00-00-00-00-E0



Hope this will helpfull while ISP server Programming.

: Garun Mishra

05/11/2015

Write a code to encrypt a String:
Note:
1. You must import two package import java.security.MessageDigest; import sun.misc.BASE64Encoder;.
2. Encoding is depend on what Base Encode you select(I’m using BASE64Encoder). Because of some time it may create problem while you are using Eclipse to code.
3. java.security.MessageDigest : This MessageDigest class provides the functionality of a message digest algorithm, such as MD5 or SHA. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.Like other algorithm-based classes in Java Security, MessageDigest has two major components:
a. Message Digest API (Application Program Interface):
This is the interface of methods called by applications needing message digest services. The API consists of all public methods.
b. Message Digest SPI (Service Provider Interface):
This is the interface implemented by providers that supply specific algorithms. It consists of all methods whose names are prefixed by engine. Each such method is called by a correspondingly-named public API method. For example, the engineReset method is called by the reset method. The SPI methods are abstract; providers must supply a concrete implementation.
A MessageDigest object starts out initialized. The data is processed through it using the update methods. At any point reset can be called to reset the digest. Once all the data to be updated has been updated, one of the digest methods should be called to complete the hash computation. The digest method can be called once for a given number of updates. After digest has been called, the MessageDigest object is reset to its initialized state.

Find the Code and try:
---------------------------------------------------------

import java.security.MessageDigest;
import sun.misc.BASE64Encoder;
import java.util.*;
import java.util.Scanner;

class EncyString
{
private static EncyString encStr = null;
private EncyString(){}
public static void main()
{
EncyString encstr = new EncyString();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String ");
String str1 = sc.nextLine();
//String str1 = "Garun";
System.out.println("Entered String "+str1);
try
{
String encString = EncyString.getInstance().encrypt(str1);
System.out.println("Encrypted String "+str1);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static synchronized EncyString getInstance()
{
if ( encStr == null )
{
encStr = new EncyString();
}
return encStr;
}


public synchronized String encrypt(String plaintext) throws Exception
{
MessageDigest md = null;
md = MessageDigest.getInstance("SHA");
md.update(plaintext.getBytes("UTF-8"));
byte raw[] = md.digest();
String hash = (new BASE64Encoder()).encode(raw);
return hash;
}

}

------------------------------------------------------------
Output:
Enter the String : Garun Mishra
Entered String : Garun Mishra
Encrypted String : MMjJuALPS5pJgk3Wz0qxy2WV8C8=

: Happy to Help you.. Garun Mishra

Address

Btm Layout
BTM Layout
560029,560068,560076

Telephone

9113034683

Website

Alerts

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

Shortcuts

Share

Category