I have to use dynamic method dispatch but the interest is always coming 'zero' no matter what I do with the data members.
Here's the code:
import java.util.*;
class Interest {
int p, r, t;
void display() {
Scanner sc = new Scanner(System.in);
System.out.println("------Interest Calculation------");
Interest ob = new Interest();
System.out.print("Enter Principal Amount: ");
ob.p = sc.nextInt();
System.out.print("Enter Rate of Interest: ");
ob.r = sc.nextInt();
System.out.print("Enter Years: ");
ob.t = sc.nextInt();
}
}
class SI extends Interest {
void display() {
super.display();
double i = (p * r * t) / 100;
System.out.println("Simple Interest: " + i);
}
}
class CI extends Interest {
void display() {
super.display();
double i = Math.pow(p * (1 + r / 100), t) - p;
System.out.println("Compound Interest: " + i);
}
}
class Money {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Interest ob;
System.out.println("1. Simple Interest");
System.out.println("2. Compound Interest");
System.out.println("Enter your choice: ");
int ch = sc.nextInt();
switch (ch) {
case 1:
ob = new SI();
ob.display();
break;
case 2:
ob = new CI();
ob.display();
break;
default:
System.out.println("Wrong Choice!");
break;
}
}
}
Output:
1. Simple Interest
2. Compound Interest
Enter your choice:
1
------Interest Calculation------
Enter Principal Amount: 4
Enter Rate of Interest: 3
Enter Years: 3
Simple Interest: 0.0