Sunday, 1 November 2015

Abstraction

4. ABSTRACTION

Providing the essential features without its inner details is called Abstraction

                                                                        (or)

Hiding the internal implementation without affecting outside world

Abstraction provides security.

For example a class contains lots of dataand the user doesn’t need the entire data.

Advantage:

Every User will get their own view of the data according to to his reqt and will not get confused with unnecessary data
 
There are two ways to achieve abstraction in java
  1. Abstract class (0 to 100%)
  2. Interface (100%)

Abstract class
 
package javatuts;
import java.io.*;
abstract class perimeter{
    abstract void peri();
}

public   class rectangle extends perimeter{
 public static  int width=0;
public static int length=0;
   static double rectangle(){
    double area=length*width;
    System.out.println("area of rectangle:"+area);
    return area;
}
 @Override
void peri(){
    int perimeter=2*(length+width);
    System.out.println("perimeter:"+perimeter);
}
    public static void main(String[] args) throws IOException,NumberFormatException{

 perimeter pr=new rectangle();
     
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("enter the value of width:\n");
  
         width=Integer.parseInt(br.readLine());
    
            System.out.println("enter the length value:\n");
   
         length=Integer.parseInt(br.readLine());
   double c= rectangle();
   System.out.println("value"+c);
        pr.peri();
}
}
output:
enter the value of width:
10
enter the length value:
18
area of rectangle:180.0
value180.0
perimeter:56
 
2.Interface:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javatuts;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

interface examp{
    void reverse();
public abstract    boolean isArmstrong(int input);
}
abstract class rever implements examp{
    @Override
    public void reverse(){
        try {
            int num,rnum=0,temp=0;
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("enter the number to reverse:\n");
            num=Integer.parseInt(br.readLine());
            while(num>0){
                temp=num%10;
                rnum=rnum*10+temp;
                num=num/10;
            }
            System.out.println("reversed number:\t"+rnum);
        } catch (IOException ex) {
            Logger.getLogger(rever.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
public  class abstractinter extends rever{

   
    public boolean isArmstrong(int input){
     String inputAsString = input + "";
        int numberOfDigits = inputAsString.length();
        int copyOfInput = input;
        int sum = 0;
        while (copyOfInput != 0) {
            int lastDigit = copyOfInput % 10;
            sum = sum + (int) Math.pow(lastDigit, numberOfDigits);
            copyOfInput = copyOfInput / 10;
        }
        if (sum == input) {
            return true;
        } else {
            return false;
        }  
    }
    public static void main(String[] args) {
        examp e=new abstractinter();
        e.reverse();
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int inputNumber = scanner.nextInt();
        abstractinter ai=new abstractinter();
        boolean result = ai.isArmstrong(inputNumber);
        if (result) {
            System.out.println(inputNumber + " is an armstrong number");
        } else {
            System.out.println(inputNumber + " is not an armstrong number");
        }
    }
 }

output:
enter the number to reverse:
2345
reversed number: 5432
Enter a number: 3
3 is an armstrong number
 

No comments:

Post a Comment