Question

WRITE A UNIT TEST CLASS THAT THOROUGHLY TESTS AN (IMAGINARY) BankAccount class and at least six...

WRITE A UNIT TEST CLASS THAT THOROUGHLY TESTS AN (IMAGINARY) BankAccount class and at least six BankAccount methods, plus its constructor. You must supply at least 20 tests. Then write code to pass them.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ource Code:

BankAccount.java

import java.lang.*;

public class BankAccount
{
//Data fields
private double balance; //Account Balance
private double annualInterestRate; //Account annual interest rate
private double monthlyInterestRate;
private double totalDeposits;
private double totalWithdraws;
private double totalInterest;

/**
* Constructor
* @param startBalance The account's balance.
* @param annual_Interest_Rate The annual interest rate.
*/
public BankAccount(double startBalance, double annual_Interest_Rate)
{
balance = startBalance;
annualInterestRate = annual_Interest_Rate;
}
//end of Constructor

/**
* setAnnualInterestRate method sets the annual interest
* rate and calculates the monthly interest rate
* @param annual_Interest_Rate The annual interest rate.
*/
public void setAnnualInterestRate(double annual_Interest_Rate)
{
monthlyInterestRate = annualInterestRate / 12;
}
//end of setAnnualInterestRate method

/**
* The deposit method adds the amount to the balance
* and calculates the total deposit
* @param amount
*/
public void setDeposit(double amount)
{
balance += amount;
totalDeposits += amount;
}
//end of deposit method

/**
* The withdraw method subtracts the amount to the balance
* and calculates the total withdraws
* @param amount
*/
public void setWithdraw(double amount)
{
balance -= amount;
totalWithdraws += amount;
}
//end of withdraw method

/**
* The calculateMonthlyInterest method calculates the total
* interest and adds the monthly interest to the balance
*/
public void calculateMonthlyInterest()
{
totalInterest = totalInterest + balance * monthlyInterestRate;
balance = balance + balance * monthlyInterestRate;
}
//end of calculateMonthlyInterest method

/**
* The getBalance method returns the account's balance.
* @return The value of the balance field.
*/
public double getBalance()
{
return balance;
}

/**
* The getAnnualInterestRate method returns the annual interest rate.
* @return The value of the annual interest rate field.
*/
public double getAnnualInterestRate()
{
return annualInterestRate;
}

/**
* The getMonthlyInterestRate method returns the monthly interest rate.
* @return The value of the monthly interest rate field.
*/
public double getMonthlyInterestRate()
{
return monthlyInterestRate;
}

/**
* The getTotalDeposits method returns the total deposit amount.
* @return The value of the total deposits field.
*/
public double getTotalDeposits()
{
return totalDeposits;
}

/**
* The getTotalWithdraws method returns the total withdraws amount.
* @return The value of the total withdraws field.
*/
public double getTotalWithdraws()
{
return totalWithdraws;
}

/**
* The getTotalInterest method returns the total interest amount.
* @return The value of the total interest field.
*/
public double getTotalnterest()
{
return totalInterest;
}

/* displayData method displays the ending details of the savings account */
public void displayData()
{
balance = balance * 100.0 / 100.0;
totalInterest = totalInterest * 100.0 / 100.0;
System.out.println();
System.out.println("The ending balance is: $" + balance);
System.out.println("Total amount of deposits: $" + totalDeposits);
System.out.println("Total amount of withdraws: $" + totalWithdraws);
System.out.println("Total interest earned: $" + totalInterest);
}
//end of displayData method
}
//end of BankAccount class







//BankAccountTest.java

import java.util.Scanner;
import java.lang.Math;

public class BankAccountTest
{
public static void main(String[] args)
{
double startBalance;
double annual_Interest_Rate;
int months;
double deposit_Amount;
double withdraw_Amount;

//Create an object for Scanner class
Scanner input = new Scanner(System.in);

//Prompt user for starting balance
System.out.print("Enter starting balance: $");
startBalance = input.nextDouble();

//Prompt user for annual interest rate
System.out.print("Enter annual interest rate: ");
annual_Interest_Rate = input.nextDouble();

//Prompt user for number of months
System.out.print("Enter the number of months: ");
months = input.nextInt();

/* Create an object for SavingsAccount class */
BankAccount sa = new
BankAccount(startBalance, annual_Interest_Rate);

//Call to setAnnualInterestRate method
sa.setAnnualInterestRate(annual_Interest_Rate);

//Loop
for (int i = 0; i < months; i++)
{
/* Prompt user for deposit amount */
System.out.print("Enter amount to deposit for the month " + (i+1) + ":$");
deposit_Amount = input.nextDouble();

//Call to deposit method
sa.setDeposit(deposit_Amount);

/* Prompt user for amount to withdraw */
System.out.print("Enter amount to withdraw for the month " + (i+1) + ":$");
withdraw_Amount = input.nextDouble();

//Call to withdraw method
sa.setWithdraw(withdraw_Amount);

/* Call to calculateMonthlyInterest method */
sa.calculateMonthlyInterest();

}
//end of loop

//Call to displayData method
sa.displayData();
}
//end of main method
}
//end of BankAccountTest class

OUTPUT:

CNN. Command Prompt C:\air>javac BankAccount Test.java C:\air>java BankAccount Test Enter starting balance : $1500 Enter annu

Hope I answered the question.


answered by: ANURANJAN SARSAM
Add a comment
Answer #2

Source Code:

BankAccount.java

import java.lang.*;

public class BankAccount
{
//Data fields
private double balance; //Account Balance
private double annualInterestRate; //Account annual interest rate
private double monthlyInterestRate;
private double totalDeposits;
private double totalWithdraws;
private double totalInterest;

/**
* Constructor
* @param startBalance The account's balance.
* @param annual_Interest_Rate The annual interest rate.
*/
public BankAccount(double startBalance, double annual_Interest_Rate)
{
balance = startBalance;
annualInterestRate = annual_Interest_Rate;
}
//end of Constructor

/**
* setAnnualInterestRate method sets the annual interest
* rate and calculates the monthly interest rate
* @param annual_Interest_Rate The annual interest rate.
*/
public void setAnnualInterestRate(double annual_Interest_Rate)
{
monthlyInterestRate = annualInterestRate / 12;
}
//end of setAnnualInterestRate method

/**
* The deposit method adds the amount to the balance
* and calculates the total deposit
* @param amount
*/
public void setDeposit(double amount)
{
balance += amount;
totalDeposits += amount;
}
//end of deposit method

/**
* The withdraw method subtracts the amount to the balance
* and calculates the total withdraws
* @param amount
*/
public void setWithdraw(double amount)
{
balance -= amount;
totalWithdraws += amount;
}
//end of withdraw method

/**
* The calculateMonthlyInterest method calculates the total
* interest and adds the monthly interest to the balance
*/
public void calculateMonthlyInterest()
{
totalInterest = totalInterest + balance * monthlyInterestRate;
balance = balance + balance * monthlyInterestRate;
}
//end of calculateMonthlyInterest method

/**
* The getBalance method returns the account's balance.
* @return The value of the balance field.
*/
public double getBalance()
{
return balance;
}

/**
* The getAnnualInterestRate method returns the annual interest rate.
* @return The value of the annual interest rate field.
*/
public double getAnnualInterestRate()
{
return annualInterestRate;
}

/**
* The getMonthlyInterestRate method returns the monthly interest rate.
* @return The value of the monthly interest rate field.
*/
public double getMonthlyInterestRate()
{
return monthlyInterestRate;
}

/**
* The getTotalDeposits method returns the total deposit amount.
* @return The value of the total deposits field.
*/
public double getTotalDeposits()
{
return totalDeposits;
}

/**
* The getTotalWithdraws method returns the total withdraws amount.
* @return The value of the total withdraws field.
*/
public double getTotalWithdraws()
{
return totalWithdraws;
}

/**
* The getTotalInterest method returns the total interest amount.
* @return The value of the total interest field.
*/
public double getTotalnterest()
{
return totalInterest;
}

/* displayData method displays the ending details of the savings account */
public void displayData()
{
balance = balance * 100.0 / 100.0;
totalInterest = totalInterest * 100.0 / 100.0;
System.out.println();
System.out.println("The ending balance is: $" + balance);
System.out.println("Total amount of deposits: $" + totalDeposits);
System.out.println("Total amount of withdraws: $" + totalWithdraws);
System.out.println("Total interest earned: $" + totalInterest);
}
//end of displayData method
}
//end of BankAccount class







//BankAccountTest.java

import java.util.Scanner;
import java.lang.Math;

public class BankAccountTest
{
public static void main(String[] args)
{
double startBalance;
double annual_Interest_Rate;
int months;
double deposit_Amount;
double withdraw_Amount;

//Create an object for Scanner class
Scanner input = new Scanner(System.in);

//Prompt user for starting balance
System.out.print("Enter starting balance: $");
startBalance = input.nextDouble();

//Prompt user for annual interest rate
System.out.print("Enter annual interest rate: ");
annual_Interest_Rate = input.nextDouble();

//Prompt user for number of months
System.out.print("Enter the number of months: ");
months = input.nextInt();

/* Create an object for SavingsAccount class */
BankAccount sa = new
BankAccount(startBalance, annual_Interest_Rate);

//Call to setAnnualInterestRate method
sa.setAnnualInterestRate(annual_Interest_Rate);

//Loop
for (int i = 0; i < months; i++)
{
/* Prompt user for deposit amount */
System.out.print("Enter amount to deposit for the month " + (i+1) + ":$");
deposit_Amount = input.nextDouble();

//Call to deposit method
sa.setDeposit(deposit_Amount);

/* Prompt user for amount to withdraw */
System.out.print("Enter amount to withdraw for the month " + (i+1) + ":$");
withdraw_Amount = input.nextDouble();

//Call to withdraw method
sa.setWithdraw(withdraw_Amount);

/* Call to calculateMonthlyInterest method */
sa.calculateMonthlyInterest();

}
//end of loop

//Call to displayData method
sa.displayData();
}
//end of main method
}
//end of BankAccountTest class

OUTPUT:

CNN. Command Prompt C:\air>javac BankAccount Test.java C:\air>java BankAccount Test Enter starting balance : $1500 Enter annu

Hope I answered the question.

If you have any doubts, or queries, feel free to ask

I'll respond to you as soon as I can.

Have a nice day

Add a comment
Know the answer?
Add Answer to:
WRITE A UNIT TEST CLASS THAT THOROUGHLY TESTS AN (IMAGINARY) BankAccount class and at least six...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Write a program that contains a BankAccount class. The BankAccount class should store the...

    In C++ Write a program that contains a BankAccount class. The BankAccount class should store the following attributes: account name account balance Make sure you use the correct access modifiers for the variables. Write get/set methods for all attributes. Write a constructor that takes two parameters. Write a default constructor. Write a method called Deposit(double) that adds the passed in amount to the balance. Write a method called Withdraw(double) that subtracts the passed in amount from the balance. Do not...

  • c++ please    Define the class bankAccount to implement the basic properties of a bank account....

    c++ please    Define the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder’s name (string), account number (int), account type (string, checking/saving), balance (double), and interest rate (double). (Store interest rate as a decimal number.) Add appropriate member func- tions to manipulate an object. Use a static member in the class to automatically assign account numbers. Also declare an array of 10 compo- nents of...

  • For this activity you will create unit tests using JUnit and some of the available features...

    For this activity you will create unit tests using JUnit and some of the available features of JUnit adhering to the Arrange, Act, Assert (AAA) unit testing methodology. Specifications: Select two different parts of your course project to unit test. You will create two separate test class files in JUnit in the appropriate area. Each JUnit test class must include the following: At least two @Test methods. Appropriate assertions. One or more JUnit test class must include the following: @Before...

  • For this activity you will create unit tests using JUnit and some of the available features...

    For this activity you will create unit tests using JUnit and some of the available features of JUnit adhering to the Arrange, Act, Assert (AAA) unit testing methodology. Specifications: Select two different parts of your course project to unit test. You will create two separate test class files in JUnit in the appropriate area. Each JUnit test class must include the following: At least two @Test methods. Appropriate assertions. One or more JUnit test class must include the following: @Before...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • Write the definition of the class Tests such that an object of this class can store...

    Write the definition of the class Tests such that an object of this class can store a student's first name, last name, five test scores, average test score, and grade. (Use an array to store the test scores.) Add constructors and methods to manipulate data stored in an object. Among other things, your class must contain methods to calculate test averages, return test averages, calculate grades, return grades, and modify individual test scores. The method toString must return test data...

  • Design a class named BankAccount containing the following data field and methods. • One double data...

    Design a class named BankAccount containing the following data field and methods. • One double data field named balance with default values 0.0 to denote the balance of the account. • A no-arg constructor that creates a default bank account. • A constructor that creates a bank account with the specified balance.  throw an IllegalArgumentException when constructing an account with a negative balance • The accessor method for the data field. • A method named deposit(double amount) that deposits...

  • C++ Design a class bankAccount that defines a bank account as an ADT and implements the...

    C++ Design a class bankAccount that defines a bank account as an ADT and implements the basic properties of a bank account. The program will be an interactive, menu-driven program. a. Each object of the class bankAccount will hold the following information about an account: account holder’s name account number balance interest rate The data members MUST be private. Create an array of the bankAccount class that can hold up to 20 class objects. b. Include the member functions to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT