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

lab Exercises 1 #116

Open
wants to merge 19 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
14 changes: 14 additions & 0 deletions 201816040216/Lab1/Acount,h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Lab 1: Account.h
// Definition of Account class.

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

41 changes: 41 additions & 0 deletions 201816040216/Lab1/Acount.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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::Debit( int amount )
{
balance = balance - amount; // add amount to balance
if(balance<0)
{
cout<<"Debit amout exceeded account balance."<<"\n";
balance=0;
}
} // end function credit

/* write code to define member function debit. */


// return the account balance
int Account::getBalance()
{
return balance; // gives the value of balance to the calling function
} // end function getBalance
40 changes: 40 additions & 0 deletions 201816040216/Lab1/AcountText,cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 */
account1.Debit(withdrawalAmount);
// 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 */
account2.Debit(withdrawalAmount);
// display balances
cout << "account1 balance: $" << account1.getBalance() << endl;
cout << "account2 balance: $" << account2.getBalance() << endl;
} // end main
57 changes: 57 additions & 0 deletions 201816040216/Lab2/GradeBook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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,string Ming )
{
setCourseName( course ); // initializes courseName
setInstructorName( instructor ); // initialiZes instructorName
setInstructorMing(Ming);
} // 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 */
void GradeBook::setInstructorName( string instructor )
{
instructorName=instructor;// store the course instruct
}// end function setCourseInstructor
/* write code to define a set member function for the instructor's name */
string GradeBook::getCourseInstructor()
{
return instructorName;
}// end function getCourseInstructtor
// display a welcome message and the instructor's name
void GradeBook::setInstructorMing(string ming )
{
instructorMing=ming;
}// end function setInstructorMing

string GradeBook::getInstructorMing()
{
return instructorMing;
}// end function getCourseInstructorMing
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:"<<getCourseInstructor()<<" "<<getInstructorMing()<<"\n";
} // end function displayMessage
28 changes: 28 additions & 0 deletions 201816040216/Lab2/GradeBook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 ,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 */
void setInstructorName( string );
/* write code to declare a set function for the instructor's name */
string getCourseInstructor();

void setInstructorMing(string);
string getInstructorMing();

void displayMessage(); // display welcome message and instructor name
private:
string courseName; // course name for this GradeBook
string instructorName; // instructor name for this GradeBook
string instructorMing;//instructor Ming for this Gradebook
}; // end class GradeBook
29 changes: 29 additions & 0 deletions 201816040216/Lab2/GradeBookText.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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
string courseinstructor,InstructorMing;
GradeBook gradeBook(
"CS101 Introduction to C++ Programming","Sam","Smith" );
// display welcome message and instructor's name
gradeBook.displayMessage();
cout<<"\n";
/* write code to change instructor's name and output changes */
cout<<"Changing instrutor name to";
cin>>courseinstructor;
cin>>InstructorMing;

gradeBook.setInstructorName(courseinstructor);
gradeBook.setInstructorMing(InstructorMing);
gradeBook.displayMessage();


} // end main
64 changes: 64 additions & 0 deletions 201816040216/Lab3/Employee.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Lab 3: Employee.cpp
// Employee class member-function definitions.
#include <iostream>
using namespace std;

#include "Employee.h" // Employee class definition
Employee::Employee(string name,string ming,int money)
{
setEmployeename(name);
setEmployeeming(ming);
setEmployeemoney(money);
}
/* 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. */

/* Define a set function for the first name data member. */
void Employee::setEmployeename(string name)
{
Employeename=name;// store the Employee name
}//end function setEmployeename
/* Define a get function for the first name data member. */
string Employee::getEmployeename()
{
return Employeename;
}//end function getEmployeename
/* Define a set function for the last name data member. */
void Employee::setEmployeeming(string ming)
{
Employeeming=ming;// store the Employee ming
/* Define a get function for the last name data member. */

}
string Employee::getEmployeeming()
{
return Employeeming;
}//end function getEmployeeming
/* 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::setEmployeemoney(int money)
{
Employeemoney=money;// store the Employee money
}//end function setEmployeemoney
/* Define a get function for the monthly salary data member. */
int Employee::getEmployeemoney()
{
return Employeemoney;
}//end function getEmployeemoney
void Employee::displaymessage()
{
cout<<getEmployeename()<<" "<<getEmployeeming()<<"; "<<"Yearly Salary:"<<getEmployeemoney()<<endl;
}//end function Employee displaymessage

int Employee::increasingmoney(int Employeemoney)
{
Employeemoney1=Employeemoney*1.1;
return Employeemoney1;
}//end function increasingmoney
void Employee::displaymessage1()
{
cout<<getEmployeename()<<" "<<getEmployeeming()<<"; "<<"Yearly Salary:"<<Employeemoney1<<endl;
}


35 changes: 35 additions & 0 deletions 201816040216/Lab3/Employee.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 setEmployeename(string);
/* Declare a get method for the employee's first name */
string getEmployeename();
/* Declare a set method for the employee's last name */
void setEmployeeming(string);
/* Declare a get method for the employee's last name */
string getEmployeeming();
/* Declare a set method for the employee's monthly salary */
void setEmployeemoney(int);
/* Declare a get method for the employee's monthly salary */
int getEmployeemoney();
void displaymessage();// display welcome message and instructor name
void displaymessage1();
int increasingmoney(int);
private:
/* Declare a string data member for the employee's first name */
string Employeename;
/* Declare a string data member for the employee's last name */
string Employeeming;
/* Declare an int data member for the employee's monthly salary */
int Employeemoney,Employeemoney1;
}; // end class Employee
35 changes: 35 additions & 0 deletions 201816040216/Lab3/EmployeeText.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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()
{
int a=34500,b=37800;//set employee's yearly money

Employee employee1("Bob","Jone",a);
Employee employee2("Susan","Baker",b);
/* Create two Employee objects and assign them to Employee variables. */
cout<<"Employee1: ";
employee1.displaymessage();
/* Output the first name, last name and salary for each Employee. */
cout<<"Employee2: ";
employee2.displaymessage();
/* Give each Employee a 10% raise. */
cout<<endl;
cout<<"Increasing employee salaries by 10%"<<endl;
cout<<"\n";

employee1.increasingmoney(a);
cout<<"Employee1: ";
employee1.displaymessage1();
employee2.increasingmoney(b);
cout<<"Employee2: ";
employee2.displaymessage1();
/* Output the first name, last name and salary of each Employee again. */
} // end main


45 changes: 45 additions & 0 deletions 201816040216/Lab4/Complex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 */
Complex t(0,0);
t.setComplexNumber( right.realPart + realPart, right.imaginaryPart + imaginaryPart);
return t;

} // 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 */
Complex temp(0,0);
temp.setComplexNumber( realPart - right.realPart,imaginaryPart - right.imaginaryPart);
return temp;
} // 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
Loading