*****************JAVA******************** Write a class BankAccount to keep trac
ID: 3870594 • Letter: #
Question
*****************JAVA********************
Write a class BankAccount to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the annual interest rate to some initial values (and you should also implement a no-arguments constructor that sets both the balance and the interest rate to zero).
The class should have methods to change or retrieve the current balance or interest rate. There should also be methods to make a deposit (add to the balance) or withdrawal (subtract from the balance).
Finally, there should be a method that adds interest to the balance at the current interest rate. This function should have a parameter indicating how many years’ worth of interest are to be added (For example, 0.5 years indicate the account should have six months’ interest added).
Use the class as part of an interactive program that allows the user to determine how long it will take an initial balance will take to grow to a given value. The program should allow the user to specify the initial balance, the interest rate, and whether there are additional yearly deposits
Explanation / Answer
public class BankAccount
{
private float balance ;
private float irate;
//default constructor of class
public BankAccount()
{
this.balance = 0;
this.irate = 0;
}
//funmction defination for displaying Available Balance
public void getBalance()
{
System.out.println("Available Balance " + balance);
}
//funmction defination for change balance
public void changeBalance(float newbalance)
{
balance=newbalance;
System.out.println("Balance Changed to " + balance);
}
//funmction defination for displaying interest rate
public void getInterest()
{
System.out.println("Interest Rate " + irate);
}
//funmction defination for changing interest rate
public void changeInterest(float newirate)
{
irate=newirate;
System.out.println("Interest Rate Changed to " + irate);
}
//funmction defination for deposit amount
public void deposit(float addbal)
{
balance=balance+addbal;
System.out.println("After"+addbal + " Rs/- Adding new balance is" + balance);
}
//funmction defination for withdraw amount
public void withdraw(float withdrawbal)
{
balance=balance-withdrawbal;
System.out.println("After"+withdrawbal + " Rs/- withdraw new balance is" + balance);
}
//funmction defination for adding interest to main balance
public void addInterest(float years)
{
float itr=((balance*years*irate)/100);
balance=balance+itr;
System.out.println("Interest added to main balance is: "+ itr + " New balance is " +balance);
}
//funmction defination for calculating time to reach target balance
public void caltime(float targetbal)
{
float simpleinterest=targetbal-balance;
float targettime= (100*simpleinterest)/(balance*irate);
System.out.println("Target time to reach balnce is( in years)"+targettime);
}
public static void main(String []args)
{
BankAccount account1=new BankAccount(); //creating an object of BankAccount
account1.getBalance();
account1.getInterest();
account1.changeBalance(10000);
account1.changeInterest(1);
account1.withdraw(1000);
account1.deposit(11000);
account1.addInterest(1);
account1.caltime(21000);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.