Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

CSE ASSIGNNMENT

WEEK-08
NAME:P.ANAGHAA THANMAYEE
REG.NO:22BCE7306
INPUT:
Write a method in java to calculate the amount payable if money has been lent on 
simple interest. Principal or money lent = P, Rate of interest = R% per annum and 
Time = T years. Then Simple Interest (SI) = (P x R x T)/ 100. Amount payable = 
Principal + SI. P, R and T are given as input to the program. 
Implement the above method using 

1. Nested inner class 

public class SimpleInterestCalculator {

private double principal;

private double rate;

private double time;

public SimpleInterestCalculator(double principal, double rate, double time) {

this.principal = principal;

this.rate = rate;

this.time = time;

public double calculateAmountPayable() {

double simpleInterest = (principal * rate * time) / 100;


double amountPayable = principal + simpleInterest;

return amountPayable;

public static void main(String[] args) {

double principal = 1000;

double rate = 5;

double time = 2;

SimpleInterestCalculator calculator = new SimpleInterestCalculator(principal, rate,


time);

double amountPayable = calculator.calculateAmountPayable();

System.out.println("Amount Payable: " + amountPayable);

private class NestedInnerClass {

public void printValues() {

System.out.println("Principal: " + principal);

System.out.println("Rate: " + rate);

System.out.println("Time: " + time);

}
OUTPUT:

INPUT:
Write a method in java to calculate the amount payable if money has been lent on 
simple interest. Principal or money lent = P, Rate of interest = R% per annum and 
Time = T years. Then Simple Interest (SI) = (P x R x T)/ 100. Amount payable = 
Principal + SI. P, R and T are given as input to the program. 
Implement the above method using 

2.Method local inner class 

public class SimpleInterest {

private double principal;

private double rate;

private double time;

public SimpleInterest(double principal, double rate, double time) {

this.principal = principal;

this.rate = rate;

this.time = time;

public double calculateAmountPayable() {

class SimpleInterestCalculator {

public double calculateSimpleInterest() {

return (principal * rate * time) / 100.0;

}
}

SimpleInterestCalculator sic = new SimpleInterestCalculator();

double simpleInterest = sic.calculateSimpleInterest();

double amountPayable = principal + simpleInterest;

return amountPayable;

public static void main(String[] args) {

SimpleInterest si = new SimpleInterest(1000, 5, 2);

double amountPayable = si.calculateAmountPayable();

System.out.println("Amount Payable: " + amountPayable);

OUTPUT:

INPUT:
Write a method in java to calculate the amount payable if money has been lent on 
simple interest. Principal or money lent = P, Rate of interest = R% per annum and 
Time = T years. Then Simple Interest (SI) = (P x R x T)/ 100. Amount payable = 
Principal + SI. P, R and T are given as input to the program. 
Implement the above method using
3.static nested inner class
public class SimpleInterest {
static class Calculator {
public static double calculateAmountPayable(double principal, double rate, double time) {
double si = (principal * rate * time) / 100;
double amountPayable = principal + si;
return amountPayable;
}
}

public static void main(String[] args) {


double principal = 1000;
double rate = 5;
double time = 2;
double amountPayable = Calculator.calculateAmountPayable(principal, rate, time);
System.out.println("Amount Payable: " + amountPayable);
}
}

OUTPUT:

INPUT:
Write a method in java to calculate the amount payable if money has been lent on 
simple interest. Principal or money lent = P, Rate of interest = R% per annum and 
Time = T years. Then Simple Interest (SI) = (P x R x T)/ 100. Amount payable = 
Principal + SI. P, R and T are given as input to the program. 
Implement the above method using 

4.anonymous inner class

import java.util.Scanner;

public class SimpleInterest {


public static void main(String[] args) {

Scanner input = new Scanner(System.in);

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

double principal = input.nextDouble();

System.out.print("Enter Rate of Interest: ");

double rate = input.nextDouble();

System.out.print("Enter Time (in years): ");

double time = input.nextDouble();

// create an anonymous inner class to calculate simple interest

SimpleInterestCalculator calculator = new SimpleInterestCalculator() {

@Override

public double calculateSimpleInterest(double principal, double rate, double time) {

return (principal * rate * time) / 100;

};

// calculate simple interest using the anonymous inner class

double simpleInterest = calculator.calculateSimpleInterest(principal, rate, time);

// calculate amount payable

double amountPayable = principal + simpleInterest;

System.out.println("Simple Interest: " + simpleInterest);


System.out.println("Amount Payable: " + amountPayable);

// define an interface for the anonymous inner class

interface SimpleInterestCalculator {

double calculateSimpleInterest(double principal, double rate, double time);

OUTPUT:

You might also like