Question

In: Computer Science

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.

  1. Create a class BankAccount to represent a bank account according to the following requirements:
  1. A bank account has three attributes: accountnumber, balance and customer name.
  2. 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.
  3. Add a constructor with three parameters to initialize all the attributes by specific values.
  4. Add a constructor that allow to initialize the balance at a given number and the customer name with empty string and the account number by zero.
  5. Add all the required setter methods
  6. Add all the required getter methods
  7. Add the method withdraw
  8. Add the method deposit
  9. Add the method reset that change the balance to zero

  1. Create a tester class. In the main method:
  1. Create one bank account using the first constructor and print its characteristics.
  2. Create one bank account using the second constructor and print its characteristics.
  3. Create one bank account using the third constructor and print its characteristics.
  4. Deposit 1000 in each banc account.
  5. Withdraw 500 from the first bank account, 200 from the second one.
  6. Change the customer name of the third account.

Solutions

Expert Solution

Program

//class BankAccount
class BankAccount
{
   //data members
   int accountNumber;
   double balance;
   String customerName;
  
   //constructor without parameters
   public BankAccount()
   {
       accountNumber = 0 ;
       balance = 0;
       customerName = "";
   }
  
   //constructor with three parameters
   public BankAccount(int accountNumber, double balance, String customerName)
   {
       this.accountNumber = accountNumber;
       this.balance = balance;
       this.customerName = customerName;
   }
  
   //constructor that allow to initialize the balance at a given number
   public BankAccount(double balance)
   {
       this.accountNumber = 0;
       this.balance = balance;
       customerName = "";
   }  
  
   //setter methods
   public void setAccountNumber(int accountNumber)
   {
       this.accountNumber = accountNumber;
   }
  
   public void setBalance(double balance)
   {
       this.balance = balance;
   }
  
   public void setCustomerName(String customerName)
   {
       this.customerName = customerName;
   }
  
   //getter methods
   public int getAccountNumber()
   {
       return accountNumber;
   }
  
   public double getBalance()
   {
       return balance;
   }
  
   public String getCustomerName()
   {
       return customerName;
   }
  
   //method withdraw
   public void withdraw(double amount)
   {
       if(balance>amount)
           balance -= amount;
       else
           System.out.println("Not sufficient balance");
   }
  
   //method deposit
   public void deposit(double amount)
   {
       balance += amount;
       System.out.println("Balance = " + balance);
   }
  
   //method reset that change the balance to zero
   public void resetBalance()
   {
       balance = 0 ;
   }
  
   public String toString()
   {
       return "Account Number = " + accountNumber +", Balance = $" +
           balance + ", Customer Name = " + customerName;
   }
}

//tester class
class Tester
{
   // main method
   public static void main (String[] args)
   {
       //Create one bank account using the first constructor and print its characteristics.
       BankAccount ba1 = new BankAccount();
       System.out.println(ba1);
      
       //Create one bank account using the second constructor and print its characteristics.      
       BankAccount ba2 = new BankAccount(118897, 50000, "John");
       System.out.println(ba2);
      
       //Create one bank account using the third constructor and print its characteristics.
       BankAccount ba3 = new BankAccount(2500);
       System.out.println(ba3);
      
       //Deposit 1000 in each bank account
       ba1.deposit(1000);
       ba2.deposit(1000);
       ba3.deposit(1000);
      
       //Withdraw 500 from the first bank account, 200 from the second one.
       ba1.withdraw(500);
       ba2.withdraw(200);
      
       //Change the customer name of the third account
       ba3.setCustomerName("Johny");
      
   }
}


Output

Account Number = 0, Balance = $0.0, Customer Name =
Account Number = 118897, Balance = $50000.0, Customer Name = John
Account Number = 0, Balance = $2500.0, Customer Name =
Balance = 1000.0
Balance = 51000.0
Balance = 3500.0


Related Solutions

how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
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();...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following: 1. Name the class SalonServices 2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type 3. Create two methods that will set the field values. a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static b....
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods designed to perform simple conversions. Specifically, you will be writing methods to convert temperature between Fahrenheit and Celsius and length between meters and inches and practicing overloading methods. See the API document for the Conversion class for a list of the methods you will write. Also, because all of the methods of the Conversion class will be static, you should ensure that it is...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file,...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file, read them back in, and format a report. Project Description You’ll read and write files containing objects of the Loan class. Here are the details of that class: Instance Variables: customer name (String) annual interest percentage (double) number of years (int) loan amount (double) loan date (String) monthly payment (double) total payments (double) Methods: getters for all instance variables setters for all instance variables...
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
How do objects enhance Java? How do objects relate to classes and methods?
How do objects enhance Java? How do objects relate to classes and methods?
Create a class BankAccount to hold at least the following data / information about a bank...
Create a class BankAccount to hold at least the following data / information about a bank account: Account balance Total number of deposits Total number of withdrawals Interest rate e.g., annual rate = 0.05 Service charges per month The class should have the following member functions: Constructor To set the required data. It may be parameterized or user’s input. depositAmount A virtual function used to accept an argument for the amount to be deposited. It should add the argument (amount)...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT