Java PGM 19-20
Java PGM 19-20
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;
@Override
public void display() {
System.out.println("Book Publication:");
super.display();
System.out.println("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;
}
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: