Search This Blog

Sunday, December 29, 2013


 Ques:- Design a class Exchange to accept a sentence and interchange the first and last alphabet with each other.Do same for all the words present in a string,but single letter word will be unchanged.
For example:- INPUT | It is a warm day
                    OUTPUT| tI si a marw yad
 



/* Design a class Exchange to accept a sentence and interchange the first and last alphabet
with each other.Do same for all the words present in a string,but single letter word will be unchanged.
For example:- INPUT | It is a warm day
OUTPUT| tI si a marw yad*/

import java .io.*;
class Exchange
{
String sent,rev;
int size;
Exchange()
{
sent=rev="";
size=0;
}
void read()throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter a string");
sent=br.readLine();
size=sent.length();
}
void ex_first_last()
{
char ch,ch1,ch2,ch3;
String wd="",mid;
int i;
for(i=0;i<size;i++)
{
ch=sent.charAt(i);
if(ch!=' ')
{
wd+=ch;
}
else
{
int len1=wd.length();
ch1=wd.charAt(0);
ch2=wd.charAt(len1-1);
ch3=ch1;
ch1=ch2;
ch2=ch3;
mid=wd.substring(1,len1);
rev=ch1+mid+ch2+" ";
wd="";
}
}
}
void display()
{
System.out.println("New String "+rev);
}
}
Ques:- A class my_string has been define with a following detail:
  Data member: str,len
  member function:
  1) default constructor()
  2) void readstring()
  3) int code (int index)() return ascii code for the character at position index)
  4) void word(display longest word in the string)
  




/*A class my_string has been define with a following detail:
* Data member: str,len
* member function:
* 1) default constructor()
* 2) void readstring()
* 3) int code (int index)() return ascii code for the character at position index)
* 4) void word(display longest word in the string)
*/
import java.io.*;
class my_string
{
String str;
int len;
public my_string()
{
str="";
len=0;
}
public void readstring()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter String");
str=br.readLine();
len=str.length();
}
int code(int x)
{
char ch=str.charAt(x);
int ascii=(int)(ch);
return ascii;
}
void word()
{
int i,j,c=0,pos=0,lar=0;
String wd="",arr[]=new String[20];
char ch;
str=str+" ";
len=str.length();
for(i=0;i<len;i++)
{
ch=str.charAt(i);
if(ch!=' ')
{
wd=wd+ch;
}
else
{
arr[c]=wd;
c++;
wd="";
}
}
for(j=0;j<c;j++)
{
if(arr[j].length()>=lar)
{
lar=arr[j].length();
pos=j;
}
}
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
{
my_string ob=new my_string();
ob.readstring();
ob.word();
}
}
}
Ques:-Accept a word arrange all the character in alphabetical order of a word and
display only those alphabets which are not present in word. 




/*Accept a word arrange all the character in alphabetical order of a word and
* display only those alphabets which are not present in word*/
import java.io.*;
class sot_word
{
public static void main(String args[])throws IOException//Main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String st;
int len,i,x,j;
char ch,temp;
System.out.println("Enter a word");
st=br.readLine();
len=st.length();
st=st.toUpperCase();
char a[]=new char[len];//Array to store the each alphabet of word
for(i=0;i<len;i++)//Storing character in array
{
ch=st.charAt(i);
a[i]=ch;
}
for(i=0;i<len;i++)//Sorting alphabet using bubble sort technique
{
for(j=0;j<len-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Sorted Word");//Printing sorted word
for(i=0;i<len;i++)
{
System.out.print(a[i]);
}
System.out.println();
for(ch=a[0],x=0;ch<=a[len-1];ch++)//Converting word into give format
{
if(ch==a[x])
{
x++;

}
else
{
System.out.print(ch);//Printing the result
}
}
}
}


Ques:-Input a sentences,frame a new word by adding initial of each word and also check whether the word is palindrome word or not.


/*Input a sentences,frame a new word by adding initial of each word and
*also check whether the word is palindrome word or not. */
import java.io.*;
class palindrome_string
{
public static void main(String arg[])throws IOException//Main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String st,temp="",temp1="";
System.out.println("Enter a sentence");//Inputing A string using Buffered Reader
st=br.readLine();
st=" "+st;
int len1=st.length();
for(int i=0;i<len1;i++)
{
char ch=st.charAt(i);//Extracting initials
if(ch==' ')
{
char ch1=st.charAt(i+1);
temp=temp+ch1;//storing on another variable i.e. new string
}
}
int len2=temp.length();
for(int j=len2-1;j>=0;j--)//reversing string
{
char ch1=temp.charAt(j);
temp1=temp1+ch1;
}
if(temp1.equalsIgnoreCase(temp))//checking whether string is same or not
{
System.out.println("Palindrome Word '"+temp1+"'");
}
else
{
System.out.println("Not a palindrome word '"+temp1+"'");
}
}
}

Tuesday, December 17, 2013

What is java program?

/*Simplest of the java program*/
//By Saurabh Prakash
class simple program
{
public static void main(String args[])
{
System.out.println(“you are learning ”);
}
}

Line1:-/* Simplest of the java program*/. The description written in /* and */ is called comment. The comments are for explaining the program. The comments are not executed by java.
Line2:-//By Saurabh Prakash
The description written after // is also a comment. There is no need to close the comment line by //.
Line3:- class simple program
This line declares a class which is an object oriented. Everything is placed inside the class. The word class is a keyword. Simple program is a name of which is called identifier which specifies the name of class to be defined. The program always start with opening braces ’{’.
Line4:- public static void main(String args[])
This line defines a method named main. A java program must include this main() method. String args[] declares a parameter named args which has an array of object of the class type String. We can also write it as String[]args. The above main method is followed by another opening brace.
Line5:-System.out.print(“you are learning”);
In the above program this is the only execution statement. The print method is a member of out object which is a static data member of system class. The string to be printed is kept in double quotes. The statement end with a semi colon. This statement will give the output you are learning.
Line6:-}, this is a closing braces to close the statement.
Line7:-}, every java program must end with a closing braces. 
What is java library?
A package of useful functions in java is preloaded in a library called Java Library.
Number of libraries in java are as follows:-
·        java.lang:- for the basic runtime support for the Java language like exception, threads.
·        Java.applet:- for applet supports.
·        Java.awt:- for windowing and GUI support
·        Java.io:- for supporting input and output data.
·        Java.util:- for utility data structures.

·        Java.math:- for support for the decimal and numeric types in the SQL data type.
What is compiler?
This program converts source code into machine language at once i.e. by taking whole program into account.
What is interpreter?         
This program converts source code into machine language step by step or line by line.
What is byte code?
The java compiler translate the source code to an intermediate language called byte code. It is platform independent. It is multiple platform byte code and which has just one aspect of Java portability.
What is java virtual machine?

The program written in java language must be compiled and interpreted both. Java language use java compiler to convert source code into byte code and then these codes are interpreted by using java interpreter and this interpreter is known as Java Virtual Machine (JVM) and machine language for JVM is known as byte code and combination of JVM and API is treated as Java Working platform.