Skip to main content

Posts

Showing posts from 2014

What is the difference between JVM, JDK & JRE

JVM The Java Virtual machine (JVM) is the virtual machine that run the Java bytecodes. The JVM doesn't understand Java typo, that's why you compile your *.java files to obtain *.class files that contain the bytecodes understandable by the JVM. It's also the entity that allows Java to be a "portable language" (write once, run anywhere). Indeed there are specific implementations of the JVM for different systems (Windows, Linux, MacOS, ..), the aim is that with the same bytecodes they all give the same results. JRE Java Runtime Environment (JRE) The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language. In addition, two key deployment technologies are part of the JRE: Java Plug-in, which enables applets to run in popular browsers; and Java Web Start, which deploys standalone applications over a network. It is also the foundation for the te

Why we Pass String array as argument to main method in Java

1. History. This is a convention since the days of C, maybe even earlier.     Java took most of its syntax from C. 2. When we run a java program to command prompt , we can pass some input to our Java program. Those inputs are stored in this String args array . Example : if you run your program as, java MyProgram first second then args will contain [ "first" , "second" ] i.e, arg [ 0 ] = "first" arg [ 1 ] = "second" 3. Java tend to make it simpler by using simplest of all data-types " String " . Also, all the other datatypes can be easily converted from String. 4. JVM takes care of passing any command line arguments as an array of strings to the main method. If there are no arguments given, an empty array is passed - but it's still there.

Alternating Characters [JAVA]

Problem Statement Shashank likes strings in which consecutive characters are different. For example, he likes  A B A B A , while he doesn't like  A B A A . Given a string containing characters  A  and  B  only, he wants to change it into a string he likes. To do this, he is allowed to delete the characters in the string. Your task is to find the minimum number of required deletions. Input Format The first line contains an integer  T  i.e. the number of test cases. Next  T  lines contain a string each. Output Format Print minimum number of required steps for each test case. Constraints 1 ≤ T ≤ 10 1 ≤ l e n g t h o f S t r i n g ≤ 10 5   Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import java.io.* ; import java.util.* ; import java.text.* ; import java.math.* ; import java.util.regex.* ; /** * @author Rakesh KR */ public class Solution { public static void main ( String [

Maximizing XOR [JAVA]

Problem Statement Given two integers:  L  and  R , find the maximal values of  A   xor   B  given, L ≤ A ≤ B ≤ R Input Format The input contains two lines,  L  is present in the first line. R  in the second line. Constraints 1 ≤ L ≤ R ≤ 10 3 Output Format The maximal value as mentioned in the problem statement. Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 import java.io.* ; import java.util.* ; import java.text.* ; import java.math.* ; import java.util.regex.* ; /** * @author Rakesh KR */ public class Solution { static int maxXor ( int l , int r ) { int big = 0; if ( l >=1 && r <=10*10*10){ for ( int i = l ; i <= r ; i ++){ for ( int j = l ; j <= r ; j ++){ int val = i ^ j ; big = ( big > val ) ? big : val ; } } } return big ; } public static void main ( String

Utopian Tree [JAVA]

Problem Statement The Utopian tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter. Now, a new Utopian tree sapling is planted at the onset of the spring. Its height is 1 meter. Can you find the height of the tree after  N  growth cycles? Input Format The first line contains an integer,  T , the number of test cases. T lines follow. Each line contains an integer,  N , that denotes the number of cycles for that test case. Constraints 1 <=  T  <= 10 0 <=  N  <= 60 Output Format For each test case, print the height of the Utopian tree after  N  cycles. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import java.io.* ; import java.util.* ; import java.text.* ; import java.math.* ; import java.util.regex.* ; /** * @author Rakesh KR */ public

Display a pattern in JAVA

Display following pattern in JAVA package com . rakesh . sample ; /** * @author Rakesh KR * @since December 2k14 */ public class Display { public static void main ( String args []){ String firstPattern = "+-----" ; String secondPattern = "| " ; for ( int i = 0 ; i < 10 ; i ++){ for ( int j = 0 ; j < 5 ; j ++){ System . out . print ( firstPattern ); } System . out . print ( "+\n" ); for ( int j = 0 ; j < 5 ; j ++){ System . out . print ( secondPattern ); } System . out . print ( "|\n" ); } for ( int j = 0 ; j < 5 ; j ++){ System . out . print ( firstPattern ); } System . out . print ( "+\n" ); } }

GoogleTalk Chatting through JAVA

The following code helps you to do chat by running a java program. We can maintain groups, and can send messages for all the members in the group. There are 3 classes: 1 . SendChatMsg . java 2 . Globals . java 3 . ConfigMessage . java SendChatMsg . java  is the major classes which do the process of sending chat message. You can configure your gmail userName and password here. Code : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 package com . rakesh . chat ; import java.util.List ; import org.jivesoftware.smack.ConnectionConfiguration ; import org.jivesoftware.smack.XMPPConnection ; import org.jivesoftware.smack.XMPPException ; import org.jivesoftware.smack.packet.Message ; /** * @author Rakesh KR * @since July 2k14 */ public class SendChatMsg { private static String username = "2krrakesh2@gmail