Calculate the area by using an interface in JAVA by Using Child Class Object OR Using Interface Object
1.Calculate the area by using an interface in JAVA by Using Child Class Object.
Implements more than one interface classes & create child class object
CODING:Implements more than one interface classes & create child class object
import java.util.Scanner;
interface square{void area_s(double side);}
interface rectangle{void area_r(double width,double height);}
interface triangle{void area_t(double base,double height);}
interface circle{void area_c(double radius);}
class CalArea implements square,rectangle,triangle,circle
{
public static void main(String [] args){
Scanner op=new Scanner(System.in);
char ch;
CalArea A1=new CalArea();
do
{
System.out.print("\n1. SQUARE\n2. RECTANGLE\n3. TRIANGLE\n4. CIRCLE\n5. EXIT\nEnter Number of Area: ");
ch=op.next().charAt(0);
switch(ch)
{
case '1':
A1.area_s(5);
break;
case '2':
A1.area_r(5,6);
break;
case '3':
A1.area_t(5,6);
break;
case '4':
A1.area_c(5);
break;
case '5':
System.out.print("\tExit...");
break;
default:
System.out.print("\tEnter Valid Number");
}
}while(ch!='5');
}
public void area_s(double side){
System.out.print("\tArea of Square: "+ side*side);
}
public void area_r(double width,double height){
System.out.print("\tArea of Rectangle: "+ width*height);
}
public void area_t(double base,double height){
System.out.print("\tArea of triangle:"+ (base*height)/2);
}
public void area_c(double radius){
System.out.print("\tArea of circle: "+ 3.14*(radius*radius));
}
}
interface square{void area_s(double side);}
interface rectangle{void area_r(double width,double height);}
interface triangle{void area_t(double base,double height);}
interface circle{void area_c(double radius);}
class CalArea implements square,rectangle,triangle,circle
{
public static void main(String [] args){
Scanner op=new Scanner(System.in);
char ch;
CalArea A1=new CalArea();
do
{
System.out.print("\n1. SQUARE\n2. RECTANGLE\n3. TRIANGLE\n4. CIRCLE\n5. EXIT\nEnter Number of Area: ");
ch=op.next().charAt(0);
switch(ch)
{
case '1':
A1.area_s(5);
break;
case '2':
A1.area_r(5,6);
break;
case '3':
A1.area_t(5,6);
break;
case '4':
A1.area_c(5);
break;
case '5':
System.out.print("\tExit...");
break;
default:
System.out.print("\tEnter Valid Number");
}
}while(ch!='5');
}
public void area_s(double side){
System.out.print("\tArea of Square: "+ side*side);
}
public void area_r(double width,double height){
System.out.print("\tArea of Rectangle: "+ width*height);
}
public void area_t(double base,double height){
System.out.print("\tArea of triangle:"+ (base*height)/2);
}
public void area_c(double radius){
System.out.print("\tArea of circle: "+ 3.14*(radius*radius));
}
}
2.Calculate the area by using an interface in JAVA by Using Interface Object.
In java we can not make interface object, but we can make reference variable of interface class,this reference variable referenced to child class object.
CODING:In java we can not make interface object, but we can make reference variable of interface class,this reference variable referenced to child class object.
import java.util.Scanner;
interface square{void area(int side);}
interface rectangle extends square{void area(int width,double height);}
interface triangle extends rectangle{void area(double base,double height);}
interface circle extends triangle{void area(double radius);}
class CA implements circle
{
public static void main(String [] args){
Scanner op=new Scanner(System.in);
char ch;
circle A1=new CA();
do
{
System.out.print("\n1. SQUARE\n2. RECTANGLE\n3. TRIANGLE\n4. CIRCLE\n5. EXIT\nEnter Number of Area: ");
ch=op.next().charAt(0);
switch(ch)
{
case '1':
A1.area(5);
break;
case '2':
A1.area(5,6D);
break;
case '3':
A1.area(5D,6D);
break;
case '4':
A1.area(5D);
break;
case '5':
System.out.print("\tExit...");
break;
default:
System.out.print("\tEnter Valid Number");
}
}while(ch!='5');
}
public void area(int side){
System.out.print("\tArea of Square: "+ side*side);
}
public void area(int width,double height){
System.out.print("\tArea of Rectangle: "+ width*height);
}
public void area(double base,double height){
System.out.print("\tArea of triangle:"+ (base*height)/2);
}
public void area(double radius){
System.out.print("\tArea of circle: "+ 3.14*(radius*radius));
}
}
interface square{void area(int side);}
interface rectangle extends square{void area(int width,double height);}
interface triangle extends rectangle{void area(double base,double height);}
interface circle extends triangle{void area(double radius);}
class CA implements circle
{
public static void main(String [] args){
Scanner op=new Scanner(System.in);
char ch;
circle A1=new CA();
do
{
System.out.print("\n1. SQUARE\n2. RECTANGLE\n3. TRIANGLE\n4. CIRCLE\n5. EXIT\nEnter Number of Area: ");
ch=op.next().charAt(0);
switch(ch)
{
case '1':
A1.area(5);
break;
case '2':
A1.area(5,6D);
break;
case '3':
A1.area(5D,6D);
break;
case '4':
A1.area(5D);
break;
case '5':
System.out.print("\tExit...");
break;
default:
System.out.print("\tEnter Valid Number");
}
}while(ch!='5');
}
public void area(int side){
System.out.print("\tArea of Square: "+ side*side);
}
public void area(int width,double height){
System.out.print("\tArea of Rectangle: "+ width*height);
}
public void area(double base,double height){
System.out.print("\tArea of triangle:"+ (base*height)/2);
}
public void area(double radius){
System.out.print("\tArea of circle: "+ 3.14*(radius*radius));
}
}
Comments
Post a Comment