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.

What is Java?
Java is an object oriented language. It was developed in 1991 in U.S.A by Sun Microsystem. It is much reliable and portable language than C++. Previously Java Language was known as Oak. Java can do two type of work:-
·        Stand Alone application programming
·        Applet programming
FEATURE:-
·        It is object oriented
·        Platform independent
·        Portable
·        Distributed
·        Security
·        Multithread Language
·        Write once read many
·        Extensible and dynamic
What is Java Development Kit {JDK}?
JDK provide a number of tools which essential to develop and run Java program these tools are:-
·        Javac:- java compiler. It translate Java Source code to byte code file to make them understandable by the interpreter.
·        Java:- java interpreter. It runs applet and application by reading and interpreting byte code file.
·        Jdb:- java debugger. This help in finding error and removing them from the program which is being compiled
·        Javah:- it produces header file.

·        Javap:- Java disk assembler. This help in converting byte code file into a program description.
Encapsulation is one of the major properties of OOP. How is it implemented in software terms?
Encapsulation is a way to implement data hiding and abstraction by wrapping up data and associated functions into single unit called object.
                               An object binds together data and its associated functions under one unit thereby enforcing encapsulation as encapsulation means wrapping up data and associated functions together into a single unit.
What is polymorphism?

It is the ability of representing a thing in more than one form. Polymorphism is used in constructor overloading.
What are data members & member functions?
The variables which are define globally in the class  just after the defining  of the class name. For E.g.
Class pen
{
int a,int b; //Data Members
{
public void cap()   //member function
{
..
}

}
What is inheritance?
Inheritance is a technique which is used to derive or build new classes from existing class. A class may be derive from one or more classes. The new classes are called derived class or sub class and existing class is called base class or super class. The derived class inherit the property of its base including its data members and member methods. It can also add its own member method and data members.
BENEFITS:-
        ·        Data in existing class can be modify easily.

        ·        Object oriented hierarchy can be built.
What is class?
A class is collection of object. It is a user defined aggregate data type. A class may be defined as a group of object with same attribute and operations. For E.g. the color (class) contain various color; red, blue, etc. (objects)
Class contain the following attributes or members:-
         a)      Data Members
         b)      Member functions

         c)      Constructor

Thursday, December 12, 2013

What is encapsulation?
Encapsulation means characteristics and behavior are combined in one unit. In other words, encapsulation is the way of combining both data (characteristics) and the function (behavior) that operates on that data under a single unit.

The wrapping up of the data into a single unit is known  as  encapsulation.
OBJECTS ENCAPSULATE STATE AND BEHAVIOUR

Can you clearly make out what this statement –“objects encapsulate state and behaviour”- is trying to convey? No? Same problem was with me also but when I read it for the first, go deep and you will find easy. Well let’s first discuss a term called encapsulation, after which the above statement will become clear to you. We shall also talk about linked term ”abstraction” to understand it more clearly.
What is abstraction?
Abstraction is a concept of simplifying a real world concept into its essential elements.
    Abstraction refers to the act of representing essential feature without including the background details or explanations.

To understand abstraction let us take an example. Suppose you are driving a car.You only know the essential features to drive a car i.e. gear handling, steering handling,use of clutch, brakes,accelerator etc. But while driving do you get into internal details of car like wiring, petrol tank, motor workingetc. ? You just change the gear or apply the brakes etc. What is happening inside is hidden from you. That what abstraction is, where you only know the essential things to drive a car without including the background details or explanations.
INTRODUCTION
The world we live in is composed of objects. Everything that we see is an example of object. We ourselves are examples of objects. The chair you are sitting on is another example of objects. The car, your father drives is also an example of object. The pets in your house are also objects. The list is endless.
Aren’t you wondering what this objects all about is? Why are we referring to everything as an object? Well, answer to all these questions, you will find in coming lines. This chapter is dedicated to the discussion of concepts of objects. Let us begin with what exactly an object is.
What is an object?
An object, in real world, can be one of the following:
·        Anything that is visible or tangible.
·        Something that may be apprehended intellectually.
·        Something towards which one can direct action or thought
If we study all things meeting above criteria of being objects , we can say that each of these has a unique identity , some definitive state or characteristics and some behavior .For instances , we can say that an ”ORANGE” is an object . Its characteristics are: it is spherical shaped; its color is orange etc. Its behavior is: it is citrus in nature and it tastes sweet sour. Similarly, we can say that your chair is also an object. Its characteristics are: it has four legs, a back, may or may not have arms. Its behavior is: it let you sit on it. With this description can you suggest some more object examples?
Keep thinking, in the mean time, we define object as follows:
An object can be thought of as an entity having specific identity, specific characteristics and specific behavior.
OR
Java is on object oriented programming language. An object is a self contained element of computer program that represent a related group of feature and is design to do specific task. Objects are also called instances.
      Every object has the following inbuilt characteristics, which we had already discussed above:-
            a)      Identity :- it is the name by which an object is recognize. E.g. pen ,box, chair etc.
            b)      State :- the object may be in any state that is processed or unprocessed. For example the computer may be in any of the following state; on state ; off state; out of order.
             c)      Behaviour :- this refers that what an object is capable of doing.

NOTE: The state of an object is represented through the values/attributes of its characteristics at a given point of time.