public class BankAccount private double balance public Bank

public class BankAccount

{

   private double balance;

   public BankAccount()

   {

      balance = 0;

   }

   public BankAccount(double initialBalance)

   {

      balance = initialBalance;

   }

   public void deposit(double amount)

   {

      balance = balance + amount;

   }

   public void withdraw(double amount)

   {

      balance = balance - amount;

   }

   public double getBalance()

   {

      return balance;

   }

}

Look at the code above, and identify the following:

Constructor

Field

Method

Primitive Type

Instance Variable

The class above is not a complete Java program that could execute. What would you do to make it execute? Change the program, or add a source code file, to make it into a Java program that executes.

Suggest two other improvements you would make to this code.

Solution

Hi, Please let me know in case of any issue.

public class BankAccount

{

   private double balance;

   public BankAccount()

   {

       balance = 0;

   }

   public BankAccount(double initialBalance)

   {

       balance = initialBalance;

   }

   public void deposit(double amount)

   {

       balance = balance + amount;

   }

   public void withdraw(double amount)

   {

       balance = balance - amount;

   }

   public double getBalance()

   {

       return balance;

   }

}

/*

Constructor :

   1. Default Constructor: BankAccount()

   2. Parameterized Constructor: BankAccount(double initialBalance)

  

Field

   double balance

  

Method

   1. void deposit(double amount)

   2. void withdraw(double amount)

   3.   double getBalance()

  

Primitive Type

   double

  

Instance Variable

   balance

*/

public class BankAccoutDriver {

  

   public static void main(String[] args) {

      

       // creating an Object of BankAccout

       BankAccount account = new BankAccount(342.12);

      

       System.out.println(\"Balance: \"+account.getBalance());

      

       account.deposit(43.23);

      

       System.out.println(\"Balance: \"+account.getBalance());

      

   }

}

/*

Sampel run:

Balance: 342.12

Balance: 385.35

*/

Improvement:

1. in withdraw mwthod: we should not allow user to withdraw if withdrawing amount is greater than balance

2. We should have a field called: Account Number in BankAccount class

public class BankAccount { private double balance; public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; }
public class BankAccount { private double balance; public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; }
public class BankAccount { private double balance; public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; }
public class BankAccount { private double balance; public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; }

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site