Get Adobe Flash player

Thursday, January 19, 2012

Final Class & Final Method

Final Class:

A subclass is inherited from a super class. But in some situation if we want to prevent the inheritance of a class then we use final class.

For example

final class abc{
void printSqure(int x){
System.out.println(x*x);
}
}

class FinalExample{
public static void main(String args[]){
abc obj= new abc();
abc.printSqure(5);
}
}

This program will output 25 If we try to inherit a subclass from abc then the compiler give us an error.

Final Method:



 



Methods in a class can be protected from overriding methods in the subclasses by declaring the method as final.



For example



class abc{
final void printSqure(int x){
System.out.println(x*x);
}
}

class FinalExample{
public static void main(String args[]){
abc obj= new abc();
abc.printSqure(5);
}
}

If we try to override the printSqure() method in any subclass of abc then we will get an error.

Method Overriding

When subclasses are created, they may have their own methods in addition to the inherited methods from superclass. It is possible for methods in subclass to have the same name and type as those of the superclass. In that case the method of subclass is override the method of superclass. override the method in the super class..

For example

class abc{
void msg(){
System.out.println("Now in class abc");
}
}

class xyz extends abc{
void msg(){
System.out.println("Now in class xyz");
}
}

class OverRide{
public static void main(String args[]){
xyz obj1= new xyz();
abc obj2= new abc();

obj1.msg();
obj2.msg();
}
}

This program will output Now in class xyz Now in class abc So here the msg() method of class xyz override the method of its superclass abc.....


Sunday, January 15, 2012

Armstrong number or not in Java

import java.io.*;

class Armstrong{
public static void main(String args[]){
try{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number");
int num= Integer.parseInt(object.readLine());
int n = num;
int check=0,remainder;
while(num > 0){
remainder = num % 10;
check = check + (int)Math.pow(remainder,3);
num = num / 10;
}
if(check == n)
System.out.println(n+" is an Armstrong Number");
else
System.out.println(n+" is not a Armstrong Number");
}
catch(Exception e){
System.out.println("Out of range!");
}
}
}


Example :



Input – 153

Output - 153 is Armstrong Number

Saturday, January 14, 2012

String Palindrome or not

import java.io.*;

public class Palindrome {
public static void main(String [] args){
try{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter string");
String s=object.readline();
int i;
int n=s.length();
String str="";
for(i=n-1;i>=0;i--)
str=str+s.charAt(i);
if(str.equals(s))
System.out.println(s+ " is palindrome");
else
System.out.println(s+ " is not a palindrome");
}
catch(Exception e){
System.out.println("Input Error!");
}
}
}


Output:



Enter string abbsd

abbsd is not palindrome



Enter string abcdedcba

abcdedcba is palindrome

Integer Palindrome or not

import java.io.*;

public class Palindrome {
public static void main(String [] args){
try{
BufferedReader object = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter number");
int num= Integer.parseInt(object.readLine());
int n = num;
int rev=0;
for (int i=0; i<=num; i++){
int r=num%10;
num=num/10;
rev=rev*10+r;
i=0;
}
if(n == rev){
System.out.print("Number is palindrome!");
}
else{
System.out.println("Number is not palindrome!");
}
}
catch(Exception e){
System.out.println("Out of range!");
}
}
}


Output:



Enter number 1234

Number is not palindrome!



Enter number 1234321

Number is palindrome!

String reverse

class StringRever{
public static void main(String args[]){
String str="Hello World";
for(int i=str.length()-1;i>=0;i--)
System.out.print(str.charAt(i));
}
}


Output:



dlroW olleH

Multi-Dimension Array in Java

Java supports multi dimensional array also. A 4x4 matrix can be represent by using two dimensional array. General syntax for declaring two dimensional array is fallow.

int tdarr[][];
tdarr= new int[4][4];

Java Program implementing 1D Array

class ArrayProg{
public static void main(String args[]){
int marks[];
marks=new int[5];

int total;
int average;
total=0;
marks[0]=45;
marks[1]=36;
marks[2]=86;
marks[3]=67;
marks[4]=56;

for(i=0;i<5;i++){
total+=marks[i];
}
average=total/5;

System.out.println("Average is : "+average);
}
}


This program will output the following result:



Average is : 58

Arrays in Java

An array is a variable representing a collection of same-typed data. Arrays are usefull in representing vectors, matrix, string analysis, sorting a tables.

 

One-Dimensional Array:

In java, an array is created in two steps. First, an array variable is to be declared and the second is to create a array object using new operator by specifying the number of memory location required. These two operations are done using two different statements.

int arr[];
arr=new int[10];


The first statement int arr[]; declare a Integer type variable arr. The second statement allocates memory for 10 int type. Now arr can handle 10 int type values. Each value can be accessed by using index value 0 to 9. Supposing 10 elements are 12,32,43,13,25,34,76,45,23,34 then.



arr[0] ---> 12

arr[1] ---> 32


arr[6] ---> 76


and so on……

Operator Precedence

An expression may contain a number of operators, variable and literals. A programmer must know how these operators are evaluated. Like other languages, each operator has it own priority. Operators are evaluated on the priorities allocated to them.  In most of cases, an expression is evaluated from left to right of the expression. Unary and assignment operators take right to left association.

operatorprecedence

Ternary Operator

The ternary operator ?, operates on three operands. This operator can replace simple if-else statements. The general form of using this operator os:

expression1 ? expression2 : expression 3 

The expression1 always should result in a Boolean value TRUE or FALSE. If the value is TRUE then expression2 will executed otherwise expression3 will executed.

For example:

class TernaryOpe{
public static void main(String args[]){
int mark=77;
String result;
result=(mark>40)? “Pass” : “Fail”;
System.out.println(“Your mark = ”+mark);
System.out.println(“And your result is : ”+result);




}
}


The above program gives the following output:



Your mark = 77

And your result is : Pass

Operators in Java

Operators operate on operands and cause changes in the operand value or give a new value. Java provides operators in four catagories.

  1. Arithmatic Operator
  2. Bitwise Operator
  3. Relational Operator
  4. Logical Operator

 

Arithmetic Operators:

 

Operation Operator Symbol
Add +
Subtract -
Multiply *
Divide /
Modulus %
 
Bitwise Operators:
 
Bitwise operators are used to manipulate individual bits of a data item. there are situations where individual bits of a data are to be modified. These operators operates only on byte, char, short, int and long types.
 
Operation Operator Symbol
Bitwise NOT ~
Bitwise AND &
Bitwise OR |
Bitwise XOR ^
Left Shift <<
Right Shift >>
Right Shift Zero Fill >>>

 

Relational Operators:

Operation Operator Symbol
Equal to ==
Not Equal to !=
Greater Than >
Greater than or equal to >=
Less than <
Less than or equal to <=

Logical Operators:

Operation Operator Symbol
Equal to ==
Logical AND &
Logical OR |
Logical XOR ^
Short-circuit AND &&
Short-circuit OR ||
Logical unary NOT !
Not equal to !=
Ternary if-else ?: