Program Based On Interface and Abstract Class: ///19BDS0083 VASANTH KUMAR
Program Based On Interface and Abstract Class: ///19BDS0083 VASANTH KUMAR
1. Write an interface called Exam with a method Pass ( ) that returns the total marks.
Write another interface called Classify with a method Average (int total) which
returns a string. Write a Class called Result which implements both Exam and
Classify. The Pass method should get the marks from the user and finds the total
marks and return. The Division method calculate the average marks and return
“First” if the average is 60 or more, “SECOND” when average is 50 or more but
below 60, “NO DIVISION” when average is less than 50
CODE:
///19BDS0083 VASANTH KUMAR
import java.io.*;
import java.util.Scanner;
interface Exam
{
boolean Pass(int mark);
}
interface Classify
{
String Division(int avg);
}
class Result implements Exam,Classify
{
public boolean Pass(int mark)
{
if(mark>=50)
return true;
else
return false;
}
public String Division(int avg)
{
if(avg>=60)
return "First";
else if(avg>=50)
return "Second";
else
return "No-Division";
}
}
public class Int {
public static void main(String[] args) {
boolean pass;
int mark,avg;
String division;
Scanner in=new Scanner(System.in);
Result res=new Result();
for(int i=0;i<=2;i++)
{
System.out.println("Enter the mark : ");
mark=Integer.parseInt(in.nextLine());
System.out.println("Enter the average : ");
avg=Integer.parseInt(in.nextLine());
pass=res.Pass(mark);
division=res.Division(avg);
if(pass)
System.out.println("Passed - "+ division + ".");
else
System.out.println("Failed - " + division+ ".");
}
}
}
OUTPUT:
2. Write an abstract class special with an abstract method double Process (double
P,double R). Create a subclass Discount and implement the Process() method
with the following formula: net=P-P*R/100. Return the Process() method with
the following formula: total=P+P*R/100. Return the total.
CODE:
import java.util.*;
import java.util.Scanner;
//19BDS0083 D VASANTH KUMAR
abstract class Net
{
double p;double r;
abstract void Process(double P,double R);
}
class Discount extends Net
{
public void Process(double x,double y)
{
p=x;r=y;
}
public double getdata()
{
double net=p-p*r/100;
return net;
}
}
class Tax extends Discount
{
public double getdata()
{
double total=p+p*r/100;
return total;
}
}
public class abstract2 {
public static void main(String[] args)
{
Discount ob1=new Discount();
Tax ob2=new Tax();
System.out.println("\nEnter the values of P and R:");
Scanner a=new Scanner(System.in);
double u=a.nextDouble();
Scanner b=new Scanner(System.in);
double v=b.nextDouble();
ob1.Process(u, v);
ob2.Process(u,v);
System.out.println("\nThe net and Total is:"+ob1.getdata()+" and "+ob2.getdata());
}
}
OUTPUT: