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

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...
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...
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...
how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
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...
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
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000 (the insurance limit of the U.S. government). The method should block until sufficient money has been withdrawn by another thread. Test your program with a large number of deposit threads. (All other classes are provided below) Bank Account.java (This class is the one that needs to be modified) /** A bank account has a balance that can be changed by deposits and withdrawals. */...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000 (the insurance limit of the U.S. government). The method should block until sufficient money has been withdrawn by another thread. Test your program with a large number of deposit threads. Bank account class: (THE ONE THAT NEEDS TO BE EDITED) /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount { private double balance; /**...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT