Program to demonstratate the use of Constructor and implement its various types

 Problem Statement :

Write a program to test class SavingsAccount. Instantiate two savingsAccount objects, saver1 and saver2, with balances of Rs 2000.00 and Rs 3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest and print the new balances for both savers. Then set the annualInterestRate to 5%, calculate the next month's interest and print the new balances for both savers.

Introduction:

Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor

There are two types of constructors:

·       Default constructor (no-arg constructor)

·       Parameterized constructor

 

Method Overloading:

If a class has multiple methods having same name but different in parameters, it is known as  Method Overloading.If we have to perform only one operation, having same name of the methods increases the readability of the program.Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs.So, we perform method overloading to figure out the program quickly.

Method overriding:

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

import java.io.*;
import java.util.*;

public class SavingAcc {
   

static int rate1=4;
int bal,acc_no,mon_int;
String nm;
void get(int bal1,int annual_int)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your name:");
nm=sc.next();
System.out.println("Enter Your Account Number:");
acc_no=sc.nextInt();
//bal1=(bal1*4)/100;        
System.out.println("Your balance is:"+bal1);
mon_int=(bal1*annual_int)/12;
bal1=bal1+mon_int;
System.out.println("Monthly interest is:"+mon_int);
System.out.println("Balance is:"+bal1);
    }
/*public void month_int(int l)
{
mon_int=(bal1*annual_int)/12;
bal1=bal1+mon_int;
System.out.println("Monthly interest is:"+mon_int);
System.out.println("Balance is:"+bal1);
}*/
public static void main(String args[])
{
int i;
SavingAcc[] sa= new SavingAcc[3];
SavingAcc sv=new SavingAcc();
sv.get(2000,4);        
//sv.month_int(4);  
for(i=0;i<2;i++)
{
   
sa[i]=new SavingAcc();
sa[i].get(3000,5);
//sa[i].month_int(5);
   
}
}
       

}

out put

Enter Your name:

ajay

Enter Your Account Number:

12345

Your balance is:2000

Monthly interest is:666

Balance is:2666

Enter Your name:

vijay

Enter Your Account Number:

23456

Your balance is:3000

Monthly interest is:1250

Balance is:4250

Enter Your name:

gaja

Enter Your Account Number:

4523

Your balance is:3000

Monthly interest is:1250

Balance is:4250

Post a Comment

Previous Post Next Post