Exercise10_2
#include
class Loan {
private:
double annualInterestRate; int numberOfYears; double loanAmount;
public:
Loan::Loan() {
annualInterestRate = 9.5; numberOfYears = 30; loanAmount = 100000; }
Loan::Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
this->annualInterestRate = annualInterestRate; this->numberOfYears = numberOfYears; this->loanAmount = loanAmount; }
double Loan::getAnnualInterestRate() {
return annualInterestRate; }
int Loan::getNumberOfYears() {
return numberOfYears; }
double Loan::getLoanAmount() {
return loanAmount; }
void Loan::setAnnualInterestRate(double annualInterestRate) {
this->annualInterestRate = annualInterestRate; }
void Loan::setNumberOfYears(int numberOfYears) {
this->numberOfYears = numberOfYears; }
void Loan::setLoanAmount(double loanAmount) {
this->loanAmount = loanAmount; }
double Loan::getMonthlyPayment() {
double monthlyInterestRate = annualInterestRate / 1200;
return loanAmount * monthlyInterestRate / (1 - (pow(1 / (1 + monthlyInterestRate), numberOfYears * 12))); }
double Loan::getTotalPayment() {
return getMonthlyPayment() * numberOfYears * 12; }
static double getMonthlyPayment(double annualInterestRate, int numberOfYears, double loanAmount) {
double monthlyInterestRate = annualInterestRate / 1200;
return loanAmount * monthlyInterestRate / (1 - (pow(1 / (1 + monthlyInterestRate), numberOfYears * 12))); }
static double getTotalPayment(double annualInterestRate, int numberOfYears, double loanAmount) {
return Loan::getMonthlyPayment(annualInterestRate, numberOfYears, loanAmount) * numberOfYears * 12; } };
int main() {
Loan loan1(7.5, 15, 100000);
cout << \ cout << \
cout << \ cout << \
return 0; }
Exercise10_4
#include
#include %using namespace std;
bool isPrime(int number) {
// Assume the number is prime bool isPrime = true;
// Test if number is prime
for (int divisor = 2; divisor <= number / 2; divisor++) {
//If true, the number is not prime if (number % divisor == 0) {
// Set isPrime to false, if the number is not prime isPrime = false;
break; // Exit the for loop } }
return isPrime; }
int main() {
const int LIMIT = 120; int count = 0;
StackOfIntegers stack;
// Repeatedly find prime numbers
for (int number = 2; number < LIMIT; number++) if (isPrime(number)) {
stack.push(number);
count++; // Increase the prime number count }
// Print the first 30 prime numbers in decreasing order cout << \ const int NUMBER_PER_LINE = 10;
while (!stack.empty()) {
cout << stack.pop() << \
if (stack.getSize() % NUMBER_PER_LINE == 0) cout << endl; // advance to the new line }
return 0; }
Exercise10_6
#include
class Course {
public:
Course::Course(const string & name) {
this->name = name; }
string Course::getName() {
return name; }
void Course::addStudent(const string & student) {
v.push_back(student); }
vector
return v; }
int Course::getNumberOfStudents() {
return v.size(); }
private:
string name; vector
int main() {
Course course1(\ Course course2(\
course1.addStudent(\ course1.addStudent(\ course1.addStudent(\
course2.addStudent(\ course2.addStudent(\
cout << \ course1.getNumberOfStudents() << \// string * students = course1.getStudents();
// for (int i = 0; i < course1.getNumberOfStudents(); i++) // cout << students[i] << \//
// cout << \// << course2.getNumberOfStudents() << \// students = course2.getStudents();
// for (int i = 0; i < course2.getNumberOfStudents(); i++) // cout << students[i] << \