Question

In: Computer Science

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 deposit(double amount) and withdraw(double amount) methods. If the amount is less than zero, the account balance remains untouched. If the amount is greater than the balance, withdraw() leaves the balance 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 an 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.

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

The CreditCardAccount class

This class inherits from Account and adds an apr property, which is the annual interest rate charged on the balance. It also has a 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, and credit limit.

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.

Add getters and setters for apr and creditLimit, leaving them untouched if the setter is given a negative value. Modify toString to include the interest rate and credit limit.

Add a calculatePayment() method that returns zero if the balance is positive and the minimum of 20 and (( apr/12) * (-balance )) ((apr/12)∗(−balance))((apr/12)∗(−balance)) if the balance is negative.

The Test Program

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

  • 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 zero, an APR of 7%, and a credit limit of $3,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()

The program will then print the interest to be paid for the savings account and the monthly payment due for the credit card accounts.

(( apr/12) * (-balance ))

Solutions

Expert Solution

Code:

import java.util.*;
import java.lang.*;
import java.io.*;
class Account
{
   protected long number;
   protected double balance;
   public Account()
   {
       number = 0L;balance = 0;
   }
   public Account(long number, long 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 amount)
   {
       balance += amount;
   }
   public void withdraw(double amount)
   {
       if(amount < 0 || amount > balance)
       {
           //System.out.println("Insufficient balance");
       }
       else
       {
           balance -= amount;
       }
   }
   public String toString()
   {
       return "Number: " + number + ", Balance: " + balance;
   }
   public double calculatePayment()
   {
       return 0;
   }
   public double calculateInterest()
   {
       return 0;
   }
}
class SavingsAccount extends Account
{
   double apr;
   public SavingsAccount()
   {
       number = 0;balance = 0;apr = 0;
   }
   public SavingsAccount(long number, double balance, double apr)
   {
       this.number = number;this.balance = balance;this.apr = apr;
   }
   public double getapr()
   {
       return apr;
   }
   public void setapr(double apr)
   {
       if(apr >= 0)
       {
           this.apr = apr;
       }
   }
   public String toString()
   {
       return "Number: " + number + ", Balance: " + balance + ", APR: " + apr;
   }
   public double calculateInterest()
   {
       return balance * apr / 100;
   }
}
class CreditCardAccount extends Account
{
   double apr;
   double creditLimit;
   public CreditCardAccount()
   {
       number = 0;balance = 0;apr = 0;creditLimit = 0;
   }
   public CreditCardAccount(long number, double balance, double apr, double creditLimit)
   {
       this.number = number;this.balance = balance;this.apr = apr;this.creditLimit = creditLimit;
   }
   public void withdraw(double amount)
   {
       if(Math.abs(balance - amount) > creditLimit)
       {
           //System.out.println("Insufficient funds");
       }
       else
       {
           balance -= amount;
       }
   }
   public double getapr()
   {
       return apr;
   }
   public void setapr(double apr)
   {
       if(apr >= 0)
       {
           this.apr = apr;
       }
   }
   public double getCreditLimit()
   {
       return creditLimit;
   }
   public void setCreditLimit(double creditLimit)
   {
       if(creditLimit > 0)
       {
           this.creditLimit = creditLimit;
       }
   }
   public String toString()
   {
       return "Number: " + number + ", Balance: " + balance + ", APR: " + apr + ", Credit Limit: " + creditLimit;
   }
   public double calculatePayment()
   {
       if(balance > 0) return 0;
       else
       {
           return Math.min(20, ((apr / 12) * (-balance)));
       }
   }
}
public class TestAccounts
{
   public static void main(String args[])
   {
       Account[] obj = new Account[5];//declaring 5 accout objects
       int i;
       obj[0] = new Account(1066L, 7500);//redeclaring each object
       obj[1] = new SavingsAccount(30507L, 4500, 1.5);
       obj[2] = new CreditCardAccount(51782737L, 7000, 8, 1000);
       obj[3] = new CreditCardAccount(629553328L, 1500, 7.5, 5000);
       obj[4] = new CreditCardAccount(4977201043L, 0, 7, 3000);
       for(i = 0; i < 5; i++)//run in a loop
       {
           obj[i].deposit(2134);
           obj[i].withdraw(4782);
           System.out.println(obj[i].toString());
       }
       System.out.println("Interest to be paid for account No. " + obj[1].getNumber() + " is: " + obj[1].calculateInterest());//savingsaccount
       for(i = 2; i < 5; i++)
       {
           System.out.println("Amount to be paid for account No. " + obj[i].getNumber() + " is: " + obj[i].calculatePayment());//creditcardaccount
       }
   }
}

Output:


Related Solutions

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 deposit(double amount) and withdraw(double amount) methods. If the amount is less than zero,...
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,...
The Account class Create a class named Account, which has the following private properties:
in java 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 (), setBalan 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...
The Account class Create a class named Account , which has the following private properties:
 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: getNunber(), getBalance(), setBalance (double newBalance) . There is no setNunber() - once an account is created, its account number cannot change. Now implement these methods: void deposit (double anount) and void withdraw(double anount). For both these methods, if the amount is less than zero,...
(In Matlab) Create a base class named Point that has the properties for x and y...
(In Matlab) Create a base class named Point that has the properties for x and y coordinates. From this class derive a class named Circle having an additional property named radius. For this derived class, the x and y properties represent the center coordinates of a circle. The methods of the base class should consist of a constructor, an area function that returns 0, and a distance function that returns the distance between two points. The derived class should have...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor 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...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (using name & id), and - keeps track of a user's available balance. - how many transactions (deposits and/or withdrawals) are made. public class BankAccount { private int id; private String name private double balance; private int numTransactions; // ** Please define method definitions for Accessors and Mutators functions below for the class private member variables string getName();...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate 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 account. A constructor...
Java programming. ** Create Account class include: 1/ private long   accountNumber; 2/ private String firstName; 3/...
Java programming. ** Create Account class include: 1/ private long   accountNumber; 2/ private String firstName; 3/ private String lastName; 4/ private double balance; 5/ public Account(long accountNumber, String firstName, String lastName, double balance); 6/ get/set methods for all attributes. 7/ public boolean deposit(double amount);   • deposit only if amount is greater than 10 but   less than 200, add the deposit   amount   to the   currentbalance. • if deposit is   successful return true,   otherwise return false; 8/ public boolean withdrawal(double amount); •...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT