Advertisements
Advertisements
Question
Write a program for the following.
Define a class Mortgage, described as below:
| Data Members: | ||
| loan (int), time (double), rate (double), interest (double), repay (double) | ||
| Member Methods: | ||
| (i) | Mortgage(int p, | constructor to initialize member data - loan to p, double r, double t) rate to r, time to t and rest to null. |
| (ii) | Estimate() | to calculate the Interest using the formula interest = (loan * rate * time )/100 and also to calculate Amount to Repay using the formula repay = loan + interest. |
| (iii) | Display() | to display all the member data. |
| Write a main() method to create and object of the class and call the above member methods. | ||
Code Writing
Advertisements
Solution
class Mortgage
{
int loan;
double time, rate, interest, repay;
//Parameterized Constructor
Mortgage(int p, double r, double t)
{
loan = p;
rate = r;
time = t;
interest = 0.0;
repay = 0.0;
}
//Estimate method
void Estimate()
{
interest = (loan * rate * time) / 100;
repay = loan + interest;
}
//Display method
void Display()
{
System.out.println("Loan =" + loan);
System.out.println("Rate =" + rate);
System.out.println("Time =" + time);
System.out.println("Interest =" + interest);
System.out.println("Amount to Repay =" + repay);
}
//Main method
public static void main(String args[])
{
Mortgage obj = new Mortgage(100000, 8.5, 2);
obj.Estimate();
obj.Display();
}
}shaalaa.com
Is there an error in this question or solution?
