Question

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 current balance to decrease and toString() method returns account name and the current balance separated by a comma. For example if name is Jake and balance is 40.0 it should return

Jake, $40.00

public class BankAccount {

    private String name;

   

    public void deposit(double amount) {

        balance = balance + amount;

    }

    public void withdraw(double amount) {

       

    }

       

}

Write a client program called BankAccountClient that creates a BankAccount object called B1 and assigns “John” to its name field and 1000 to balance field. Call the deposit method to deposit 500 to this account and call the display method to display the current balance. Then, withdraw 300 from this account and print the object using println. Create another object called B2 without passing parameters and display the name and current balance for this object.

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

SOURCE CODE:

BankAccount.java

public class BankAccount {
private String name;
private double balance;
  
public BankAccount() {
this.name = null;
this.balance = 0.0;
}

public BankAccount(String name, double balance) {
this.name = name;
this.balance = balance;
}

public double getBalance() {
return balance;
}

public String getName() {
return name;
}

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

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

public String toString() {
return name + ",$" + balance;
}
  
public void display()
{
System.out.println(this.toString());
}
}

BankAccountClient.java


public class BankAccountClient {
public static void main(String args[])
{
BankAccount B1 = new BankAccount("John",1000);
B1.deposit(500);
B1.display();
B1.withdraw(300);
System.out.println(B1);
BankAccount B2 = new BankAccount();
B2.display();
}
}

OUTPUT:

John,$1500.0
John,$1200.0
null,$0.0

Add a comment
Know the answer?
Add Answer to:
Write a Java class called BankAccount (Parts of the code is given below), which has two...
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
  • 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...

  • [JAVA] < Instruction> You are given two classes, BankAccount and ManageAccounts ManageAccounts is called a Driver...

    [JAVA] < Instruction> You are given two classes, BankAccount and ManageAccounts ManageAccounts is called a Driver class that uses BankAccount. associate a member to BankAcount that shows the Date and Time that Accounts is created. You may use a class Date. To find API on class Date, search for Date.java. Make sure to include date information to the method toString(). you must follow the instruction and Upload two java files. BankAccount.java and ManageAccounts.java. -------------------------------------------------------------------------- A Bank Account Class File Account.java...

  • C++ Please create the class named BankAccount with the following properties below: // The BankAccount class...

    C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (name & ID), keeps track of a user's available balance. // It also keeps track of how many transactions (deposits and/or withdrawals) are made. public class BankAccount { Private String name private String id; private double balance; private int numTransactions; // Please define method definitions for Accessors and Mutator functions below Private member variables string getName(); // No set...

  • 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...

  • Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount....

    Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount. Implement four methods: • public void deposit(double amount, String account) • public void withdraw(double amount, String account) • public void transfer(double amount, String account) • public double getBalance(String account) Here the account string is "S" or "C". For the deposit or withdrawal, it indicates which account is affected. For a transfer, it indicates the account from which the money is taken; the money is...

  • 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...

  • 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...

  • 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...

  • Requirements:  Your Java class names must follow the names specified above. Note that they are...

    Requirements:  Your Java class names must follow the names specified above. Note that they are case sensitive. See our template below.  BankAccount: an abstract class.  SavingAccount: a concrete class that extends BankAccount. o The constructor takes client's firstname, lastname and social security number. o The constructor should generate a random 6-digit number as the user account number. o The initial balance is 0 and the annual interest rate is fixed at 1.0% (0.01).o The withdraw method signature...

  • For this lab you must write a complete class in C++. This will be a class...

    For this lab you must write a complete class in C++. This will be a class named BankAccount. This class must have the following private variables: 1. accountHolderName : string 2. balance : double 3. interestRate: double This class must have the following public constructor: 1. BancAccount(name : string, balance : double, rate : double) This class must have the following public member functions: 1. getAccountHolderName() : string a. returns the account holders name 2. getBalance() : double a. returns...

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