Problem Statement: To demonstrate the concept of abstract class and method.
Algorithm
Step 1: Start
Step 2: create abstract class special with n static method
Step 3: double process(double p,double r)
Step 4: create class discount extends special
Step 5: create tax which extends special
Step 6: tax t=new tax()
Step 7: discount d=new discount()
Step 8: Read b,c
Step 9: a=d.process(b,c)
Step 10: Read b,c
Step 11: print the value of discount
Step 12: a=t.process(b,c)
Step 13: print the value of tax
Step 14: stop
Function process(double p,double r) of class discount
Step 1: Start
Step 2: net=(p-(p*(r/100)))
Step 3: return net
Step 4: exit
Function process(double p,double r) of class tax
Step 1: Start
Step 2: total=(p+(p*(r/100)))
Step 3: return total
Step 4: Exit.
Program
import java.io.*;
abstract class Special
{
abstract double Process(double p,double r);
}
class Discount extends Special
{
double net;
double Process(double p,double r)
{
net=p-p*(r/100);
return net;
}
}
class Tax extends Special
{
double total;
double Process(double p,double r)
{
total=p+p*(r/100);
return total;
}
}
public class MySubAbs {
public static void main(String args[])
{
double ret,p,r;
DataInputStream in=new DataInputStream(System.in);
Discount d=new Discount();
Tax t=new Tax();
try
{
System.out.println("Enter P and R to compute discount : ");
p=Double.parseDouble(in.readLine());
r=Double.parseDouble(in.readLine());
ret=d.Process(p,r);
System.out.println("Discount : "+ret);
System.out.println("Enter P and R to compute tax : ");
p=Double.parseDouble(in.readLine());
r=Double.parseDouble(in.readLine());
ret=t.Process(p,r);
System.out.println("Tax : "+ret);
}
catch(Exception e)
{
System.out.println("Error : " + e);
}
}
}
Output
Enter P and R to compute discount :
20
30
Discount : 14.0
Enter P and R to compute tax :
50
60
Tax : 80.0
Enter P and R to compute discount :
250
30
Discount : 175.0
Enter P and R to compute tax :
666
30
Tax : 865.8
Abstract class and methods in Java is a post from ShoutToWorld - Let's Learn Let's Shout - Helping bloggers and developers.