0% found this document useful (0 votes)
7 views

assignment34

The document outlines simple Java programs demonstrating functions, classes, polymorphism, and inheritance. It includes algorithms and code examples for calculating the area of a rectangle, creating classes and objects, implementing a simple calculator, and generating pay slips for employees. Each section provides a clear aim, algorithm, program code, and expected output.

Uploaded by

undestined
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

assignment34

The document outlines simple Java programs demonstrating functions, classes, polymorphism, and inheritance. It includes algorithms and code examples for calculating the area of a rectangle, creating classes and objects, implementing a simple calculator, and generating pay slips for employees. Each section provides a clear aim, algorithm, program code, and expected output.

Uploaded by

undestined
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

1)Simple JAVA Programs to implement functions

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

PROGRAM AND OUTPUT:


1. Calculate the Area of a Rectangle:

import java.util.Scanner;

public class AreaOfRectangle {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter length: "); double


length = scanner.nextDouble();
System.out.print("Enter width: "); double width
= scanner.nextDouble(); double area =
calculateRectangleArea(length, width);
System.out.println("Area of rectangle: " + area);
scanner.close();
}

public static double calculateRectangleArea(double length, double width)


{ return length * width;
}}
OUTPUT:
Enter length: 5
Enter width: 3
Area of rectangle: 15.0

2)Simple JAVA Programs with Class and Objects


AIM:
To write a Simple JAVA Program with Class and Objects.

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.

PROGRAM AND OUTPUT:


Creating a Class and Object:

class Person {
String name;
int age;

void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}

public class Main {


public static void main(String[] args) { Person
person1 = new Person(); person1.name =
"John"; person1.age = 25;
person1.displayInfo();

Person person2 = new Person();


person2.name = "Jane"; person2.age = 30;
person2.displayInfo();
}
} OUTPUT:
Name: John
Age: 25
Name: Jane
Age: 30

3)Implement JAVA program Simple Calculator using polymorphism


AIM:
To Implement JAVA program - Simple Calculator using polymorphism.

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.

PROGRAM AND OUTPUT:

class Calculator {
public double performOperation(double num1, double num2)
{ return 0; // Default operation, overridden by subclasses
}
}

class Addition extends Calculator {


@Override
public double performOperation(double num1, double num2)
{ return num1 + num2;
}
}

class Subtraction extends Calculator {


@Override
public double performOperation(double num1, double num2)
{ return num1 - num2;
}
}

class Multiplication extends Calculator {


@Override
public double performOperation(double num1, double num2)
{ return num1 * num2;
}
}

class Division extends Calculator {


@Override
public double performOperation(double num1, double num2)
{ if (num2 != 0) {
return num1 / num2;
} else {
System.out.println("Cannot divide by zero.");
return Double.NaN;
}
}
}

public class Main {


public static void main(String[] args) { Calculator
calculator = new Calculator(); Addition addition =
new Addition(); Subtraction subtraction = new
Subtraction(); Multiplication multiplication = new
Multiplication(); Division division = new Division();

double num1 = 10; double


num2 = 5;

System.out.println("Addition: " + addition.performOperation(num1, num2));


System.out.println("Subtraction: " + subtraction.performOperation(num1, num2));
System.out.println("Multiplication: " + multiplication.performOperation(num1, num2));
System.out.println("Division: " + division.performOperation(num1, num2));
}}
OUTPUT:
Addition: 15.0
Subtraction: 5.0
Multiplication: 50.0
Division: 2.0

4)Implement JAVA program - Pay Slip Generator using Inheritance


AIM:
To Implement JAVA program - Pay Slip Generator using Inheritance.

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.

PROGRAM AND OUTPUT:

class Employee { int id;


String name; double
basicSalary;

Employee(int id, String name, double basicSalary) { this.id


= id; this.name = name;
this.basicSalary = basicSalary;
}

double calculateSalary() {
return basicSalary;
}
}

class PermanentEmployee extends Employee {


double allowance;

PermanentEmployee(int id, String name, double basicSalary, double allowance) { super(id,


name, basicSalary);
this.allowance = allowance;
}

@Override
double calculateSalary() {
return basicSalary + allowance;
}
}

class ContractEmployee extends Employee {


double overtimePay;

ContractEmployee(int id, String name, double basicSalary, double overtimePay) { super(id,


name, basicSalary);
this.overtimePay = overtimePay;
}

@Override double
calculateSalary() {
return basicSalary + overtimePay;
}
}

public class Main {


public static void main(String[] args) {
PermanentEmployee permanentEmployee = new PermanentEmployee(101,
"John", 5000, 1000);
ContractEmployee contractEmployee = new ContractEmployee(102, "Jane", 3000,
500);

System.out.println("Pay Slip for Permanent Employee:");


System.out.println("ID: " + permanentEmployee.id);
System.out.println("Name: " + permanentEmployee.name);
System.out.println("Total Salary: " + permanentEmployee.calculateSalary());

System.out.println("\nPay Slip for Contract Employee:");


System.out.println("ID: " + contractEmployee.id);
System.out.println("Name: " + contractEmployee.name);
System.out.println("Total Salary: " + contractEmployee.calculateSalary());
}}
Output:
Pay Slip for Permanent Employee:
ID: 101
Name: John
Total Salary: 6000.0

Pay Slip for Contract Employee:


ID: 102
Name: Jane
Total Salary: 3500.0

You might also like