0% found this document useful (0 votes)
12 views9 pages

SandhyaP Exercise2

Uploaded by

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

SandhyaP Exercise2

Uploaded by

jagadish22112004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Ex.

2
Exercise - 2
Date: 22.07.2024

AIM
Develop a java application with Employee class with Emp_name, Emp_id, Address, Mail_id,
Mobile_no as members. Inherit the classes, Programmer, Assistant Professor, Associate
Professor and Professor from employee class. Add Basic Pay (BP) as the member of all the
inherited classes with 97% of BP as DA, 10 % of BP as HRA, 12% of BP as PF, 0.1% of BP
for staff club fund. Generate pay slips for the employees with their gross and net salary

ALGORITHM
1. Initialize the scanner.
2. Define class `emp` with attributes: name, id, adr, mid, mob, bp.
3. Define method `details` to prompt and read employee name, id, address, mail id, and
mobile number.
4. Define method `sal` to:
- Calculate DA as 0.97 * bp.
- Calculate HRA as 0.10 * bp.
- Calculate PF as 0.12 * bp.
- Calculate SCF as 0.001 * bp.
- Calculate gross salary as bp + DA + HRA.
- Calculate net salary as gross - PF - SCF.
- Print employee details and salary information.
5. Define subclasses of `emp`:
- `Programmer` with `bp` set to 50000.
- `AssistantProfessor` with `bp` set to 60000.
- `AssociateProfessor` with `bp` set to 70000.
- `Professor` with `bp` set to 80000.
6. In `Main` method:
- Create instances of `Programmer`, `AssistantProfessor`, `AssociateProfessor`, and
`Professor`.
- Call `details` and `sal` methods for each instance and print them.
PROGRAM
import java.util.Scanner;

class emp {
String name;
int id;
String adr;
String mid;
String mob;
float bp;

public void details() {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter Employee name: ");


name = scanner.nextLine();

System.out.print("Enter Employee ID: ");


id = scanner.nextInt();
scanner.nextLine(); // Consume newline

System.out.print("Enter Address: ");


adr = scanner.nextLine();

System.out.print("Enter Mail ID: ");


mid = scanner.nextLine();

System.out.print("Enter Mobile no.: ");


mob = scanner.nextLine(); // Use nextLine() for string input
}
public void sal() {
float da = 0.97f * bp;
float hra = 0.10f * bp;
float pf = 0.12f * bp;
float scf = 0.001f * bp;

float gross = bp + da + hra;


float net = gross - pf - scf;
System.out.println();
System.out.println("Employee name: " + name);
System.out.println("Employee ID: " + id);
System.out.println("Address: " + adr);
System.out.println("Mail ID: " + mid);
System.out.println("Mobile no.: " + mob);
System.out.println("Basic Pay: " + bp);
System.out.println("Gross Salary: " + gross);
System.out.println("Net Salary: " + net);
System.out.println();
}
}

class Programmer extends emp {


public Programmer() {
bp = 50000;
}
}

class AssistantProfessor extends emp {


public AssistantProfessor() {
bp = 60000;
}
}

class AssociateProfessor extends emp {


public AssociateProfessor() {
bp = 70000;
}
}

class Professor extends emp {


public Professor() {
bp = 80000;
}
}

public class Main {


public static void main(String[] args) {
Programmer programmer = new Programmer();
programmer.details();
programmer.sal();

AssistantProfessor assistantProfessor = new AssistantProfessor();


assistantProfessor.details();
assistantProfessor.sal();

AssociateProfessor associateProfessor = new AssociateProfessor();


associateProfessor.details();
associateProfessor.sal();
Professor professor = new Professor();
professor.details();
professor.sal();
}
}

OUTPUT
RESULT
The Java program to generate Employee pay slip is written and executed successfully.
AIM
The monthly payment for a given loan pays the principal and the interest. The monthly
interest is computed by multiplying the monthly interest rate and the balance (the remaining
principal). The principal paid for the month is therefore the monthly payment minus the
monthly interest. Write a program that lets the user enter the loan amount, number of years,
and interest rate then displays the amortization schedule for the loan.

ALGORITHM
1. Initialize Scanner to read user input.
2. Input Loan Details: loan amount, number of years, annual interest rate.
3. Calculate Monthly Rate and Payments:
- Compute monthly interest rate: `monRate = annRate / 1200`.
- Compute number of months: `numMonths = yrs * 12`.
- Compute monthly payment using the formula.
- Print monthly payment and total payment.
4. Initialize Balance and Total Interest: set balance to loan amount, initialize total interest
paid to 0.
5. Print Amortization Schedule Header.
6. Calculate and Print Each Payment:
- For each month from 1 to `numMonths`:
- Calculate interest payment: `intPay = monRate * bal`.
- Calculate principal payment: `prinPay = monPay - intPay`.
- Update balance: `bal -= prinPay`.
- Accumulate total interest: `totInt += intPay`.
- Print payment details: payment number, interest payment, principal payment, balance.
7. Print Total Interest Paid.

PROGRAM
import java.util.Scanner;

public class LoanAmortization {


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

System.out.print("Enter loan amount: ");


double loanAmt = sc.nextDouble();

System.out.print("Enter number of years: ");


int yrs = sc.nextInt();

System.out.print("Enter annual interest rate: ");


double annRate = sc.nextDouble();

double monRate = annRate / 1200;


int numMonths = yrs * 12;
double monPay = loanAmt * monRate / (1 - 1 / Math.pow(1 + monRate, numMonths));

System.out.printf("Monthly Payment: %.2f\n", monPay);


System.out.printf("Total Payment: %.2f\n\n", monPay * numMonths);

double bal = loanAmt;


double totInt = 0;
System.out.println("Payment \tInterest\tPrincipal\tBalance");
for (int i = 1; i <= numMonths; i++) {
double intPay = monRate * bal;
double prinPay = monPay - intPay;
bal -= prinPay;
totInt += intPay;

System.out.printf("\t%d\t\t%.2f\t\t%.2f\t\t%.2f\n", i, intPay, prinPay, bal);


}
System.out.printf("\nTotal Interest Paid: %.2f\n", totInt);
}
}

OUTPUT

RESULT
The Java program to generate Loan amortization schedule is written and executed
successfully.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy