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)
this.width = width;
this.height = 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(System.in);
System.out.println(“Enter the width and Height”);
double width=sc.nextDouble();
double height=sc.nextDouble();
// 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
System.out.println("Rectangle:");
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Height: " + rectangle.getHeight());
System.out.println("Area: " + rectangle.getArea());
System.out.println("Perimeter: " + rectangle.getPerimeter());
}
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)
this.firstName = firstName;
this.lastName = 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)
this.employeeId = employeeId;
this.jobTitle = 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 + " " + super.getLastName(); // 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");
System.out.println("Person:");
System.out.println("First Name: " + person.getFirstName());
System.out.println("Last Name: " + person.getLastName());
// Creating an instance of Employee
Employee employee = new Employee("Veena", "RS", 12345, "Associate Professor");
System.out.println("\nEmployee:");
System.out.println("First Name: " + employee.getFirstName());
System.out.println("Last Name (with Job Title): " + employee.getLastName());
System.out.println("Employee ID: " + employee.getEmployeeId());