0% found this document useful (0 votes)
4 views6 pages

Java Module 2

The document outlines three Java programming modules: the first focuses on creating an Employee class with subclasses to calculate salaries and generate pay slips; the second illustrates the use of abstract classes by implementing a Shape class to calculate the area of different shapes; and the third demonstrates string operations using an ArrayList to append, insert, search, and list strings based on specific criteria.

Uploaded by

yogadhakshan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Java Module 2

The document outlines three Java programming modules: the first focuses on creating an Employee class with subclasses to calculate salaries and generate pay slips; the second illustrates the use of abstract classes by implementing a Shape class to calculate the area of different shapes; and the third demonstrates string operations using an ArrayList to append, insert, search, and list strings based on specific criteria.

Uploaded by

yogadhakshan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1 Module 2

2.1. Employee Class and Inheritance (with Pay Slip Generation)

Aim:

To demonstrate the concepts of inheritance and class hierarchy in Java by creating an Employee class
and its subclasses, and to implement salary calculation and pay slip generation.

Algorithm:

1. Create a class Employee with member variables like Emp_name, Emp_id, Address,
Mail_id, and Mobile_no.
2. Create subclasses Programmer, AssistantProfessor, AssociateProfessor, and
Professor that inherit the Employee class.
3. Add a member variable BasicPay to each subclass and calculate the allowances:
o DA = 97% of Basic Pay
o HRA = 10% of Basic Pay
o PF = 12% of Basic Pay
o Staff Club Fund = 0.1% of Basic Pay
4. Calculate the gross and net salary.
5. Create a method to generate the pay slip with the employee's details and salary details.

Java Code:

class Employee {
String Emp_name;
String Emp_id;
String Address;
String Mail_id;
String Mobile_no;

Employee(String name, String id, String address, String mail, String


mobile) {
Emp_name = name;
Emp_id = id;
Address = address;
Mail_id = mail;
Mobile_no = mobile;
}
}

class Programmer extends Employee {


double BasicPay;

Programmer(String name, String id, String address, String mail, String


mobile, double pay) {
super(name, id, address, mail, mobile);
BasicPay = pay;
}

double calculateSalary() {
double DA = 0.97 * BasicPay;
double HRA = 0.10 * BasicPay;
double PF = 0.12 * BasicPay;
double StaffClubFund = 0.001 * BasicPay;
double grossSalary = BasicPay + DA + HRA;
double netSalary = grossSalary - PF - StaffClubFund;
return netSalary;
}

void generatePaySlip() {
double netSalary = calculateSalary();
[Link]("Pay Slip for " + Emp_name);
[Link]("Employee ID: " + Emp_id);
[Link]("Basic Pay: " + BasicPay);
[Link]("Net Salary: " + netSalary);
}
}

public class EmployeeSalary {


public static void main(String[] args) {
Programmer p = new Programmer("John", "P123", "123 Street",
"john@[Link]", "1234567890", 50000);
[Link]();
}
}

Output:

Pay Slip for John


Employee ID: P123
Basic Pay: 50000.0
Net Salary: 57300.0
2.2. Abstract Class Shape and Implementing Methods to Calculate Area

Aim: To illustrate the use of abstract classes and methods in Java by creating an abstract Shape class
and implementing area calculation for different shapes.

Algorithm:

1. Create an abstract class Shape with an integer array and an abstract method
printArea().
2. Create three classes (Rectangle, Triangle, Circle) that extend Shape.
3. Implement the printArea() method in each class to compute the area of the respective
shape.

Java Code:

abstract class Shape {


int dimension1;
int dimension2;

Shape(int d1, int d2) {


dimension1 = d1;
dimension2 = d2;
}

abstract void printArea();


}

class Rectangle extends Shape {


Rectangle(int length, int breadth) {
super(length, breadth);
}

void printArea() {
int area = dimension1 * dimension2;
[Link]("Area of Rectangle: " + area);
}
}

class Triangle extends Shape {


Triangle(int base, int height) {
super(base, height);
}

void printArea() {
double area = 0.5 * dimension1 * dimension2;
[Link]("Area of Triangle: " + area);
}
}
class Circle extends Shape {
Circle(int radius) {
super(radius, 0);
}

void printArea() {
double area = [Link] * [Link](dimension1, 2);
[Link]("Area of Circle: " + area);
}
}

public class ShapeTest {


public static void main(String[] args) {
Shape s1 = new Rectangle(10, 20);
[Link]();

Shape s2 = new Triangle(10, 20);


[Link]();

Shape s3 = new Circle(7);


[Link]();
}
}

Output:

Area of Rectangle: 200


Area of Triangle: 100.0
Area of Circle: 153.93804002589985
2.3. String Operations Using ArrayList

Aim: To demonstrate the use of ArrayList in Java for performing various string operations such as
appending, inserting, searching, and listing strings based on a given criterion.

Algorithm:

1. Create an ArrayList<String> to store the strings.


2. Implement functions to:
o Append: Add a string at the end.
o Insert: Add a string at a specific index.
o Search: Check if a string exists.
o List strings that start with a given letter.

Java Code:

import [Link];

public class StringOperations {

ArrayList<String> strings = new ArrayList<>();

void appendString(String str) {


[Link](str);
}

void insertString(int index, String str) {


[Link](index, str);
}

boolean searchString(String str) {


return [Link](str);
}

void listStringsStartingWith(char letter) {


for (String str : strings) {
if ([Link](0) == letter) {
[Link](str);
}
}
}

public static void main(String[] args) {


StringOperations so = new StringOperations();

[Link]("Apple");
[Link]("Banana");
[Link]("Avocado");
[Link](1, "Apricot");

[Link]("Search for 'Banana': " +


[Link]("Banana"));
[Link]("Strings starting with 'A':");
[Link]('A');
}
}

Output:

Search for 'Banana': true


Strings starting with 'A':
Apple
Apricot
Avocado

You might also like