29, ఫిబ్రవరి 2012, బుధవారం

CLASSES ,OBJECTS,CONSTRUCTORS, INPUT/OUTPUT, LOOPS AND STRINGS IMPLEMENATAION IN A SIMPLE JAVA PROGRAM

java language allows three types of comments
1)single line comments //
2)multiline comments /* ...*/
3)Documentation comments /** .. */

The key considerations were summed up by the Java team in the following list of buzzwords:
SIMPLE       easy to learn and use
 SECURE  programs are confined to the Java execution environment and cannot access other parts of the computer.
 PORTABLE
 OBJECT-ORIENTED  a clean, usable, pragmatic approach to objects, not restricted by the need for compatibility with other languages.
 ROBUST    allow the programmer check the mistakes early performs compile-time (strong typing) and run-time (exception-handling) checks, manages memory automatically.
 MULTITHREADED     writing program that perform concurrent computations
 ARCHITECTURE-NEUTRAL J V M provides a platform independent environment for the execution of Java byte code
 INTERPRETED  compiled into an intermediate byte code
 HIGH PERFORMANCE
 DISTRIBUTED Java handles TCP/IP protocols, accessing a resource through its URL much like accessing a local file.
DYNAMIC substantial amounts of run-time type information to verify and resolve access to objects at run-time.


Data Types :- Java defines eight simple types:
1)byte – 8-bit integer type
2)short – 16-bit integer type
3)int – 32-bit integer type
4)long – 64-bit integer type
5)float – 32-bit floating-point type
6)double – 64-bit floating-point type
7)char – symbols in a character set
8)boolean – logical values true and false


Scope of a variable:-
A variable declared inside the scope is not visible outside:
{
int n;
}
n = 1;// this is illegal

Two styles of array declaration:   type array-variable[];or   type [] array-variable;
Multidimensional arrays are arrays of arrays:
1) declaration:     int array[][];
2) creation:          int array = new int[2][3];
3) initialization    int array[][] = { {1, 2, 3}, {4, 5, 6} };

 bitwise operators
~
~op
Inverts all bits
&
op1 & op2
Produces 1 bit if both operands are 1
|
op1 |op2
Produces 1 bit if either operand is 1
^
op1 ^ op2
Produces 1 bit if exactly one operand is 1
>> 
op1 >> op2
Shifts all bits in op1 right by the value of op2
<< 
op1 << op2
Shifts all bits in op1 left by the value of op2



A class is a blueprint that defines the variables and methods common to all objects of a certain kind.
Example: ‘your dog’ is a object of the class Dog.
An object holds values for the variables defines in the class.
An object is called an instance of the Class

Real world objects are things that have:
Example: your dog:
state – name, color, breed, sits?, barks?, wages tail?, runs?
behavior – sitting, barking, waging tail, running
A software object is a bundle of variables (state) and methods (operations).


A SAMPLE PROGRAM WHICH EXPLAINS THE CONCEPT OF CLASSES AND OBJECTS
import java.io.*;
import java.lang.*;
class box
{
//INSTANCE OR CLASS VARIABLES 
int length;
int breadth;
int height;
//method-1 constructor
box()
{
length=1;
breadth=2;
 height=2;
}
//method-2  reading data
void getdata(int l,int b, int h)
{
length=l;
 breadth=b;
 height=h;
}
//method-3 display the values on the screen
void display()
{
System.out.println("the values of box are "+length+"  , "+ breadth+ "  ,   "+height);
}
//method-4 calculation of the volume
void volume()
{
int volume;
volume=length* breadth* height;
System.out.println("the volume is"+volume);
}
}


//SECOND CLASS the above class implementation is done here
public class boximpl
{
public static void main(String args[]) throws Exception
{
//here we are initilizing the data to the object through constructor
box b=new box();
b.display();
b.volume();

//here we are reassigning values through method which is already done by the constructor
b.getdata(1,2,3);
b.display();
b.volume();


/*now we will consontrate how repeatedly do the volume calculation by taking concern of user each and every iteration this can be done by so many was here i assisted the string class  and activating of one of the reader class for user input*/ 
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s="yes";
while(s.equals("yes"))
{
System.out.println("WELCOME TO THE NEW ITERATION"+'\n'+'\n');
System.out.println("enter the value for the length");

int l1= Integer.parseInt(br.readLine());
System.out.println("enter the value for the breadth");
int b1= Integer.parseInt(br.readLine());
System.out.println("enter the value for the height");
int h1= Integer.parseInt(br.readLine());

b.getdata(l1,b1,h1);
b.display();
b.volume();
System.out.println("do u wants repetation ");
 s=br.readLine();
}
}//end of main()
}//end of


OUTPUT:-
compilation and interpretation
e:\ramu>javac boximpl.java


e:\ramu>java boximpl

The values of box are 1  , 2  ,   2
The volume is4
The values of box are 1  , 2  ,   3
The volume is6
WELCOME TO THE NEW ITERATION


enter the value for the length
1
Enter the value for the breadth
2
Enter the value for the height
3
The values of box are 1, 2,  3
The volume is6
Do u wants repetition
Yes

Access Specifiers: An access specifier is a key word that represents how to access a member
of a class. There are four access specifiers in java.
private: private members of a class are not available outside the class.
public: public members of a class are available anywhere outside the class.
protected: protected members are available outside the class.
default: if no access specifier is used then default specifier is used by java compiler.
Default members are available outside the class.


IN THIS I AM FRAMING HOW WE WILL PASS OBJECTS AS REFERENCE TO THE SAME CLASS ANOTHER OBJECT METHOD 
class Complex
{
private int real,imag;
public void Complex(int x,int y)
{
real=x;
imag=y;
}
Complex()
{
real=10;
imag=20;
}
public void Complex(int x)
{
real=x;
}

public void display()
{
System.out.println(+real+"i"+imag);
}

public void add(Complex c1, Complex c2)
{
real=c1.real+c2.real;
imag=c1.imag+c2.imag;
}}
class ramu
{
public static void main(String args[])
{
Complex c1 = new Complex();
Complex c2 = new Complex();
Complex c3 = new Complex();
c1.display();
c2.display();
c3.display();
c1.Complex(1,2);
c2.Complex(2,3);
c1.display();
c2.display();
//here I am passing to the same class object references two the third class object method add()
c3.add(c1,c2);
c3.display();
}}

STRING CLASS

String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1;
  strOb1.length();    strOb1.charAt(3);
if(strOb1.equals(strOb2))

String myString = "I" + " like " + "Java.";  //in this + string operator concatenation  variable contains ilikejava

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి