This assignment demonstrates the use of interfaces and how they can be used in c
ID: 3558704 • Letter: T
Question
This assignment demonstrates the use of interfaces and how they can be used in combination with inheritance.
Use the four shapes that you implemented in Assignment Inheritance as a starting point.
I recommend creating a copy before you implement any changes.
In this assignment you will create 2 interfaces: Shape and Printable and you will modify the classes Rectangle, Square,
IsoscelesRightTriangle, and Circle so that they implement one or both of the interfaces.
In addition you will create a class called InterfaceApp (different from InheritanceApp). This class includes the main
method and demonstrates the polymorphic behavior of interfaces.
Declare the interfaces according to the UML diagrams:
ad interface Shape:
perimeter .. returns the perimeter of the shape
area .. returns the area of the shape
ad interface Printable:
print
Explanation / Answer
interfaceApp.java
import java.lang.*;
interface Shape{
double perimeter();
double area();
}
interface Printable{
void print();
}
class Rectangle implements Shape, Printable
{
int len,bre,peri,are;
Rectangle(int l,int b){
len = l;
bre = b;
}
public double perimeter()
{
return len * bre;
}
public double area(){
return len * bre;
}
public void print()
{
for(int i=0;i<bre;i++){
if(i==0 || i==bre-1){
for(int j=0;j<len;j++){
System.out.print("* ");
}
}
else{
System.out.print("*");
for(int k=0;k<len-1;k++)System.out.print(" ");
System.out.print("*");
}
System.out.print(" ");
}
}
public String toString(){
return "Rectangle(6X3)";
}
}
class Square implements Shape, Printable
{
int len,peri,are;
public Square(int l){
len = l;
}
public double perimeter()
{
return len * len;
}
public double area(){
return len * len;
}
public void print()
{
for(int i=0;i<len;i++){
if(i==0 || i==len-1){
for(int j=0;j<len;j++){
System.out.print("* ");
}
}
else{
System.out.print("*");
for(int k=0;k<len-1;k++)System.out.print(" ");
System.out.print("*");
}
System.out.print(" ");
}
}
public String toString(){
return "Square(5)";
}
}
class IsoscelesRightTriangle implements Shape, Printable
{
int h;
double peri,are;
public IsoscelesRightTriangle(int he){
h= he;
}
public double perimeter(){
return h + h + Math.sqrt((6*6)+(6*6));
}
public double area()
{
return (double)(h*h)/2;
}
public void print(){
for(int i=0;i<h;i++){
for(int j=0;j<i;j++){
System.out.print("* ");
}
System.out.print(" ");
}
}
public String toString(){
return "Triangle(6)";
}
}
class Circle implements Shape
{
int r,dia;
double peri,are;
public Circle(int rad){
r = rad;
}
public double perimeter(){
return 2 * 3.14 * r;
}
public double area(){
return 3.14 * r * r;
}
public double diameter(){
return r * 2;
}
public String toString(){
return "Circle(5)";
}
}
public class InterfaceApp {
public static void main(String[] args) {
Shape []sh = new Shape[3];
Printable []p = new Printable[3];
Rectangle rect = new Rectangle(6, 3);
Square squ = new Square(5);
IsoscelesRightTriangle is = new IsoscelesRightTriangle(6);
Circle cir = new Circle(5);
System.out.println("Diameter:"+cir.diameter()+" Perimeter:"+cir.perimeter()+" Area:"+cir.area());
System.out.println(cir);
System.out.print("Shape Array: -------- ");
sh[0] = squ;
sh[1] = rect;
sh[2] = is;
p[0] = squ;
p[1] = rect;
p[2] = is;
for(int i=0;i<sh.length;i++){
System.out.println(sh[i]+" Perimeter:"+sh[i].perimeter()+" Area:"+sh[i].area());
if(p[i] instanceof Printable){
p[i].print();
}
}
}
}
output:
Diameter:10.0
Perimeter:31.400000000000002
Area:78.5
Circle(5)
Shape Array:
--------
Square(5)
Perimeter:25.0
Area:25.0
* * * * *
* *
* *
* *
* * * * *
Rectangle(6X3)
Perimeter:18.0
Area:18.0
* * * * * *
* *
* * * * * *
Triangle(6)
Perimeter:20.485281374238568
Area:18.0
*
* *
* * *
* * * *
* * * * *
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.