LabProgram 3&4
LabProgram 3&4
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
this.width = width;
this.height = height;
return width;
return height;
double width=sc.nextDouble();
double height=sc.nextDouble();
System.out.println("Rectangle:");
}
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.
class Person
// Member data
// Constructor
this.firstName = firstName;
this.lastName = lastName;
return firstName;
return lastName;
this.employeeId = employeeId;
this.jobTitle = jobTitle;
return employeeId;
return jobTitle + " " + super.getLastName(); // Including job title with last name
{
// Creating an instance of Person
System.out.println("Person:");
System.out.println("\nEmployee:");