assignment34
assignment34
AIM:
To write a Simple JAVA Program to implement functions.
ALGORITHM:
Algorithm for Calculating the Area of a Rectangle:
1. Start
2. Import the Scanner class for input reading 3. Define a class
named AreaOfRectangle
4. Define the main method:
1. Create a Scanner object named 'scanner' to read user input
2. Display a prompt to the user to enter the length of the rectangle
3. Read the input length and store it in the variable 'length'
4. Display a prompt to the user to enter the width of the rectangle
5. Read the input width and store it in the variable 'width'
6. Call the function 'calculateRectangleArea' with 'length' and 'width' as
arguments and store the result in 'area'
7. Print "Area of rectangle: " followed by the calculated 'area'
8. Close the scanner
5. Define a function named 'calculateRectangleArea' that takes 'length' and 'width' as parameters:
1. Return the product of 'length' and 'width'
6. End
import java.util.Scanner;
ALGORITHM:
Creating a Class and Object:
1)Define a class Person with two instance variables: name and age, and a method displayInfo to
print the details.
2)In the Main class, create two Person objects (person1 and person2). 3)Set the
name and age for each person.
4)Call the displayInfo method for each person to display their details.
class Person {
String name;
int age;
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
ALGORITHM:
1)Create a base class Calculator with a method performOperation that returns 0 (this will be
overridden by subclasses).
2)Create subclasses Addition, Subtraction, Multiplication, and Division, each overriding
the performOperation method with their respective implementations.
3)In the Main class:
Create instances of Calculator, Addition, Subtraction, Multiplication, and Division.
Set values for num1 and num2.
Call the performOperation method on each instance and display the results.
class Calculator {
public double performOperation(double num1, double num2)
{ return 0; // Default operation, overridden by subclasses
}
}
ALGORITHM:
1)Create a base class Employee with attributes such as id, name, and basicSalary. 2)Create subclasses for
di erent types of employees, like PermanentEmployee and ContractEmployee, which inherit from
the Employee class.
3)In the Employee class, create a method calculateSalary to calculate the salary for di erent
employee types.
4)In each subclass, override the calculateSalary method based on the specific salary calculation for
that employee type.
5)In the Main class:
Create instances of PermanentEmployee and ContractEmployee.
Set values for id, name, basicSalary, and specific attributes for each employee type. Call
the calculateSalary method on each employee instance to calculate the total salary.
Display the pay slips for each employee.
double calculateSalary() {
return basicSalary;
}
}
@Override
double calculateSalary() {
return basicSalary + allowance;
}
}
@Override double
calculateSalary() {
return basicSalary + overtimePay;
}
}