Interface assignement
Create a class Figure with two data members’ i.e dim1 and dim2. Create an abstract method that returns area and pass these data members as a parameter.
Create two classes’ rectangle and triangle inherited from Figure.
Take the value of variables from the user in main function.
Create one object of each and display the area of rectangle and triangle.
abstract class Figure{
public static final int dim1 = 0;
public static final int dim2 = 0;
public abstract float area(int a,int b);
}
class Traingle extends Figure{
@Override
public float area(int a, int b) {
return ((a*b)/2); //Assumption
}
}
class Rectangle extends Figure{
@Override
public float area(int a, int b) {
return a*b;
}
}
public class Nasir_FA16_BEE_149 {
public static void main(String args[]) {
int a=4,b=7;
Rectangle c=new Rectangle();
System.out.println("Area of Rectangle "+c.area(a, b));
Rectangle t=new Rectangle();
System.out.println("Area fo Trangle "+t.area(a, b));
}
}
OUTPUT
Comments
Post a Comment