prowessapps.in Abstract and Interface's Programs

 WAP to calculate area of circle, rectangle,square using Abstract class Shape .

import java.util.Scanner;
abstract class Shape {
 final double PI=3.14;
 double ar;
 Scanner s=new Scanner(System.in);
 public abstract void area();
 public void display() {
  System.out.println("Area = "+ar);
 }
}
class Circle extends Shape {
 public void area() {
  System.out.print("Enter R: ");
  int r = s.nextInt();
  ar=PI*r*r;
  display();
 }
}

class Rectangle extends Shape {
 public void area() {
  System.out.print("Enter Len: ");
  int l = s.nextInt();
  System.out.print("Enter Breadth: ");
  int b = s.nextInt();
  ar = l*b;
  display();
 }
}
class Main{
 public static void main(String[] a)
 {
  Shape s = new Circle();
  s.area();
  Shape s2 = new Rectangle();
  s2.area();
 }
}
}
OUTPUT :
Enter R: 7
Area = 153.86
Enter Len: 10
Enter Breadth: 5
Area = 50.0

 WAP to calculate nCr and 1!+2!+3!+4! using abstract class.
FORMULA: nCr = n!/(r!*(n-r)!).

import java.util.Scanner;
abstract class Factorial {
 Scanner s=new Scanner(System.in);
 abstract void calculate();
 int fact(int x) {
  int res = 1;
  for(int i=1; i<= x; i++) {
   res = res * i;
  }
 return res;
 }
}
class PatternA extends Factorial{
 public void calculate() {
  System.out.print("Enter N: ");
  int n = s.nextInt();
  System.out.print("Enter R: ");
  int r = s.nextInt();
  int res=fact(n)/(fact(n-r)*fact(r));
  System.out.println("Result- "+res);
 }
}
class PatternB extends Factorial {
 public void calculate() {
  System.out.print("Enter N: ");
  int n = s.nextInt();
  int sum =0;
  for(int i=1; i<=n; i++) {
   sum = sum+fact(i);
  }
  System.out.print("Result- "+sum);
 }
}
class Main{
 public static void main(String[] a)
 {
  PatternA p = new PatternA();
  p.calculate();
  PatternB p2 = new PatternB();
  p2.calculate();
 }
}
OUTPUT :
Enter N: 5
Enter R: 2
Result- 10
Enter N: 6
Result- 873

 WAP to demonstrate the use of Interface.

interface Shape {
 void draw();
}
class Rectangle implements Shape {
 public void draw() {
  System.out.println("Draw Rectangle");
 }
}
class Circle implements Shape {
 public void draw() {
  System.out.println("Draw Circle ");
 }
}
class Test {
 public static void main(String[] a)
 {
 Shape s = new Rectangle();
 s.draw(); 
 Shape s2 = new Circle();
 s2.draw();
 }
} 
OUTPUT :
Draw Rectangle
Draw Circle

 WAP to get Rate of interest of banks using interface Bank.

interface Bank
{
 public float roi();
}
class HDFC implements Bank {
 public float roi() {
  return 4.5f;
 }
}
class PNB implements Bank {
 public float roi() {
  return 4.9f;
 }
}
class SBI implements Bank {
 public float roi() {
  return 3.5f;
 }
}
class Main {
 public static void main(String[] a)
 {
  Bank b = new HDFC();
  System.out.println("HDFC- "+b.roi());
  Bank b2 = new PNB();
  System.out.println("PNB- "+b2.roi());
  Bank b3 = new SBI();
  System.out.println("SBI- "+b3.roi());
 }
}  
OUTPUT :
HDFC- 4.5
PNB- 4.9                         
SBI- 3.5


 Create an Interface with name "Shape", WAP to calculate the area of circle and cylinder.

import java.util.*;
interface Shape {
 double PI= 3.14;
 Scanner s=new Scanner(System.in);
 public void area();
}
class Circle implements Shape {
 public void area() {
 // s = new Scanner(System.in);
  System.out.print("Enter Radius: ");
  double r = s.nextDouble();
  double ar = PI*r*r;
  System.out.println("Circle Area- "+ar);
 }
}
class Cylinder implements Shape {
 public void area() {
  System.out.print("Enter Radius: ");
  double r = s.nextDouble();
  System.out.print("Enter Height: ");
  double h = s.nextDouble();
  double ar =2*PI*r*h;
  System.out.print("Cylinder Area- "+ar);
 }
}
class Test{
 public static void main(String[] a) 
 {
  Shape s = new Circle();
  s.area();
  Shape s2 = new Cylinder();
  s2.area();
 }
}
OUTPUT :
Enter Radius: 7
Circle Area- 153.86
Enter Radius: 5
Enter Height: 7
Cylinder Area- 219.8 

  WAP to demonstrate multiple inheritance using interfaces.

import java.util.Scanner;
interface SubMarks {
 public void getMarks();
}
interface Sports {
 public void sportMarks();
}
class Result implements SubMarks,Sports {
 int marks,sport,total;
 Scanner s = new Scanner(System.in);
 public void getMarks() {
  System.out.print("Subject Marks: ");
  marks = s.nextInt();
 }
 public void sportMarks() {
  System.out.print("Sport Marks: ");
  sport = s.nextInt();
 }
 public void display() {
  total = marks+sport;
  System.out.println("Total - "+total);
 }
}
class Main {
 public static void main(String[] a)
 {
  Result r =new Result();
  r.getMarks();
  r.sportMarks();
  r.display();
 }
}
OUTPUT :
Subject Marks: 90
Sport Marks: 80
Total - 170 

  WAP to demonstrate inheritance of interfaces.

import java.util.Scanner;
interface SubMarks {
 public void getMarks();
}
interface Sports extends SubMarks {
 public void sportMarks();
}
class Result implements Sports {
 int marks,sport,total;
 Scanner s = new Scanner(System.in);
 public void getMarks() {
  System.out.print("Subject Marks: ");
  marks = s.nextInt();
 }
 public void sportMarks() {
  System.out.print("Sport Marks: ");
  sport = s.nextInt();
 }
 public void display() {
  total = marks+sport;
  System.out.println("Total - "+total);
 }
}
class Main {
 public static void main(String[] a)
 {
  Result r =new Result();
  r.getMarks();
  r.sportMarks();
  r.display();
 }
}
OUTPUT :
Subject Marks: 90
Sport Marks: 80
Total - 170 

  WAP to demonstrate inheritance of interfaces.

interface Feature {
 public void getSQRT();
 default void square(int a) {
  System.out.println("Square- "+a*a);
 }
 static void cube(int a) {
  System.out.println("Cube - "+a*a*a);
 }
}
class Calc implements Feature{
 int a = 20;
 public void getSQRT() {
  double x = Math.sqrt(a);
  System.out.println("SQRT- "+x);
 }
}
class Main {
 public static void main(String[] a)
 {
 Calc c = new Calc();
 c.getSQRT();
 c.square(10);
 Feature.cube(10);
 }
}
OUTPUT :
SQRT- 4.47213595499958
Square- 100
Cube - 1000 




CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved