Question

In: Computer Science

In java, I have problem how to declare BankAccount Account = new BasicAccount(100.00); Given the class...

In java, I have problem how to declare BankAccount Account = new BasicAccount(100.00);

Given the class BankAccount , implement a subclass of BankAccount called BasicAccount whose withdraw method will only withdraw money if there is enough money currently in the account (no overdrafts allowed). Assume all inputs to withdraw and the constructor will be valid non-negative doubles.

public class BankAccount {
    private double balance;

    public BankAccount() {
        balance = 0;
    }


    public BankAccount(double initialBalance) {
        balance = initialBalance;
    }


    public void deposit(double amount) {
        double newBalance = balance + amount;
        balance = newBalance;
    }


    public void withdraw(double amount) {
        double newBalance = balance - amount;
        balance = newBalance;
    }


    public double getBalance() {
        return balance;
    }
}

public class Main {

    public static void main(String []args) {
BankAccount account = new BasicAccount(100.00); // creates a BasicAccount with an initial balance of 100.00

account.withdraw(80.00);

account.getBalance(); // returns 20.0

account.withdraw(50.00); // amount won't be withdrawn since it's greater than balance

account.getBalance(); // returns 20.0
    }
}
 

Solutions

Expert Solution

The class BasicAccount will extend the class BankAccount.

Since, balance is a private member in class BankAccount, a setter method is also required in the BankAccount class.

Therefore, the complete definition of class BankAccount is as follows:

BankAccount.java

public class BankAccount {
    private double balance;
    
    //default constructor
    public BankAccount() {
        balance = 0;
    }

    //for initializing the balance
    public BankAccount(double initialBalance) {
        balance = initialBalance;
    }

    //for depositing in the account
    public void deposit(double amount) {
        double newBalance = balance + amount;
        balance = newBalance;
    }

    //for withdrawing amount from the account
    public void withdraw(double amount) {
        double newBalance = balance - amount;
        balance = newBalance;
    }

    //for getting the value of balance
    public double getBalance() {
        return balance;
    }
    
    //setter method for setting value of balance
    public void setBalance(double b){
        balance = b;
    }
}

The subclass BasicAccount will define the method withdraw() such that the amount gets withdrawn only when there is sufficient balance.

The constructor calls the super constructor for initialization.

The defintion of class BasicAccount is as follows:

BasicAccount.java

//subclass BasicAccount extends BankAccount class
//the constructor calls the super constructor
class BasicAccount extends BankAccount{
    
    //constructor calls the super constructor
    public BasicAccount(double initialBalance){
        super(initialBalance);
    }
    
    //withdraw method
    //withdraw only when there is sufficient balance
    public void withdraw(double amount) {
        double newBalance = getBalance() - amount;
        
        if (newBalance >= 0)
        setBalance(newBalance);
    }
}

The main class for showing the object initialzation and result after calling the withdraw() method is as follows:

Main.java

public class Main {

    public static void main(String []args) {
BankAccount account = new BasicAccount(100.00); // creates a BasicAccount with an initial balance of 100.00

account.withdraw(80.00);

System.out.println(account.getBalance()); // returns 20.0

account.withdraw(50.00); // amount won't be withdrawn since it's greater than balance

System.out.println(account.getBalance()); // returns 20.0
    }
}

The output after the main class is executed is as follows:


Related Solutions

1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account...
1.   Design a class called BankAccount. The member fields of the class are: Account Name, Account Number and Account Balance. There are also other variables called MIN_BALANCE=9.99, REWARDS_AMOUNT=1000.00, REWARDS_RATE=0.04. They look like constants, but for now, they are variables of type double Here is the UML for the class:                                                         BankAccount -string accountName // First and Last name of Account holder -int accountNumber // integer -double accountBalance // current balance amount + BankAccount()                     //default constructor that sets name to “”,...
How can I write a separate java class to change a given name or a given...
How can I write a separate java class to change a given name or a given email of a user that was already saved (keep in mind that there may be numerous names and emails). Note: It should change the name or email that is saved in the txt file. Here is my code so far: User class public class User { private String fName; private String lName; private String UserEmail; public User(String firstName, String lastName, String email) { this.fName...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
In C++, define the class bankAccount to implement the basic properties of a bank account. An...
In C++, 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 functions to manipulate an object. Use a static member in the class to automatically assign account numbers. Also declare an array of 10 components of type bankAccount to...
Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
13. The class bankAccount stores a bank customer’s account number and balance. Suppose that account number...
13. The class bankAccount stores a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. This class provides the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. b. The class  checkingAccount from the class bankAccount (designed in part a). This class inherits members to store the account number and the balance from the base class. A customer...
Write a program to create a bank account and to process transactions. Call this class bankAccount...
Write a program to create a bank account and to process transactions. Call this class bankAccount A bank account can only be given an initial balance when it is instantiated. By default, a new bank account should have a balance of 0. A bank account should have a public get method, but no public set method. A bank account should have a process method with a double parameter to perform deposits and withdrawals. A negative parameter represents a withdrawal. It...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public static void main (String args []) { System.out.println("File:../SmallAreaIncomePovertyEstData.text"); System.out.println("Id" + "\t" + "Population" + "\t" + "ChildPop" + "\t" + "CPovPop" + "\t" + "CPovPop%"); }// read the data try (FileReader fr = new FileReader("File: C:\\605.201/SmallAreaIncomePovertyEstData.text")) { int c; while (( c = fr.read())!= -1){ System.out.print((char) c); } } catch(IOException e) { System.out.println("I/O Error" + e); }    } Please help to fix
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT