Menu
Topics Index
...
`

Calculate Interest Using Interfaces


If you need explanation Read this topic

If you need Answer Take test on this topic

Calculate Interest Using Interfaces
What will be the output of the following program?
public class InterestCalculator {
    public static void main(String[] args) {
        Interest ii = new Interest(4000.0, 5, 2);
        print(ii.getInterest());
        Simple s = new Simple(8000.0, 4, 2);
        print(s.getInterest());
        print(((IInterest) s).getInterest());
    }
    private static void print(double interest) {
        System.out.print(String.format("~%5.2f~", interest));
    }
}
interface IInterest { double getInterest(); }
class Interest {
    double principal, rate, years;
    public Interest(double principal, double rate, double years) {
        this.principal = principal; this.rate = rate; this.years = years;
    }
    double getInterest() { return principal * rate * years;    }
}
class Simple extends Interest implements IInterest {
    public Simple(double principal, double rate, double years) {
        super(principal, years, rate);
    }
    double getInterest() { return super.getInterest() / 100.0; }
}

A. ~40000.00~00640.00~00640.00~
B. ~40000.00~~640.00~~640.00~
C. ~40000.00~~00640.00~~00640.00~
D. Some other output not shown here
E. Compilation or Runtime Error
Topic: Unknown

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans:E In Simple Class: we are extending the Interest class and IInterest interface we have same method "getInterest()" in both . hence it gives compile time error: can hide the method

Posted by Indrasena Reddy    2014-08-28 12:41:10


Answer is b.first and second function calls will call Interest and Simple Class's getInterest method and third function call will call Simple class's interest method since s is an instance of Simple Class.~%5.2 f means  5 digits before decimal and 2 digits after decimal

Posted by Shashanka Mogaliraju    2014-08-28 12:49:05


E runtime error aftr printing 40000.00 as exception in thread main occurs

Posted by Shabaaz Shaikz    2014-08-28 12:49:22


ans e.
unresolved compilation problem.
need to change the visibility of simple class getinterest to public.
bcz cannot reduce the visibility of interface method.
prints ~4000.00~ after exception occured

Posted by Maheshwari Natarajan    2014-08-28 15:05:05


Congratulations Indrasena Reddy. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-09-01 06:02:26


© meritcampus 2019

All Rights Reserved.

Open In App