0% found this document useful (0 votes)
13 views12 pages

Ai 270 Opp Assignment

The document is an assignment submitted by Abdul Jalal to Dr. Quratulain Safdar, consisting of two main programming tasks. The first task involves creating a BankAccount class to manage customer accounts, including functions for crediting, debiting, and checking the balance. The second task requires creating a HeartRates class to calculate a person's age, maximum heart rate, and target heart rate based on their birth date and current date.

Uploaded by

alimonster105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

Ai 270 Opp Assignment

The document is an assignment submitted by Abdul Jalal to Dr. Quratulain Safdar, consisting of two main programming tasks. The first task involves creating a BankAccount class to manage customer accounts, including functions for crediting, debiting, and checking the balance. The second task requires creating a HeartRates class to calculate a person's age, maximum heart rate, and target heart rate based on their birth date and current date.

Uploaded by

alimonster105
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Assignment # 01

SUBMITTED TO Dr Quratulain Safdar


SUBMITTED BY Abdul Jalal
REGISTRATION NO. AI 270
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270

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:

BankAccount(string name, int initialBalance) {


accountHolder = name;
if (initialBalance >= 0) {
balance = initialBalance;
} else {
balance = 0;
cout << "Error: Initial balance for " << accountHolder << " was invalid. Set to 0." << endl;
}
}

Page |1
Instructor Name: Dr Quratulain Safdar Department: AI
Section: AI (Green) Assignment Number: Assignment # 01
Student Name: Abdul Jalal Registration No : AI 270

void credit(int amount) {


balance += amount;
cout << "Credited " << amount << " to " << accountHolder << "'s account." << endl;
}

void debit(int amount) {


if (amount > balance) {
cout << "Error: Debit amount exceeds " << accountHolder << "'s account balance." << endl;
} else {
balance -= amount;
cout << "Debited " << amount << " from " << accountHolder << "'s account." << endl;
}
}

int getBalance() const {


return balance;
}

void displayAccountInfo() const {


cout << "Account Holder: " << accountHolder << endl;
cout << "Current Balance: " << balance << endl;
}
};

int main() {
string name1, name2;
int init1, init2;

cout << "Enter account holder name for account1: ";


cin >> name1;
cout << "Enter initial balance for account1: ";
cin >> init1;
BankAccount account1(name1, init1);

cout << "Enter account holder name for account2: ";


cin >> name2;
cout << "Enter initial balance for account2: ";
cin >> 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

BankAccount account2(name2, init2);

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 && choice <= 4) {


int amount;
cout << "Enter amount: ";
cin >> amount;

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

4. Debit from account2


5. Display account1 info
6. Display account2 info
7. Exit
Enter choice (1-7): 1
Enter amount: 20
Credited 20 to Jalal's account.

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

4. Debit from account2


5. Display account1 info
6. Display account2 info
7. Exit
Enter choice (1-7): 7
Exiting.

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) {}

void setFirstName(string first) { firstName = first; }


void setLastName(string last) { lastName = last; }
void setBirthMonth(int month) { birthMonth = month; }
void setBirthDay(int day) { birthDay = day; }
void setBirthYear(int year) { birthYear = year; }

string getFirstName() const { return firstName; }


string getLastName() const { return lastName; }
int getBirthMonth() const { return birthMonth; }
int getBirthDay() const { return birthDay; }
int getBirthYear() const { return birthYear; }

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;

cout << "Enter first name: ";


cin >> first;

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 << "Enter last name: ";


cin >> last;
cout << "Enter birth month (1-12): ";
cin >> month;
cout << "Enter birth day (1-31): ";
cin >> day;
cout << "Enter birth year: ";
cin >> year;

HeartRates person(first, last, month, day, year);

cout << "\n" << person.getFirstName() << " " << person.getLastName()
<< ", born: " << person.getBirthMonth() << "/"
<< person.getBirthDay() << "/" << person.getBirthYear() << endl;

int age = person.getAge();


cout << "Age: " << age << " years" << endl;

int maxHR = person.getMaximumHeartRate();


cout << "Maximum heart rate: " << maxHR << " beats per minute" << endl;

string targetHR = person.getTargetHeartRate();


cout << "Target heart rate range: " << targetHR << " beats per minute" << 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

Ali Mosavi, born: 3/5/2005


To calculate the age, please enter the current date:
Enter current month (1-12): 3
Enter current day (1-31): 5
Enter current year: 2025

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

You might also like