Question
Pre-lab Questions: Answer these questions before your lab session (No submission required). 1. What is a class constructor used for? 2. How do constructors differ
Pre-lab Questions: Answer these questions before your lab session (No submission required).
1. What is a class constructor used for? 2. How do constructors differ from other methods in a class? 3. What is the difference between private and public attributes and methods in a class? Use the class below to answer questions 4 through 6. public class Sample { public Sample(int data1, String data2, double data3) { attrib1 = data1; nameObject = data2; attrib2 = data3; } private int someMethod() { //This method will return an integer value return attrib1; } public double calcArea() { //This method does not accept any arguments return PI*attrib2*attrib2; } public double calcAreaWithRad(double rad) { //This method accepts one integer argument return PI*rad*rad; } public int attrib1; public double attrib2; private String nameObject; private final int PI = 3.14; } 4. Which attributes could be directly accessed from outside the class? 5. Which methods could only be called from inside the class? 6. Write a line of code to create an object instance of the Sample class using the constructor defined in the class, passing it three appropriate parameter values. CSE 110 Lab 7: Object design and writing classes Lab Exercise: Complete this portion of the lab during your lab session. Lab Exercise: This portion of the lab should be completed during your lab session. The exercise for this week is to write a class that simulates managing a simple bank account. The account is created with an initial balance. It is possible to deposit and withdraw funds, to add interest, and to find out the current balance. This should be implemented in class named Account that includes: A default constructor that sets the initial balance to zero, accountNumber to zero, ownerName to an empty string, and the interestRate to zero. Other constructor that takes initial balance, accountNumber, and the ownerName at the time of object creation A function getBalance that returns the current balance. A method deposit for depositing a specified amount. A method withdraw for withdrawing a specified amount. A method addInterest for adding interest to the account. The addInterest method changes the balance in the account to balance*(1+interestRate). - displayAccountSummary method should display the accountNumber, ownerName, balance, and the interestRate. The UML diagram for the Account class is shown in the following UML diagram. The Account Class BankAccount - -ownerName:string -interestRate:double balance: double -acountNumber : int +double getBalance (); +void deposit(double ); +void withdraw(double ); +void addInterest(); +BankAccount(); +BankAccount(double, double, int, string) + displayAccountSummary(); Then , include Your BankAccount class in the following program, compile and run. It should produce the following output. #include using namespace std; // To do : copy and paste your BankAccount implementation here int main() { BankAccount myAccount(1000.50,0.05,1111, John William); myAccount.deposit(500); myAccount.withdraw(200); myAccount.addInterest(); myAccount. displayAccountSummary(); return 0; } Output: Account Number : 1111 Owners Name : John William Balance : 1300.50 Interest rate : 5%
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started