CS25C09 – JAVA PROGRAMMING LABORATORY
EX. NO. : 04 INTERFACE
AIM :
The aim is to develop algorithms and implement Java programs using interfaces, abstraction and predefined
interfaces.
ALGORITHM :
1. Start the program.
2. Declare the required interface, abstract class and variables.
3. Implement the required methods in the respective classes.
4. Create objects and invoke the implemented methods.
5. Perform the required operations.
6. Display the output.
7. Stop the program.
PROGRAMS :
1. Calculate area of rectangle and circle using interfaces.
import [Link];
interface Area {
void calculateArea();
}
class Rectangle implements Area {
double length, breadth;
Rectangle(double length, double breadth) {
[Link] = length;
[Link] = breadth;
}
public void calculateArea() {
[Link]("Area of Rectangle = " + (length * breadth));
}
}
class Circle implements Area {
double radius;
Circle(double radius) {
[Link] = radius;
}
public void calculateArea() {
[Link]("Area of Circle = " + ([Link] * radius * radius));
}
}
class InterfaceArea {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter length: ");
double length = [Link]();
[Link]("Enter breadth: ");
double breadth = [Link]();
[Link]("Enter radius: ");
double radius = [Link]();
Area r = new Rectangle(length, breadth);
Area c = new Circle(radius);
[Link]();
[Link]();
}
}
OUTPUT :
[Link] 25CSED51
CS25C09 – JAVA PROGRAMMING LABORATORY
Enter length: 10
Enter breadth: 5
Enter radius: 7
Area of Rectangle = 50.0
Area of Circle = 153.93804002589985
[Link] 25CSED51
CS25C09 – JAVA PROGRAMMING LABORATORY
2. Design a Java interface for ADT Stack. Implement this interface using array.
import [Link];
interface Stack {
void push(int item);
int pop();
void display();
}
class StackArray implements Stack {
int stack[] = new int[5];
int top = -1;
public void push(int item) {
if (top == 4)
[Link]("Stack Overflow");
else
stack[++top] = item;
}
public int pop() {
if (top == -1) {
[Link]("Stack Underflow");
return -1;
}
return stack[top--];
}
public void display() {
if (top == -1) {
[Link]("Stack is empty");
return;
}
[Link]("Stack elements: ");
for (int i = top; i >= 0; i--)
[Link](stack[i] + " ");
[Link]();
}
}
class StackInterface {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Stack s = new StackArray();
[Link]("Enter number of elements: ");
int n = [Link]();
[Link]("Enter elements:");
for (int i = 0; i < n; i++)
[Link]([Link]());
[Link]();
[Link]("Popped element = " + [Link]());
[Link]();
}
}
OUTPUT :
Enter number of elements: 4
Enter elements:
10
20
30
40
Stack elements: 40 30 20 10
Popped element = 40
Stack elements: 30 20 10
[Link] 25CSED51
CS25C09 – JAVA PROGRAMMING LABORATORY
3. Write a Java Program to create an abstract class named Shape that contains two integers and an empty method
named printArea(). Provide three classes named Rectangle, Triangle and Circle such that each one of the classes
extends the class Shape. Each one of the classes contains only the method printArea() that prints the area of the
given shape.
abstract class Shape {
double a, b;
Shape(double a, double b) {
this.a = a;
this.b = b;
}
abstract void printArea();
}
class Rectangle extends Shape {
Rectangle(double length, double breadth) {
super(length, breadth);
}
void printArea() {
[Link]("Area of Rectangle = " + (a * b));
}
}
class Triangle extends Shape {
Triangle(double base, double height) {
super(base, height);
}
void printArea() {
[Link]("Area of Triangle = " + (0.5 * a * b));
}
}
class Circle extends Shape {
Circle(double radius) {
super(radius, 0);
}
void printArea() {
[Link]("Area of Circle = " + ([Link] * a * a));
}
}
class ShapeArea {
public static void main(String[] args) {
Shape r = new Rectangle(10, 5);
Shape t = new Triangle(10, 6);
Shape c = new Circle(7);
[Link]();
[Link]();
[Link]();
}
}
OUTPUT :
Area of Rectangle = 50.0
Area of Triangle = 30.0
Area of Circle = 153.93804002589985
[Link] 25CSED51
CS25C09 – JAVA PROGRAMMING LABORATORY
4. Write a Java program using pre defined interface Comparable. Create a class called Person which implements
Comparable. Declare data members name and age in a constructor. Define the member functions getName(),
getAge() and compareTo().
import [Link];
class Person implements Comparable<Person> {
String name;
int age;
Person(String name, int age) {
[Link] = name;
[Link] = age;
}
String getName() {
return name;
}
int getAge() {
return age;
}
public int compareTo(Person p) {
return [Link] - [Link];
}
}
class PersonCompare {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first person name: ");
String name1 = [Link]();
[Link]("Enter first person age: ");
int age1 = [Link]();
[Link]();
[Link]("Enter second person name: ");
String name2 = [Link]();
[Link]("Enter second person age: ");
int age2 = [Link]();
Person p1 = new Person(name1, age1);
Person p2 = new Person(name2, age2);
if ([Link](p2) == 0)
[Link]("Both persons have the same age.");
else if ([Link](p2) > 0)
[Link]([Link]() + " is older.");
else
[Link]([Link]() + " is older.");
}
}
OUTPUT :
Enter first person name: Arun
Enter first person age: 21
Enter second person name: Kiran
Enter second person age: 19
Arun is older.
[Link] 25CSED51
CS25C09 – JAVA PROGRAMMING LABORATORY
5. Write a Java Program to create an abstract class named Shape and provide three classes named Square,
Triangle and Circle such that each one of the classes extends the class Shape.
abstract class Shape {
abstract void printArea();
}
class Square extends Shape {
double side;
Square(double side) {
[Link] = side;
}
void printArea() {
[Link]("Area of Square = " + (side * side));
}
}
class Triangle extends Shape {
double base, height;
Triangle(double base, double height) {
[Link] = base;
[Link] = height;
}
void printArea() {
[Link]("Area of Triangle = " + (0.5 * base * height));
}
}
class Circle extends Shape {
double radius;
Circle(double radius) {
[Link] = radius;
}
void printArea() {
[Link]("Area of Circle = " + ([Link] * radius * radius));
}
}
class ShapeExample {
public static void main(String[] args) {
Shape s = new Square(5);
Shape t = new Triangle(10, 6);
Shape c = new Circle(7);
[Link]();
[Link]();
[Link]();
}
}
OUTPUT :
Area of Square = 25.0
Area of Triangle = 30.0
Area of Circle = 153.93804002589985
[Link] 25CSED51
CS25C09 – JAVA PROGRAMMING LABORATORY
OPEN ENDED PROBLEM STATEMENT
1. Develop a Java application for an online payment system using interfaces.
Requirements :
• Create an interface Payment.
• Implement classes CreditCardPayment, UPIPayment and NetBanking.
• Each payment method should implement pay() and refund().
• Accept payment amount and display payment status.
PROGRAM :
import [Link];
interface Payment {
void pay(double amount);
void refund(double amount);
}
class CreditCardPayment implements Payment {
public void pay(double amount) {
[Link]("Credit Card Payment Successful: Rs." + amount);
}
public void refund(double amount) {
[Link]("Credit Card Refund Successful: Rs." + amount);
}
}
class UPIPayment implements Payment {
public void pay(double amount) {
[Link]("UPI Payment Successful: Rs." + amount);
}
public void refund(double amount) {
[Link]("UPI Refund Successful: Rs." + amount);
}
}
class NetBanking implements Payment {
public void pay(double amount) {
[Link]("Net Banking Payment Successful: Rs." + amount);
}
public void refund(double amount) {
[Link]("Net Banking Refund Successful: Rs." + amount);
}
}
class OnlinePayment {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter payment amount: ");
double amount = [Link]();
Payment p = new UPIPayment();
[Link](amount);
[Link](amount);
}
}
OUTPUT :
Enter payment amount: 1500
UPI Payment Successful: Rs.1500.0
UPI Refund Successful: Rs.1500.0
RESULT :
Thus the interface programs are executed successfully.
[Link] 25CSED51