Ai 270 Opp Assignment
Ai 270 Opp Assignment
01:
(Account Class) Create an Account class that a bank might use to represent customers’ bank
accounts. Include a data member of type int to represent the account balance. Provide a
constructor that receives an initial balance and uses it to initialize the data member. The
constructor should validate the initial balance to ensure that it’s greater than or equal to 0. If
not, set the balance to 0 and display an error message indicating that the initial balance was
invalid. Provide three member functions. Member function credit should add an amount to
the current balance. Member function debit should withdraw money from the Account and
ensure that the debit amount does not exceed the Account’s balance. If it does, the balance
should be left unchanged and the function should print a message indicating "Debit amount
exceeded account balance." Member function getBalance should return the current balance.
Create a program that creates two Account objects and tests the member functions of class
Account.
SOURCE CODE:
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
int balance;
string accountHolder;
public:
Page |1
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270
int main() {
string name1, name2;
int init1, init2;
Page |2
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270
int choice;
do {
cout << "\nChoose operation:\n";
cout << "1. Credit to account1\n";
cout << "2. Debit from account1\n";
cout << "3. Credit to account2\n";
cout << "4. Debit from account2\n";
cout << "5. Display account1 info\n";
cout << "6. Display account2 info\n";
cout << "7. Exit\n";
cout << "Enter choice (1-7): ";
cin >> choice;
if (choice == 1) {
account1.credit(amount);
} else if (choice == 2) {
account1.debit(amount);
} else if (choice == 3) {
account2.credit(amount);
} else if (choice == 4) {
account2.debit(amount);
}
} else if (choice == 5) {
account1.displayAccountInfo();
} else if (choice == 6) {
account2.displayAccountInfo();
} else if (choice == 7) {
cout << "Exiting." << endl;
} else {
cout << "Invalid choice." << endl;
}
} while (choice != 7);
return 0;
}
Page |3
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270
SCREEN SHOT:
OUTPUT:
Enter account holder name for account1: Jalal
Enter initial balance for account1: 20
Enter account holder name for account2: Azam
Enter initial balance for account2: -1
Error: Initial balance for Azam was invalid. Set to 0.
Choose operation:
1. Credit to account1
2. Debit from account1
3. Credit to account2
Page |4
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270
Choose operation:
1. Credit to account1
2. Debit from account1
3. Credit to account2
4. Debit from account2
5. Display account1 info
6. Display account2 info
7. Exit
Enter choice (1-7): 2
Enter amount: 50
Error: Debit amount exceeds Jalal's account balance.
Choose operation:
1. Credit to account1
2. Debit from account1
3. Credit to account2
Page |5
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270
02:
(Target-Heart-Rate Calculator) While exercising, you can use a heart-rate monitor to see
that your heart ratestays within a safe range suggested by your trainers and doctors.
According to the American Heart Association (AHA)
(www.americanheart.org/presenter.jhtml?identifier=4736), the formula for calculating your
maximum heart rate in beats per minute is 220 minus your age in years. Your target heart
rate is a range that is 50–85% of your maximum heart rate. [Note: These formulas are
estimates provided by the AHA. Maximum and target heart rates may vary based on the
health, fitness and gender of the individual. Always consult a physician or qualified health
care professional before beginning or modifying an exercise program.] Create a class called
HeartRates. The class attributes should include the person’s first name, last name and date
of birth (consisting of separate attributes for the month, day and year of birth). Your class
should have a constructor that receives this data as parameters. For each attribute provide
set and get functions. The class also should include a function getAge that calculates and
returns the person’s age (in years), a function getMaxiumumHeartRate that calculates and
returns the person’s maximum heart rate and a function getTargetHeartRate that calculates
and returns the person’s target heart rate. Since you do not yet know how to obtain the
current date from the computer, function getAge should prompt the user to enter the current
month, day and year before calculating the person’s age. Write an application that prompts
for the person’s information, instantiates an object of class HeartRates and prints the
information from that object—including the person’s first name, last name and date of
birth—then calculates and prints the person’s age in (years), maximum heart rate and
target-heart-rate range.
SOURCE CODE:
Page |6
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270
#include <iostream>
#include <string>
using namespace std;
class HeartRates {
private:
string firstName;
string lastName;
int birthMonth;
int birthDay;
int birthYear;
int currentMonth;
int currentDay;
int currentYear;
bool dateSet;
public:
HeartRates(string first, string last, int month, int day, int year)
: firstName(first), lastName(last), birthMonth(month), birthDay(day),
birthYear(year), dateSet(false) {}
int getAge() {
Page |7
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270
if (!dateSet) {
cout << "To calculate the age, please enter the current date:" << endl;
cout << "Enter current month (1-12): ";
cin >> currentMonth;
cout << "Enter current day (1-31): ";
cin >> currentDay;
cout << "Enter current year: ";
cin >> currentYear;
dateSet = true;
}
int age = currentYear - birthYear;
if (currentMonth < birthMonth ||
(currentMonth == birthMonth && currentDay < birthDay)) {
age--;
}
return age;
}
int getMaximumHeartRate() {
int age = getAge();
return 220 - age;
}
string getTargetHeartRate() {
int maxHR = getMaximumHeartRate();
int minTarget = static_cast<int>(maxHR * 0.5);
int maxTarget = static_cast<int>(maxHR * 0.85);
return to_string(minTarget) + " - " + to_string(maxTarget);
}
};
int main() {
string first, last;
int month, day, year;
Page |8
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270
cout << "\n" << person.getFirstName() << " " << person.getLastName()
<< ", born: " << person.getBirthMonth() << "/"
<< person.getBirthDay() << "/" << person.getBirthYear() << endl;
return 0;
}
SCREEN SHOT:
Page |9
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270
OUTPUT:
Enter first name: Abdul
Enter last name: Jalal
Enter birth month (1-12): 3
Enter birth day (1-31): 6
Enter birth year: 2005
P a g e | 10
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270
Age: 19 years
Maximum heart rate: 201 beats per minute
Target heart rate range: 181 - 270 beats per minute
P a g e | 11