3.
Implement a Java program to define a class named "Rectangle" with attributes for width and
height. Include methods to compute the area and perimeter of the rectangle, and demonstrate the
functionality in the main method.
// Rectangle class
class Rectangle
// Attributes for width and height
private double width;
private double height;
// Constructor to initialize the width and height
public Rectangle(double width, double height)
[Link] = width;
[Link] = height;
// Method to calculate the area of the rectangle
public double getArea()
return width * height;
// Method to calculate the perimeter of the rectangle
public double getPerimeter()
return 2 * (width + height);
// Getter methods for width and height (optional)
public double getWidth()
{
return width;
public double getHeight()
return height;
// Main class to demonstrate functionality
public class Main
public static void main(String[] args)
// Read width and height
Scanner sc=new Scanner([Link]);
[Link](“Enter the width and Height”);
double width=[Link]();
double height=[Link]();
// Create a rectangle object with the entered width and height
Rectangle rectangle = new Rectangle(width,height);
// Display the width, height, area, and perimeter of the rectangle
[Link]("Rectangle:");
[Link]("Width: " + [Link]());
[Link]("Height: " + [Link]());
[Link]("Area: " + [Link]());
[Link]("Perimeter: " + [Link]());
}
4. Showcase the concept of polymorphism by designing appropriate methods, defining member
data, and writing a main program to create a class named "Person" with methods getFirstName()
and getLastName(). Then, create a subclass called "Employee" that introduces an additional method
called getEmployeeId() and overrides the getLastName() method to include both the employee's job
title and last name.
// Super class Person
class Person
// Member data
private String firstName;
private String lastName;
// Constructor
public Person(String firstName, String lastName)
[Link] = firstName;
[Link] = lastName;
// Methods to get first and last names
public String getFirstName() {
return firstName;
public String getLastName() {
return lastName;
// Subclass Employee extends Person
class Employee extends Person
// Additional member data for Employee
private int employeeId;
private String jobTitle;
// Constructor for Employee
public Employee(String firstName, String lastName, int employeeId, String jobTitle)
super(firstName, lastName); // Calling the constructor of the superclass (Person)
[Link] = employeeId;
[Link] = jobTitle;
// New method to get the employee ID
public int getEmployeeId()
return employeeId;
// Overriding the getLastName() method to include the job title
public String getLastName() {
return jobTitle + " " + [Link](); // Including job title with last name
public class Main
public static void main(String[] args)
{
// Creating an instance of Person
Person person = new Person("Person-ABC", "XYZ");
[Link]("Person:");
[Link]("First Name: " + [Link]());
[Link]("Last Name: " + [Link]());
// Creating an instance of Employee
Employee employee = new Employee("Veena", "RS", 12345, "Associate Professor");
[Link]("\nEmployee:");
[Link]("First Name: " + [Link]());
[Link]("Last Name (with Job Title): " + [Link]());
[Link]("Employee ID: " + [Link]());