Ir Javalab Assignment3
Ir Javalab Assignment3
1. Write a Java program to create a base class Animal with methods move() and
makeSound(). Create two subclasses Bird and Panthera. Override the move() method in
each subclass to describe how each animal moves. Also, override the makeSound() method
in each subclass to make a specific sound for each animal.
SOURCE CODE:
class Animal {
void move() {
System.out.println("Animal moves ");
}
void makeSound() {
System.out.println("Animal makes a sound\n");
}
}
class Bird extends Animal {
void move() {
System.out.println("Birds fly");
}
}
class Panthera extends Animal{
void makeSound() {
System.out.println("Pather makes Sound");
}
}
public class Main {
public static void main(String args[]) {
Animal bird = new Bird();
Animal panthera = new Panthera();
performAction(bird);
performAction(panthera);
}
static void performAction(Animal animal) {
animal.move();
animal.makeSound();
}
}
OUTPUT:
Birds fly
Animal makes a sound
Animal moves
Pather makes Sound
11
2. Write a Java program to create an abstract class Shape3D with abstract methods
calculateVolume() and calculateSurfaceArea(). Create subclasses Sphere and Cube that
extend the Shape3D class and implement the respective methods to calculate the volume
and surface area of each shape.
Cube Sphere
SOURCE CODE:
12
public class Test {
public static void main(String args[]) {
Sphere sphere = new Sphere(7.0);
Cube cube = new Cube(3.0);
System.out.println("Sphere Volume : "+sphere.calculateVolume());
System.out.println("Sphere Surface Area : "+sphere.calculateSurfaceArea());
OUTPUT:
3. Write a Java program to create an interface Shape with the getArea() method. Create three
classes Rectangle, Circle, and Triangle that implement the Shape interface. Implement the
getArea() method for each of the three classes.
SOURCE CODE:
import java.util.*;
interface Shape {
double getArea();
}
class Rectangle implements Shape {
double length;
double width;
Rectangle(double length,double width) {
this.length=length;
this.width=width;
}
public double getArea() {
return length*width;
}
}
class Circle implements Shape {
double radius ;
13
Circle(double radius) {
this.radius=radius;
}
public double getArea() {
return 2.0 * Math.PI * Math.pow(radius,2);
}
}
class Triangle implements Shape {
double height,base;
14
OUTPUT:
4. Write a Java program to create a class called Shape with methods called getPerimeter()
and getArea(). Create a subclass called Circle that overrides the getPerimeter() and
getArea() methods to calculate the area and perimeter of a circle.
SOURCE CODE:
class Shape {
double getPerimeter() {
return 0.0;
}
double getArea() {
return 0.0;
}
}
class Circle extends Shape {
double radius;
public Circle(double radius) {
this.radius = radius;
}
double getPerimeter() {
return 2 * Math.PI * radius;
}
double getArea() {
return Math.PI * Math.pow(radius,2);
}
}
15
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(7);
System.out.println("Perimeter of the circle : " + circle.getPerimeter());
System.out.println("Area of the circle : " + circle.getArea());
}
}
OUTPUT:
5. Write a Java program to create a class called Employee with methods called work() and
getSalary(). Create a subclass called HRManager that overrides the work() method and
adds a new method called addEmployee().
SOURCE CODE:
class Employee {
String name;
double salary;
public Employee(String name, double salary) {
this.name=name;
this.salary=salary;
}
public void work() {
System.out.println(name+" is working");
}
public double getsalary() {
return salary;
}
}
class HRManager extends Employee {
public HRManager(String name,double salary) {
super(name,salary);
}
public void work() {
System.out.println(name +"is managing HR tasks");
}
16
public void addEmployee(String name) {
System.out.println(name + " is new Employee");
}
}
employee.work();
System.out.println("Employee salary: " + employee.getsalary());
hrm.work();
System.out.println("Employee salary: " + hrm.getsalary());
hrm.addEmployee("Alice");
}
}
OUTPUT:
Ramesh is working
Employee salary: 500000.0
17