0% found this document useful (0 votes)
4 views6 pages

Java PGM 19-20

The document contains two experiments demonstrating method overloading and dynamic method dispatch in Java. The first experiment calculates the area of a square, circle, and triangle using method overloading, while the second experiment involves a class hierarchy for book and paper publications, showcasing dynamic polymorphism. Both experiments include code examples and instructions for execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Java PGM 19-20

The document contains two experiments demonstrating method overloading and dynamic method dispatch in Java. The first experiment calculates the area of a square, circle, and triangle using method overloading, while the second experiment involves a class hierarchy for book and paper publications, showcasing dynamic polymorphism. Both experiments include code examples and instructions for execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT NUMBER 19

AIM: Write a program to calculate area of circle triangle and square using
method overloading
CODE:
class Area
{
public void calcArea(int l)
{
System.out.println("Area of square= "+l*l);
}
public void calcArea(double r)
{
System.out.println("Area of circle= "+(3.14*r*r));
}
public void calcArea(int b,int h)
{
System.out.println("Area of Triangle= "+(0.5*b*h));
}
}
public class Main
{
public static void main(String[] args)
{
Area obj=new Area();
obj.calcArea(5);
obj.calcArea(5.5);
obj.calcArea(5,10);
}
}
OUTPUT:
EXPERIMENT NUMBER 19
AIM: Declare a class called book having author_name as private data member. Extend book
class to have two sub classes called book_publication & paper_publication. Each of these
classes have private member called title. Write a program to show usage of dynamic method
dispatch (dynamic polymorphism) to display book or paper publications of given author. Use
command line arguments for inputting

CODE:
class Book {
private String author_name;

public Book(String author) {


author_name = author;
}

public String getters() {


return author_name;
}

public void display() {


System.out.println("Name of author: " + getters());
}
}

class Book_publication extends Book {


private String title;

public Book_publication(String name, String title) {


super(name);
this.title = title;
}

@Override
public void display() {
System.out.println("Book Publication:");
super.display();
System.out.println("Title: " + title);
}
}

class Paper_publication extends Book {


private String title;

public Paper_publication(String name, String title) {


super(name); // Corrected to pass the author name to the superclass
this.title = title;
}

@Override
public void display() {
System.out.println("Paper Publication:");
super.display();
System.out.println("Title: " + title);
}
}
class Main {
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Usage: java Main <author_name> <type> <title>");
System.out.println("<type>: 'book' for book publication or 'paper' for
paper publication");
return;
}

String author_Name = args[0];


String type = args[1].toLowerCase();
String title = args[2];

Book publication;

if (type.equals("book")) {
publication = new Book_publication(author_Name, title);
} else if (type.equals("paper")) {
publication = new Paper_publication(author_Name, title);
} else {
System.out.println("Invalid type. Use 'book' or 'paper'.");
return;
}

publication.display();
}
}
OUTPUT:

You might also like