Question

The Account class Create a class named Account, which has the following private properties: number: long...

The Account class

Create a class named Account, which has the following private properties:

number: long
balance: double
Create a no-argument constructor that sets the number and balance to zero.

Create a two-parameter constructor that takes an account number and balance.

First, implement getters and setters: getNumber(), getBalance(), setBalance(double newBalance). There is no setNumber() -- once an account is created, its account number cannot change.

Now implement these methods: void deposit(double amount) and void withdraw(double amount). For both these methods, if the amount is less than zero, the account balance remains untouched. For the withdraw() method, if the amount is greater than the balance, it remains untouched.

Then, implement a toString method that returns a string with the account number and balance, properly labeled.

The SavingsAccount class

This class inherits from Account and adds a private apr property, which is the annual percentage rate for interest. Write a no-argument constructor that sets the account number, balanace, and APR to zero. Write a three-argument constructor that takes an account number, balance, and interest rate as a decimal (thus, a 3.5% interest rate is given as 0.035).

Add a getter and setter method for apr. The setter should leave the APR untouched if given a negative value. Write a calculateInterest() method that returns the annual interest, calculated as the current balance times the annual interest rate. Modify toString() to include the interest rate and annual interest.

The CreditCardAccount class

This class inherits from Account and adds a private apr property, which is the annual interest rate charged on the balance. It also has a private creditLimit property (double) which gives the credit limit for the card. Write a no-argument constructor that sets all the properties to zero. Write a four-argument constructor that takes an account number, balance, interest rate as a decimal (thus, a 3.5% interest rate is given as 0.035), and credit limit. Write getters and setters for the apr and creditLimit.

Override the withdraw() function so that you can have a negative balance. If a withdrawal would push you over the credit limit, leave the balance untouched. Examples:

If your balance is $300 with a credit limit of $700, you can withdraw $900 (leaving a balance of $-600).
If your balance is $-300 with a credit limit of $700, you can withdraw $350 (leaving a balance of $-650).
If your balance is $-300 with a credit limit of $700, you can not withdraw $500, because that would then owe $800, which is more than your $700 limit.
In short, the maximum amount you can withdraw is your current balance plus the credit limit.

Add a calculatePayment() method that works as follows:

If the balance is positive, the minimum amount you have to pay on your card per month is zero.
Otherwise, your monthly payment is the minimum of 20 and (???/12)⋅(−???????)
(
a
p
r
/
12
)
(
b
a
l
a
n
c
e
)
(???/12)⋅(−???????)
(
a
p
r
/
12
)
(
b
a
l
a
n
c
e
)
Modify toString() to include the interest rate, credit limit, and monthly payment.

The Test Program

Write a program named TestAccounts that creates an array of five Account objects:

An Account number 1066 with a balance of $7,500.
A SavingsAccount number 30507 with a balance of $4,500 and an APR of 1.5%
A CreditCardAccount number 51782737 with a balance of $7,000.00, APR of 8%, and credit limit of $1000.00
A CreditCardAccount number 629553328 with a balance of $1,500.00, an APR of 7.5%, and a credit limit of $5,000
A CreditCardAccount number 4977201043 with a balance of -$5,000.00, an APR of 7%, and a credit limit of $10,000
Your program will use a loop to do the following for each account:

Deposit $2,134.00
Withdraw $4,782.00
Print the account status using toString()
Here is some sample output:

Account: 1066
Balance: $4852.00

Account: 30507
Balance: $1852.00
Interest Rate: 1.50%
Annual Interest: $27.78

Account: 51782737
Balance: $4352.00
Interest Rate: 8.00%
Credit Limit: $1000.00
Monthly Payment: $0.00

Account: 629553328
Balance: $-1148.00
Interest Rate: 7.50%
Credit Limit: $5000.00
Monthly Payment: $7.18

Account: 4977201043
Balance: $-7648.00
Interest Rate: 7.00%
Credit Limit: $10000.00
Monthly Payment: $20.00
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Account.java

public class Account {
private long number;
private double balance;

public Account(){
number = 0;
balance = 0.0;
}
  
public Account(long number, double balance) {
this.number = number;
this.balance = balance;
}
  
public long getNumber() {
return number;
}
  
public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public void deposit(double amt)
{
if(amt > 0)
balance += amt;
}
  
public void withdraw(double amt)
{
if(amt < balance)
balance -= amt;
}
  
@Override
public String toString() {
return "Account number=" + number + "\nBalance=" + balance;
}
  
  
}

SavingsAccount.java

public class SavingsAccount extends Account{
private double apr;

public SavingsAccount() {
super(); // calls the no-arg constructor of Account class
apr = 0.0;
}

public SavingsAccount(long number, double balance, double apr) {
super(number, balance); // calls the 2-arg constructor of Account class
this.apr = apr/100;
}

public double getApr() {
return apr;
}
  
public double calculateInterest(){
return apr * (super.getBalance());
}

@Override
public String toString() {
return super.toString() + "\nAnnual rate of Interest: " + apr*100 + "%\nAnnual interest: " + this.calculateInterest();
}
  
  
}

CreditCardAccount.java

public class CreditCardAccount extends Account{
private double apr;
private double creditLimit;
  
public CreditCardAccount(){
super(); // calls the no-arg constructor of Account class
this.apr = 0.0;
this.creditLimit = 0.0;
}

public CreditCardAccount(long number, double balance, double apr, double creditLimit) {
super(number, balance);
this.apr = apr/100;
this.creditLimit = creditLimit;
}

public double getApr() {
return apr;
}

public void setApr(double apr) {
this.apr = apr;
}

public double getCreditLimit() {
return creditLimit;
}

public void setCreditLimit(double creditLimit) {
this.creditLimit = creditLimit;
}
  
@Override
public void withdraw(double amt)
{
if(amt < (super.getBalance() + creditLimit))
{
// if the sum of credit limit and balance is less than the amount to withdraw, you can withdraw
super.setBalance(super.getBalance() - amt);
}
}
  
public double calculatePayment(){
double amount;
  
if(super.getBalance() > 0)
amount = 0;
else
amount = 20 + ((this.apr / 12) * (-super.getBalance()));
  
return amount;
}
  
@Override
public String toString(){
return super.toString() + "\nAnnual rate of interest: " + this.apr * 100 + "%\nCredit Limit: " + this.creditLimit + "\nMonthly payment: " + this.calculatePayment();
}
  
}

TestAccounts.java

public class TestAccounts {
public static void main(String... args){
Account[] accounts = new Account[5];
// since Account is the super class of SavingsAccount and CreditCardAccount
// it can hold the instances of SavingsAccount and CreditCardAccount
  
accounts[0] = new Account(1066, 7500);
accounts[1] = new SavingsAccount(30507, 4500, 1.5);
accounts[2] = new CreditCardAccount(51782737, 7000,8,1000);
accounts[3] = new CreditCardAccount(629553328, 1500, 7.5, 5000);
accounts[4] = new CreditCardAccount(497720104, -5000, 7, 10000);
  
// this is a foreach loop, it will take out objects from accounts array one by one
// store them in variable "account" and performs operations on them
  
for(Account account : accounts)
{
account.deposit(2134);
account.withdraw(4782);
  
System.out.println(account);
System.out.println();
}
  
}
}

Add a comment
Know the answer?
Add Answer to:
The Account class Create a class named Account, which has the following private properties: number: long...
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
  • 9.7 (The Account class) Design a class named Account that contains: • A private int data...

    9.7 (The Account class) Design a class named Account that contains: • A private int data field named id for the account (default o). • A private double data field named balance for the account (default o). • A private double data field named annualInterestRate that stores the current interest rate (default o). Assume that all accounts have the same interest rate. • A private Date data field named dateCreated that stores the date when the account was created. •...

  • Design a class named BankAccount that contains: 1. A private int data field named accountId for...

    Design a class named BankAccount that contains: 1. A private int data field named accountId for the account. 2. A private double data field named accountBalance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A private int data field named numberOfDeposits...

  • (The Account class) Design a class named Account that contains: A private int data field named...

    (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default...

  • The code is attached below has the Account class that was designed to model a bank account.

    IN JAVAThe code is attached below has the Account class that was designed to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. I need creat program using the 2 java programs.Create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.I need to write a test program that creates objects of Account,...

  • JAVA Create a class called SavingsAccount. The class should have a static variable named, annualInterestRate, to...

    JAVA Create a class called SavingsAccount. The class should have a static variable named, annualInterestRate, to store the annual interest rate for all account holders. Each object of the class should contain a private instance variable named, savingsBalance, indicating the amount the saver currently has on deposit. Write methods to perform the following: calculateMonthlyInterest – calculates the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12. This interest should be added to the balance. depositAmount – allows the...

  • In C++: Define a class named Account that stores a balance (monetary amount). The class should...

    In C++: Define a class named Account that stores a balance (monetary amount). The class should have a private double variable balance. Add one accessor getBalance function that returns the balance and one deposit function that increases the balance by a given double amount. Then subclass Account with a SavingsAccount class. This class should have a private double variable interest. Add an addInterest function that increases the balance (from the superclass Account) according to the interest. Demonstrate the use in...

  • PYTHON PROGRAMMING LANGUAGE Design a class named Savings Account that contains: A private int data field...

    PYTHON PROGRAMMING LANGUAGE Design a class named Savings Account that contains: A private int data field named id for the savings account. A private float data field named balance for the savings account. A private float data field named annuallnterestRate that stores the current interest rate. A_init_ that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). . The accessor and mutator methods for id, balance, and annuallnterestRate. A method...

  • C++ Create a class named SavingsAccount that contains the following: Three (3) private class variables: a...

    C++ Create a class named SavingsAccount that contains the following: Three (3) private class variables: a String called ‘name’, a double called “balance” and another double called “interestRate” and 4 function prototypes. A constructor that receives 3 parameters: a string, and 2 doubles; the constructor will use these parameters to initialize the name, the balance, and the interestRate respectively. setBalance: it receives a double parameter called theBalance and assigns it to the balance. getBalance: it returns the balance. calculateInterestAmount: it...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • Now create another class named SavingsAccount. This should be a subclass of BankAccount. A SavingsAccount should...

    Now create another class named SavingsAccount. This should be a subclass of BankAccount. A SavingsAccount should have an interest rate that is specified in its constructor. Furthermore, there should be a method, addInterest, that deposits interest into account, based on the account balance and interest rate. Create another subclass of BankAccount named CheckingAccount. A CheckingAccount should keep track of the number of transactions made (deposits and withdrawals). Name this field transactionCount. Also, the set fee for transactions is three dollars,...

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