In: Computer Science
Problem 1:
In java
Create a program to calculate interest rate and total balance - the program consists of
(a) A class that encapsulates the interest rate and total balance calculation
(b) A test program that uses class from step (A) to perform the following:
(a)
//Account.java
public class Account
{
//variables to store total balance and interest
rate
private double total_balance;
private double interest_rate;
/*Constructor to set total balance and interest rate
value*/
public Account(double initial_balance, double
interest_rate)
{
this.total_balance=initial_balance;
//convert interest_rate to decimal
value
this.interest_rate=interest_rate/100.0;
}
public void setInterest_rate(double
interest_rate)
{
this.interest_rate =
interest_rate/100.0;
}
public void setInitial_balance(double
initial_balance)
{
this.total_balance =
initial_balance;
}
/*Return interest value to integer value*/
public double getInterest_rate()
{
return interest_rate*100.0;
}
public double getInterestAmount()
{
return
total_balance*interest_rate;
}
/**The method getTotalBalance returns the total
balance */
public double getTotalBalance()
{
total_balance=total_balance+
getInterestAmount();
return total_balance;
}
} //end of the class Account
-------------------------------------------------------------------------------------------------------------------------------------
(b)
/* * The java program that creates an instance of Account class with initial balance and interest rate.
Then print the rate of interest, interest amount and total balance .* */
//TestProgram.java
public class TestProgram
{
public static void main(String[] args)
{
//Set interest rate to 5% and
initial balance to 1000
Account account= new Account(1000,
5);
System.out.printf("Interest rate :
%.2f %%\n",account.getInterest_rate());
//Print interest amount and total
balance
System.out.printf("Interest amount
: %.2f Total Balance: %.2f\n",
account.getInterestAmount(),account.getTotalBalance());
//Increase interest rate to
10%
account.setInterest_rate(10);
System.out.printf("Interest rate :
%.2f %%\n",account.getInterest_rate());
//Print interest amount and total
balance
System.out.printf("Interest amount
: %.2f Total Balance: %.2f\n",
account.getInterestAmount(),account.getTotalBalance());
//Increase initial balance to
2000
account.setInitial_balance(2000);
System.out.printf("Interest rate :
%.2f %%\n",account.getInterest_rate());
//Print interest amount and total
balance
System.out.printf("Interest amount
: %.2f Total Balance: %.2f\n",
account.getInterestAmount(),account.getTotalBalance());
}//end of main method
} //end of the class
------------------------------------------------------------------------------------------------------------
Sample Output:
Interest rate: 5.00 %
Interest amount: 50.00 Total Balance: 1050.00
Interest rate: 10.00 %
Interest amount: 105.00 Total Balance: 1155.00
Interest rate: 10.00 %
Interest amount: 200.00 Total Balance:
2200.00