-1

I have written this code. The output should calculate the interest of the Bank but it gives 0.0 as output. I have created a class named as Bank and extended it in ICICI class.

import java.util.Scanner;

public class Bank
{
    static double rate;
    // n = number of years
    public  double calculateInterest( double PrincipalAmount, double n)
    {
        double interest;
        interest = (PrincipalAmount * n*rate) /100; // interest formula
        return  interest;
    }

    public static void main(String[] args)
    {
        Scanner s1 = new Scanner(System.in);
        System.out.print("Enter PrincipalAmount :" );
        double PrincipalAmount = s1.nextDouble();

        Scanner s2 = new Scanner(System.in);
        System.out.print("Enter Number of Years :" );
        double n = s2.nextDouble();

        ICICI ic;
        ic = new ICICI();// new object created of ICICI Class
        ic.rate = rate; // object call by reference
       System.out.print("Interest of ICICI is " +  ic.calculateInterest( PrincipalAmount,n));
    }
}

public class ICICI extends Bank
{
    double rate = 4;
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
M.Rajput
  • 1
  • 1

3 Answers3

0

you are doing following:

ic.rate = rate;

and rate is not initialized....

so ic.rate = 0.0;

you should by the way take a look at this:

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

calculateInterest method is using static rate variable not instance rate , inheritance does not apply on variable , it does not override variable. so default value of static rate will be 0.0 and hence calculateInterest will give 0.0 (because it is double)answer.

Rahul Rabhadiya
  • 430
  • 5
  • 19
0

You have mistakenly altered the assignment statement:

ic.rate = rate;

Instead, it should be : rate= ic.rate;

Thanks!

Payal Bansal
  • 725
  • 5
  • 17