Advertisements
Advertisements
Question
Define a class Interest having the following description:
Class name: Interest
Data members:
int p: to store principal (sum)
int r: to store rate
int t: to store time
double interest: to store the interest to be paid
double amt: to store the amount to be paid
Member methods:
void input(): Stores the principal, rate, time
void cal(): Calculates the interest and amount to be paid
void display(): Displays the principal, interest and amount to be paid
Write a program to compute the interest according to the given conditions and display the output.
Time | Rate of interest |
For 1 year | 6.5% |
For 2 years | 7.5% |
For 3 years | 8.5% |
For 4 years or more | 9.5% |
(Note: Time to be taken only in whole years)
Code Writing
Solution
import java.util.*;
class Interest
{int p, t;
double r, interest, amt;
void input()
{Scanner ob = new Scanner(System.in);
System.out.println("Enter the principal");
p = ob.nextlnt();
System.out.println("Enter the time");
t = ob.nextlnt();
}
void cal()
{if. (t == 1) r = 6.5;
else if (t == 2) r = 7.5;
else if (t == 3) r = 8.5;
else if (t >= 4) r = 9.5;
interest = (p * r * t) / 100;
amt = p + interest;
}
void display()
{System.out.println("Principal:Rs. " + p);
System.out.println("Interest: Rs. " + interest);
System.out.println("Amount: Rs. " + amt);
}
public static void main(String args[])
{Interest in = new Interest();
in.input();
in.cal();
in.display();
}
}
shaalaa.com
Is there an error in this question or solution?