Docsity

Problem in Exam - Discrete Structures for Computer Science | CS 23021, Exams of Computer Science

Partial preview of the text

Download Problem in Exam - Discrete Structures for Computer Science | CS 23021 and more Exams Computer Science in PDF only on Docsity!

The Problem

A bank account at the National Source Bank pays an interest of 5%. Tim opens a bank

account by depositing $500. Tim can withdraw and deposit and check the balance of the

account at any time via an ATM machine with a bank card.

Write a program that creates a bank account, deposits $1000 in it, withdraws $300 from

it, computes the interest and updates the new balance with the interests.

Procedural Approach

1. Create a separate function for each action that can be performed on the account.

2. Create a main and from the main call the functions one by one according to the

problem requirements.

The function are actions. Therefore are identified by the verbs in the problem description:

1. open an account

2. deposit money

3. withdraw money

4. compute interest

5. update the account with the interest

//This program uses the procedural approach to demonstrate the BankAccount problem. #include #include <stdlib.h> using namespace std; //list of the functions used //open creates an account and returns the number of the account int open(double amount); //it adds the amount to deposit to the balance and returns the new balance double deposit(double abalance, double amount); //it subtracts the amount to withdraw to the balance and returns the new balance double withdraw(double abalance, double amount); //it computes the interest of the account and returns the new balance double compute_interest(double abalance, const double ARATE); //it adds the current balance to the interest matured double update(double abalance, double ainterest); int main()//here starts the main {int account, double balance, double interest; const double RATE = 0.05; cout << "How much money to you want to start the account with? " << endl; cout << "Insert the amount here: $"; cin >> balance; //create an account and add some money in it account = open(balance); cout << "The number of the account just created is " << account << endl; cout << "and has " << balance << " dollars in it." << endl; //deposit money in the account balance = deposit(balance, 1000); cout << "The account has " << balance << " dollars in it now." << endl; //withdraw money from the account balance = withdraw(balance, 300); cout << "The account has " << balance << " dollars in it now." << endl; //compute the interest you the account interest = compute_interest(balance, RATE); cout << "The interest on your account is " << interest << endl; //update the balance with the interest balance = update(balance, interest); cout << "The account has " << balance << " dollars in it now." << endl; return 0; } // end of main

Object Oriented Approach

In this approach we create separately a class (we will call it BankAccount) and a main.

In order to understand which class (or classes) to create, we must identify the object (or

the objects) that are required by the problem. The objects are nouns. The only important

object we have to describe in our problem is a bank account. Therefore we must

implement a class that we will call for simplicity BankAccount.

The class Bank Account is written separately from the main. A class in general contains 2

parts: the public part and the private part.

๏‚ท In the public part we insert the constructors and the methods of the class.

๏‚ท In the private part we insert all the attributes of the class ( instance fields )

The constructor is a special function that creates an object of the class (for example a

specific bank account)

A method is a function that describes something that the class can perform (i.e. describes

the behavior of the class). For example in the class BankAccount, the class can

1. create an account

2. deposit money in an account

3. withdraw money in an account

4. compute interest of an account

5. update an account with the interest

There are 2 types of methods: those who change the value of the data in the class

( mutators ), and those who just read the data in the class ( inspectors ).

Contrary to the previous approach, it is the BankAccount who performs these actions

through its methods and not the main who call directly the functions.

The attributes are the variables that describe the content of each BankAccount. For

example, each BankAccount has a number, a balance, an interest rate, and an interest

produced.

When you have completed the class description you write a main (a test driver) to guide

the sequence of the action required by the problem.

//Bank Account Class (with Constructors) //Program to demonstrate the class BankAccount. #include #include <stdlib.h> using namespace std; //Class for a bank account starts here: class BankAccount { public: //first we define the constructors //constructor number 1 BankAccount(double amount, double rate) //Initializes the account balance to the amount //initializes the interest rate to rate percent. {if ((amount < 0) || (rate < 0)) { cout << "Illegal values for money or interest rate.\n"; exit(1); } balance = amount; interest = 0.0; number = rand(); interest_rate = rate; } //constructor number 2 BankAccount( ) //Initializes the account balance to $0.00 and the interest rate to 0.0%. {balance = 0.0; interest = 0.0; interest_rate = 0.0; } //here starts the list of the methods of the class each with its implementation void deposit(double amount) //The amount is added to the balance of the account. { balance = balance + amount; } void withdraw(double amount) //The amount is subtracted to the balance of the account. { balance = balance - amount; } void compute_interest() //Computes the interest of the account {interest = balance * interest_rate; } void update() //Updates the current account with the interest { balance = balance + interest; } int get_number() //Returns the number of the current account. { return number; } double get_interest() //Returns the interest of the current account. {return interest; } double get_balance( ) //Returns the current account balance. {return balance; }