YOU ARE USER NUMBER:

YOU ARE USER NUMBER:

Saturday, July 19, 2008

JAVA BASICS!!!

JAVA BASICS!!!

I am giving you two programs and then explaining it, pls don't ignore the remarks as these are part of explanations!!!

EXAMPLE1

import java.io.*; // needed for BufferedReader, InputStreamReader, etc.
/** A Java program that demonstrates console based input and output. */
public class inputcode
{
// Create a single shared BufferedReader for keyboard input
private static BufferedReader stdin =
new BufferedReader( new InputStreamReader( System.in ) );

// Program execution starts here
public static void main ( String [] args ) throws IOException
{
// Prompt the user
System.out.print( "Type some data for the program: " );


// Read a line of text from the user.
String input = stdin.readLine();

// Display the input back to the user.
System.out.println( "input = " + input );

} // end main method

} // end MyConsoleIO class



The first line of the above program calls the method(function) java.io.* which contains the operations required for input and output operations. Next we have
public class inputcode. The work of public here is to make this class accessible to the other classes though in this case there is only one class. public class inputcode also defines the anly class in this program that is input code. The line after the curly braces that is public static void main ( String [] args ) throws IOException is explained in parts!

public: The keyword public is an access specifier that declares the main method( function ) as unprotected and therefore making it accessible to all the other classes.

static: Next appears the keyword static, which declares this method as one that belongs to the entire class and not a part of any object of the class.

void: The type modifier void states that the main method does not return any values.

throws IOException:
You will also have to inform the compiler that you are calling a method that may cause a checked exception to occur. Add the phrase throws IOException clause to the header of any method that calls stdin.readLine(). You will also need to add this clause to any method that calls your method that calls readLine.

Next come System.out.print(); which is the output code required in java. String input = stdin.readLine() reads a string from the keyboard. This is one of the simplest programs in Java!!!


EXAMPLE 2

import java.io.*;

public class sum
{

private static BufferedReader stdin =
new BufferedReader( new InputStreamReader( System.in ) );

public static void main ( String [] args ) throws IOException
{
int a,b,sum;
String c,d;// THIS IS DONE TO CREATE 2 STRING VARIABLES WHICH ARE ACTUALLY NUMBERS AND LATER WE WILL CONVERT THE STRING CONSTANTS TO INTEGERS CONSTANTS
System.out.print( "Enter the values of two numbers: " );


c= stdin.readLine();
d = stdin.readLine();
a = Integer.parseInt( c );// converts a String into an int value
b = Integer.parseInt( d );// converts a String into an int value
sum=a+b;


System.out.println( "Sum = " + sum );

}
}



The main difference of example 1 with example 2 is that in example 1 we had required some string to be displayed after acquiring the string from the user but here the problem is that we require two numbers from the user. So what we do here is simply define 2 String variables and assign them values from the user. Note that these two values acquired are still after all strings and can't be used for summation or for that matter any mathematical operations. So we require to convert the string constants to integer constants and so we use Integer.parseInt() which converts string to integer!!!

CLASS, OBJECTS, CONSTRUCTORS!!!

Again we start the discussion with a program!!!

class rectangle
{
int length,breadth;
void value(int x, int y)
{
length=x;
breadth=y;
}
int area()
{
int abc=length*breadth;
return(abc);
}
}

class rectarea
{
public static void main(String args[])
{
int area1;
int area2;
rectangle rect1=new rectangle();
rectangle rect2=new rectangle();
rect1.length=15;
rect1.breadth=10;
rect2.value(20,12);
area1=rect1.length*rect1.breadth;
area2=rect2.area();
System.out.println("Area1= "+ area1);
System.out.println("Area2= "+ area2);
}
}

Here we have defined 2 classes and have tried to show the how the two are related to one another. We have started with the class rectangle which is handling two data variables length and breadth and also 2 methods( functions ). The first function value is of type void and so as usual does not return any values!!! It simply assigns to the instance variables length and breadth.Note that the first function has 2 parameters. The second function area is of type int and so it returns a value of type int!!! It has no parameters. It computes the area by multiplying length and breadth and then returns this value i.e. the area which is of type integer. Then the next class rectarea starts which is the main class in this program. Firstly we have defined two instance variables area1 and area2 both of which are of integer types!!! Now the real thing starts. We then define two objects rect1 and rect2 of the rectangle class type. We have then described two methods of assigning values to the instance variable of class rectangle. One is by the direct use of the dot operator and the second one is by the direct use of methods of functions!!!

We now start our discussion on constructors and as always we start by writing a program :

class rectangle

{

int length;

int width;

rectangle(int x, int y) // constructor initialized….note that there is no return type!!!

{

length=x;

width=y;

}

int area()

{

int a=length*width;

return(a);

}

}

class rect

{

public static void main(String args[])

{

int AREA;

rectangle A1 = new rectangle(15,10);

AREA=A1.area();

System.out.println("AREA="+AREA);

}

}

The above program makes use of a constructor….from the above program we can see the basic difference from a normal method…..as we had seen in the previous example. The only significant difference is that there is no return type not even void. This is because a constructor returns the instances of the entire class itself!!! In the main function there are differences. We notice that we can assign values to the instance variables directly during the creation of objects itself. It makes the program a lot less complicated!!! We now only need to access one method rather than 2 as we had to do in the previous example!!!





BY AGNIT CHATTERJEE, CSE, GURU NANAK INSTITUTE OF TECHNOLOGY, BATCH:2008-2012




2 comments:

arnab7889 said...

gr8 job buddy
bt u hav nly c in 1st yr.concentrate on it.
arnab
2nd yr ece
heritage

sabarni said...

gr8..
sabarni
1st yr, mca
b.p.p.i.m.t