0

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

Zethyst
  • 33
  • 6
  • Probably because integer arithmetic often results in 0 during division. Switch to `double` please. – markspace Mar 18 '23 at 18:35
  • 2
    You are multiplying p, r and t, which are all integers. Thus, the result is an integer. Then you are dividing this integer by the integer 100. Because integers can't store floating point digits, java cuts them off. After java has already done that, you are assigning the calculated value to a double and lost the decimal places. Solution: Convert it to a double before doing the division. If one of the divisors is a double, java will convert the other one to a double, too. Simply calculate `(p * r * t) / 100d` or `(p * r * t) / 100.` or `(p * r * t) / 100.0`, they all use 100 as a double. – Cactusroot Mar 18 '23 at 18:44
  • 2
    As an aside, I'd strongly encourage you to put more time into thinking of meaningful names for both variables and classes - `ob`, `SI`, `CI`, `p`, `r`, and `t` are really non-descriptive. (`SimpleInterest`, `CompoundInterest`, `principle`, `rate`, and `years` would be much more meaningful names.) – Jon Skeet Mar 18 '23 at 18:45
  • Next, in your `Interest.display` method, you're creating a new instance of `Interest`. You're populating the fields within that new instance - and then ignoring it. I would recommend you call it something entirely different (as it's *not* displaying the result), and set the fields on the instance it's called on, rather than a new one. Separate out the different aspects: 1) picking which Interest subclass to use; 2) populating the fields; 3) displaying the result. – Jon Skeet Mar 18 '23 at 18:48
  • Thank you all for your suggestions. Helped me a lot. – Zethyst Mar 18 '23 at 18:53
  • @Cactusroot I did but it's still not working. – Zethyst Mar 18 '23 at 19:00
  • Have you directly assigned the fields `Interest#display` instead of creating a seperate object and also switched to `100d` for the calculation in `CI#display`? – Cactusroot Mar 18 '23 at 19:03
  • @Cactusroot Many thanks! I used different name for the Interest#display object and it worked like a charm. – Zethyst Mar 18 '23 at 19:06
  • @Cactusroot My bad, it worked because I assigned default values in it. Is there a way to ask the user for the values here? – Zethyst Mar 18 '23 at 19:08
  • You don't need the object at all. Inside of the function `Interest#display` you are already in the scope of an `Interest` object! You can assign the variables directly without using `ob.` and remove the local variable `ob` completely. – Cactusroot Mar 18 '23 at 19:09
  • @Cactusroot Now I get it. I did because when I used to create main function inside the class it was not visible despite it being in the same class. Why was that though? – Zethyst Mar 18 '23 at 19:17
  • Ah, I see. That's because inside of the main function, there wasn't any object of the class Interest yet. A static function, like the main function, is a complete standalone function: You are not inside of the context/scope of any object. A function inside of the class is always called on an object of that class, like you do by calling `ob.display()`. The variable `ob` holds an object of that class, so when assigning the variables, you modify the variables of this object. – Cactusroot Mar 18 '23 at 19:25
  • The difference of modifying an object from a variable or from within the class is that within the class you can also access fields with the `private` visibility modifier. This is used to ensure that variables of objects cannot be arbitrarily changed from anywhere, you are trying to sustain an autonomy of an object. Since your variables don't have a visibility modifier, they are package private, so you can modify them from anything within your folder. This is why you could modify the variables from your main function. – Cactusroot Mar 18 '23 at 19:27

0 Answers0