Write A Program To Demonstrate Use of Implementing Interfaces
Write A Program To Demonstrate Use of Implementing Interfaces
INTERFACES.
import java.lang.*;
interface Area
{
final static float pi=3.14F;
float compute(float x,float y);
}
class rectangle implements Area
{
public float compute(float x,float y)
{
return(pi*x*y);
}
}
class circle implements Area
{
public float compute(float x,float y)
{
return(pi*x*x);
}
}
class interfacedemo
{
public static void main(String a[])
{
rectangle rect=new rectangle();
circle cir=new circle();
Area A;
A=rect;
System.out.println("Area of rectangle="+A.compute(10,20));
A=cir;
System.out.println("Area of circle="+A.compute(30,0));
}
}
Program (Shape.java)
/**
* The Shape class is an abstract class that holds
* general data about a shape.
*/
//Get height
public double getHeight()
{
return height;
}
//Get width
public double getWidth()
{
return width;
}
/**
* This program demonstrates polymorphic behavior.
*/
Animal.java:
Zoo.java:
import java.util.ArrayList;
To create the above interface program follow the instructions as shown below
compile Department.java
compile Hostel.java,StudentMaster.java
class Hostel
int noofroom;
void getHostelName()
void getHostelLocation()
void getNoOfRoom()
import java.util.*;
class Student extends Hostel implements Department
String sname,regno,elesub;
String deptName,deptHead;
int avgMarks;
void getStudentName()
String getStudentRegNo()
return regno;
void getElectiveSubject()
void getAvgMarks()
{
System.out.println("Department Head : " + deptHead);
void addStudent()
sname=sc.nextLine();
regno=sc.nextLine();
elesub=sc.nextLine();
hname=sc.nextLine();
hlocation=sc.nextLine();
deptName=sc.nextLine();
deptHead=sc.nextLine();
noofroom=sc.nextInt();
avgMarks=sc.nextInt();
void migrate()
deptName=sc.nextLine();
deptHead=sc.nextLine();
void display()
getStudentName();
getElectiveSubject();
getAvgMarks();
getDetpName();
getDetpHead();
import java.util.*;
class StudentMaster
int sno=0;
String rno;
int ch;
boolean b;
while(true)
System.out.println(" 3. Display");
System.out.println(" 4. Exit");
ch=sc.nextInt();
switch(ch)
case 1:
st[sno]=new Student();
st[sno++].addStudent();
break;
case 2:
rno=sc.next();
b=false;
for(int i=0;i<sno;i++)
if(st[i].getStudentRegNo().equals(rno))
b=true;
st[0].migrate();
break;
}
if(b==false)
break;
case 3:
rno=sc.next();
b=false;
for(int i=0;i<sno;i++)
if(st[i].getStudentRegNo().equals(rno))
b=true;
st[0].display();
break;
if(b==false)
break;
case 4:
System.exit(0);
default:
System.out.println("--Invalid Entry--");
}
C:\myfile\interface>cd\
C:\>cd myfile
C:\myfile>cd interface2
C:\myfile\interface2>javac Department.java
C:\myfile\interface2>javac Hostel.java
C:\myfile\interface2>javac Student.java
C:\myfile\interface2>javac StudentMaster.java
C:\myfile\interface2>java StudentMaster
1. Admit a student
2. Migrate a student
3. Display
4. Exit
5. Enter Your Choice
1
Enter Student name : ashwini
Enter Registration Number : 444
Enter Elective Subject : graphics
Enter Hostel Name : rose
Enter Hostel Location : chennai
Enter Department Name : mca
Enter Department Head : sundar
Enter No of room : 78
Enter Avg Marks : 90
1. Admit a student
2. Migrate a student
3. Display
4. Exit
5. Enter Your Choice
2
Enter Registration no :
444
Enter new Department Name : bca
Enter new Department Head : vel
1. Admit a student
2. Migrate a student
3. Display
4. Exit
5. Enter Your Choice
3
Enter Registration no :
444
Student : ashwini
Student Registration No is : 444
Elective Subject : graphics
Average Marks : 90
Department Name : bca
Department Head : vel
1. Admit a student
2. Migrate a student
3. Display
4. Exit
5. Enter Your Choice
4
C:\myfile\interface2>