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];