3
Define a class with the following specifications:
Class name: Bank
Member variables:
double p — stores the principal amount
double n — stores the time period in years
double r — stores the rate of interest
double a — stores the amount
Member methods:
void accept () — input values for p and n using Scanner class methods only.
void calculate () — calculate the amount based on the following conditions:
| Time in (Years) | Rate % |
|---|---|
| Upto 1⁄2 | 9 |
| > 1⁄2 to 1 year | 10 |
| > 1 to 3 years | 11 |
| > 3 years | 12 |
$a = p\Big(1+\frac{r}{100}\Big)^n$
void display () — display the details in the given format.
Show SolutionHide Solution↓
Principal Time Rate Amount
XXX XXX XXX XXX
Write the main method to create an object and call the above methods.
import java.util.Scanner;
public class Bank
{
private double p;
private double n;
private double r;
private double a;
void accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter principal amount: ");
p = in.nextDouble();
System.out.print("Enter time period in years: ");
n = in.nextDouble();
}
void calculate() {
if (n <= 0.5) {
r = 9;
} else if (n <= 1) {
r = 10;
} else if (n <= 3) {
r = 11;
} else {
r = 12;
}
a = p * Math.pow(1 + (r / 100), n);
}
void display() {
System.out.println("Principal\tTime\tRate\tAmount");
System.out.println(p + "\t" + n + "\t" + r + "\t" + a);
}
public static void main(String args[]) {
Bank b = new Bank();
b.accept();
b.calculate();
b.display();
}
}
