Question

BankAccount Class Copy Constructor UML as shown below +-------------------------------------- + BankAccount +-------------------------------------- - balance: double +--------------------------------------...

BankAccount Class Copy Constructor
UML as shown below
+--------------------------------------
+ BankAccount
+--------------------------------------
- balance: double
+--------------------------------------
+ BankAccount(startBalance:double)
+ BankAccount(anotherAccount: BankAccount)
+ toString(): String

CSCI-185 Prof. M. Kadri

Page 2 of 5

+--------------------------------------
Add a copy constructor to the BankAccount class. This constructor should accept a
BankAccount object as an argument. It should assign to the balance field the value in
the argument's balance field. As a result, the new object will be a copy of the argument
object.
Write a driver program to demonstrate that your copy constructor works. How?

Homework Answers

Answer #1

BankAccount.java

public class BankAccount {
  
   // Data member
   private double balance;
  
   // Constructor to initialize balance
   public BankAccount(double balance) {
       this.balance = balance;
   }
  
   // Copy Constructor
   public BankAccount(BankAccount ba) {
       this.balance = ba.balance;
   }
  
   // This method return BankAccount details in string format
   public String toString() {
       return "Balance: "+balance;
   }
}

BankAccountTest.java

public class BankAccountTest {

   public static void main(String[] args) {
       // Create object of BankAccount by passing balance
       BankAccount ba1 = new BankAccount(1500);
       // Create object of BankAccount by copy constructor
       BankAccount ba2 = new BankAccount(ba1);
      
       // Print bankAccounts
       System.out.println("Bank Account 1: "+ba1);
       System.out.println("Bank Account 2: "+ba2);  
   }
}

OUTPUT

Know the answer?
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
Similar Questions
Java... Write a class named TestScores. The class constructor should accept an array of test scores...
Java... Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to...
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...
Problem Description: Problem Description: (The Account class) Design a class named Account that contains: A private...
Problem Description: Problem Description: (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor...
C# Step 1: Create a Windows Forms Application. Step 2: Create a BankAccount class. Include: Private...
C# Step 1: Create a Windows Forms Application. Step 2: Create a BankAccount class. Include: Private data fields to store the account holder's name and the account balance A constructor with 0 arguments A constructor with 1 argument (account holder's name) A constructor with 2 arguments(account holder's name and account balance) Public properties for the account holder's name and the account balance. Do not use auto-implemented properties. A method to increase the balance (deposit) A method to decrease the balance...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and...
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and make a java program for your bank. However, there is another type of customer that may not have money in the bank and may not in fact have a back account with this bank at all, but may have a loan here. Make another class in the application you are making called CarLoan. Carloan's should have a name, amount owed, a rate, and a...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....