Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday, 2 February 2019

FIRST WEEK TERM WORK Program 3

3) Using the concept of interface, write a Java Program that calculates and prints the areas of triangle, square and rectangle.

package Program3;

interface shapes {
abstract void print();

abstract void calc();
}

class triangle implements shapes {
public int a, b, area;

triangle(int p, int q) {
a = p;
b = q;
}

public void calc() {

area = a * b;

}

public void print() {
System.out.println("Triangle area" + area);
}

}

class rectangle implements shapes {
public int a, b, area;

rectangle(int p, int q) {
a = p;
b = q;
}

public void calc() {

area = a * b;

}

public void print() {
System.out.println("Rectangle area" + area);
}

}

class square implements shapes {
public int a, area;

square(int p) {
a = p;

}

public void calc() {

area = a * a;

}

public void print() {
System.out.println("Square area" + area);
}

}

public class ShapesInterface {
public static void main(String args[]) {
shapes obj = new triangle(5, 10);
obj.calc();
obj.print();
obj = new rectangle(5, 10);
obj.calc();
obj.print();
obj = new square(5);
obj.calc();
obj.print();
}

}

0 comments:

Post a Comment