Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

201816060101 #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 201816060101/1st Lab exercises.docx
Binary file not shown.
43 changes: 43 additions & 0 deletions 201816060101/Lab1/Account.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Lab 1: Account.cpp
// Member-function definitions for class Account.
#include <iostream>
using namespace std;

#include "Account.h" // include definition of class Account

// Account constructor initializes data member balance
Account::Account(int initialBalance)
{
balance = 0; // assume that the balance begins at 0

// if initialBalance is greater than 0, set this value as the
// balance of the Account; otherwise, balance remains 0
if (initialBalance > 0)
balance = initialBalance;

// if initialBalance is negative, print error message
if (initialBalance < 0)
cout << "Error: Initial balance cannot be negative.\n" << endl;
} // end Account constructor

// credit (add) an amount to the account balance
void Account::credit(int amount)
{
balance = balance + amount; // add amount to balance
} // end function credit

/* write code to define member function debit. */
bool Account::debit(int Debit)
{
//Return false if the balance is insufficient
if (balance < Debit) return false;
//Otherwise, the fee will be deducted
else balance -= Debit;
return true;
}

// return the account balance
int Account::getBalance()
{
return balance; // gives the value of balance to the calling function
} // end function getBalance
15 changes: 15 additions & 0 deletions 201816060101/Lab1/Account.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Lab 1: Account.h
// Definition of Account class.

class Account
{
public:
Account(int); // constructor initializes balance
void credit(int); // add an amount to the account balance
/* write code to declare member function debit. */
bool debit(int);
int getBalance(); // return the account balance
private:
int balance; // data member that stores the balance
}; // end class Account

43 changes: 43 additions & 0 deletions 201816060101/Lab1/AccountTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Lab 1: AccountTest.cpp
// Create and manipulate Account objects.
#include <iostream>
using namespace std;

// include definition of class Account from Account.h
#include "Account.h"

// function main begins program execution
int main()
{
Account account1(50); // create Account object
Account account2(0); // create Account object

// display initial balance of each object
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;

int withdrawalAmount; // stores withdrawal amount read from user

cout << "\nEnter withdrawal amount for account1: "; // prompt
cin >> withdrawalAmount; // obtain user input
cout << "\nsubtracting " << withdrawalAmount
<< " from account1 balance\n\n";
/* write code to withdraw money from account1 */
if (!account1.debit(withdrawalAmount)) cout << "Debit amount exceeded account balance.\n\n";

// display balances
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;

cout << "\nEnter withdrawal amount for account2: "; // prompt
cin >> withdrawalAmount; // obtain user input
cout << "\nsubtracting " << withdrawalAmount
<< " from account2 balance\n\n";
/* write code to withdraw money from account2 */
if (!account2.debit(withdrawalAmount)) cout << "Debit amount exceeded account balance.\n\n";

// display balances
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
} // end main

50 changes: 50 additions & 0 deletions 201816060101/Lab2/GradeBook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Lab 2: GradeBook.cpp
// Member-function definitions for class GradeBook.
#include <iostream>
using namespace std;

// include definition of class GradeBook from GradeBook.h
#include "GradeBook.h"

// constructor initializes courseName and instructorName
// with strings supplied as arguments
GradeBook::GradeBook(string course, string instructor)
{
setCourseName(course); // initializes courseName
setInstructorName(instructor); // initialiZes instructorName
} // end GradeBook constructor

// function to set the course name
void GradeBook::setCourseName(string name)
{
courseName = name; // store the course name
} // end function setCourseName

// function to retrieve the course name
string GradeBook::getCourseName()
{
return courseName;
} // end function getCourseName

/* write code to define a get member function for the instructor's name */
string GradeBook::getInstructorName()
{
return instructorName;
}

/* write code to define a set member function for the instructor's name */
void GradeBook::setInstructorName(string name)
{
instructorName = name;
}

// display a welcome message and the instructor's name
void GradeBook::displayMessage()
{
// display a welcome message containing the course name
cout << "Welcome to the grade book for\n" << getCourseName() << "!"
<< endl;
/* write code to output the instructor's name */
cout << "This course is presented by: " << getInstructorName() << "\n";
} // end function displayMessage

23 changes: 23 additions & 0 deletions 201816060101/Lab2/GradeBook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Lab 2: GradeBook.h
// Definition of GradeBook class that stores an instructor's name.
#include <string> // program uses C++ standard string class
using namespace std;

// GradeBook class definition
class GradeBook
{
public:
// constructor initializes course name and instructor name
GradeBook(string, string);
void setCourseName(string); // function to set the course name
string getCourseName(); // function to retrieve the course name
/* write code to declare a get function for the instructor's name */
string getInstructorName();
/* write code to declare a set function for the instructor's name */
void setInstructorName(string);
void displayMessage(); // display welcome message and instructor name
private:
string courseName; // course name for this GradeBook
string instructorName; // instructor name for this GradeBook
}; // end class GradeBook

24 changes: 24 additions & 0 deletions 201816060101/Lab2/GradeBookTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Lab 2: GradeBookTest.cpp
// Test program for modified GradeBook class.
#include <iostream>
using namespace std;

// include definition of class GradeBook from GradeBook.h
#include "GradeBook.h"

// function main begins program execution
int main()
{
// create a GradeBook object; pass a course name and instructor name
GradeBook gradeBook("CS101 Introduction to C++ Programming", "Sam Smith");

// display welcome message and instructor's name
gradeBook.displayMessage();

/* write code to change instructor's name and output changes */
cout << "\nChanging instructor name to Judy Jones\n\n";
gradeBook.setInstructorName("Judy Jones");

// display welcome message and instructor's name
gradeBook.displayMessage();
} // end main
49 changes: 49 additions & 0 deletions 201816060101/Lab3/Employee.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Lab 3: Employee.cpp
// Employee class member-function definitions.
#include <iostream>
using namespace std;

#include "Employee.h" // Employee class definition

/* Define the constructor. Assign each parameter value to the appropriate data
member. Write code that validates the value of salary to ensure that it is
not negative. */
Employee::Employee(string FirstName, string LastName, int MonthlySalary) :FirstName(FirstName), LastName(LastName), MonthlySalary(MonthlySalary) {}

/* Define a set function for the first name data member. */
void Employee::setFirstName(string _FirstName)
{
FirstName = _FirstName;
}

/* Define a get function for the first name data member. */
string Employee::getFirstName()
{
return FirstName;
}

/* Define a set function for the last name data member. */
void Employee::setLastName(string _LastName)
{
LastName = _LastName;
}

/* Define a get function for the last name data member. */
string Employee::getLastName()
{
return LastName;
}

/* Define a set function for the monthly salary data member. Write code
that validates the salary to ensure that it is not negative. */
void Employee::setMonthlySalary(int _MonthlySalary)
{
if (_MonthlySalary <= 0) MonthlySalary = 0;
else MonthlySalary = _MonthlySalary;
}

/* Define a get function for the monthly salary data member. */
int Employee::getMonthlySalary()
{
return MonthlySalary;
}
32 changes: 32 additions & 0 deletions 201816060101/Lab3/Employee.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Lab 3: Employee.h
// Employee class definition.

#include <string> // program uses C++ standard string class
using namespace std;

// Employee class definition
class Employee
{
public:
/* Declare a constructor that has one parameter for each data member */
Employee(string, string, int);
/* Declare a set method for the employee's first name */
void setFirstName(string);
/* Declare a get method for the employee's first name */
string getFirstName();
/* Declare a set method for the employee's last name */
void setLastName(string);
/* Declare a get method for the employee's last name */
string getLastName();
/* Declare a set method for the employee's monthly salary */
void setMonthlySalary(int);
/* Declare a get method for the employee's monthly salary */
int getMonthlySalary();
private:
/* Declare a string data member for the employee's first name */
string FirstName;
/* Declare a string data member for the employee's last name */
string LastName;
/* Declare an int data member for the employee's monthly salary */
int MonthlySalary;
}; // end class Employee
25 changes: 25 additions & 0 deletions 201816060101/Lab3/EmployeeTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Lab 3: EmployeeTest.cpp
// Create and manipulate two Employee objects.
#include <iostream>
using namespace std;

#include "Employee.h" // include definition of class Employee

// function main begins program execution
int main()
{
/* Create two Employee objects and assign them to Employee variables. */
Employee a("BoB", "Jones", 34500 / 12), b("Susan", "Baker", 37800 / 12); //Initializing elements
/* Output the first name, last name and salary for each Employee. */
cout << "Employee 1: " << a.getFirstName() << " " << a.getLastName() << "; Yearly Salary: " << a.getMonthlySalary() * 12 << "\n";
cout << "Employee 2: " << b.getFirstName() << " " << b.getLastName() << "; Yearly Salary: " << b.getMonthlySalary() * 12 << "\n";

/* Give each Employee a 10% raise. */
cout << "\nIncreasing employee salaries by 10%\n";
a.setMonthlySalary(a.getMonthlySalary() * 1.1);
b.setMonthlySalary(b.getMonthlySalary() * 1.1);

/* Output the first name, last name and salary of each Employee again. */
cout << "Employee 1: " << a.getFirstName() << " " << a.getLastName() << "; Yearly Salary: " << a.getMonthlySalary() * 12 << "\n";
cout << "Employee 2: " << b.getFirstName() << " " << b.getLastName() << "; Yearly Salary: " << b.getMonthlySalary() * 12 << "\n";
} // end main
40 changes: 40 additions & 0 deletions 201816060101/Lab4/Complex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Lab 4: Complex.cpp
// Member-function definitions for class Complex.
#include <iostream>
using namespace std;

#include "Complex.h"

Complex::Complex(double real, double imaginary)
{
setComplexNumber(real, imaginary);
} // end Complex constructor

Complex Complex::add(const Complex &right)
{
/* Write a statement to return a Complex object. Add
the realPart of right to the realPart of this Complex
object and add the imaginaryPart of right to the
imaginaryPart of this Complex object */
return Complex(realPart + right.realPart, imaginaryPart + right.imaginaryPart);
} // end function add

Complex Complex::subtract(const Complex &right)
{
/* Write a statement to return a Complex object. Subtract
the realPart of right from the realPart of this Complex
object and subtract the imaginaryPart of right from
the imaginaryPart of this Complex object */
return Complex(realPart - right.realPart, imaginaryPart - right.imaginaryPart);
} // end function subtract

void Complex::printComplex()
{
cout << '(' << realPart << ", " << imaginaryPart << ')';
} // end function printComplex

void Complex::setComplexNumber(double rp, double ip)
{
realPart = rp;
imaginaryPart = ip;
} // end function setComplexNumber
17 changes: 17 additions & 0 deletions 201816060101/Lab4/Complex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Lab 4: Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H

/* Write class definition for Complex */
class Complex {
public:
Complex(double = 0, double = 0);
Complex add(const Complex&);
Complex subtract(const Complex&);
void printComplex();
void setComplexNumber(double, double);
private:
double realPart, imaginaryPart;
};

#endif
Loading