CSI 250L Lab 5 Solution, Spring 2012
American University of Science and Technology · Programming II Lab · 9 pages · Dr. Saeed Raheel · with solution
More CSI 250L files
- Lab 1 Solution · Spring 2012Lab · with solutionView
- Lab 1 · Spring 2012LabView
- Lab 10 Solution · Spring 2012Lab · with solutionView
- Lab 10 · Spring 2012LabView
- Lab 11 Solution · Spring 2012Lab · with solutionView
- Lab 11 · Spring 2012LabView
- Lab 12 Solution · Spring 2012Lab · with solutionView
- Lab 12 · Spring 2012LabView
Show the text of this lab
American University of Science & Technology
Department Of Computer Science
CSI 250L – Computer Programming-II Lab
Spring 2011/2012
Lab Work 5
Problem 3:
Create a Polygon class that stores the Cartesian coordinates of the four points.
The constructor accepts four sets of coordinates and verifies that each of these is
in the first quadrant with no single x- or y-coordinate larger than 20.0. These
coordinates should be stored in private data members.
Provide set, get and print functions.
#include <iostream>
using namespace std;
class Polygon
{
private:
int p1x, p1y;
int p2x, p2y;
public:
Polygon();
Polygon(int, int, int, int);
void setp1x(int);
void setp1y(int);
int getp1x();
int getp1y();
void setp2x(int);
void setp2y(int);
int getp2x();
int getp2y();
void print();
};
Polygon::Polygon()
{
p1x = p1y = p2x = p2y = 0;
}
Polygon::Polygon(int x1, int y1, int x2, int y2)
{
1
if (x1<=20) setp1x(x1);
else {cout << "Invalid x-coordinate for the first
point!"<<endl;p1x=0;}
if (y1<=20) setp1y(y1);
else {cout << "Invalid y-coordinate for the first
point!"<<endl;p1y=0;}
if (x2<=20) setp2x(x2);
else {cout << "Invalid x-coordinate for the second
point!"<<endl;p2x=0;}
if (y2<=20) setp2y(y2);
else {cout << "Invalid y-coordinate for the second
point!"<<endl;p2y=0;}
}
void Polygon::setp1x(int x)
{
if (x<=20) p1x = x;
else {cout << "Invalide x-coordinate for the first point!" <<
endl;p1x=0;}
}
void Polygon::setp1y(int y)
{
if (y<=20) p1y = y;
else {cout << "Invalide y-coordinate for the first point!" <<
endl;p1y=0;}
}
int Polygon::getp1x()
{
return p1x;
}
int Polygon::getp1y()
{
return p1y;
}
void Polygon::setp2x(int x)
{
if (x<=20) p2x = x;
else {cout << "Invalide x-coordinate for the second point!" <<
endl;p2x=0;}
}
void Polygon::setp2y(int y)
{
if (y<=20) p2y = y;
else {cout << "Invalide y-coordinate for the second point!" <<
endl;p2y=0;}
}
int Polygon::getp2x()
{
return p2x;
}
2
int Polygon::getp2y()
{
return p2y;
}
void Polygon::print()
{
cout << "The four coordinates of the polygon are : " << endl;
cout << "Point 1 : ( " << getp1x() << ", " << getp1y() << ")" <<
endl;
cout << "Point 2 : ( " << getp2x() << ", " << getp2y() << ")" <<
endl;
}
int main()
{
int x1, y1, x2, y2;
cout << "Please enter the four coordinates of the first polygon :
" << endl;
cin >> x1 >> y1 >> x2 >> y2;
Polygon p1;
p1.setp1x(x1);
p1.setp1y(y1);
p1.setp2x(x2);
p1.setp2y(y2);
p1.print();
cout << "\nPlease enter the four coordinates of the second
polygon : " << endl;
cin >> x1 >> y1 >> x2 >> y2;
Polygon p2(x1, y1, x2, y2);
p2.print();
system("pause");
return 0;
}
3
Problem 4:
Write the class definitions and implementations for the following class specifications.
Use separate files for the definition and implementation.
Staff
Attributes
ID: an integer to identify each staff.
fname: a string of characters( dynamic
allocation).
lname: a string of characters( dynamic
allocation).
rank: an integer to classify a staff position.
(1,2,3)
BS: a double value for a staff basic salary.
Methods
Constructor(default values)
print: a constant function, to print the
information about a staff.
Provide set and get functions.
Calculate_salary: compute an employee
salary by deducting a tax from the basic
salary depending on the rank as follows:
1. for rank 1 a tax of 5% is deducted.
2. for rank 2 a tax of 7% is deducted.
3. for rank 3 a tax of 10% is deducted
Write a driver program to create N objects of the class staff and display a menu to do the
following:
1. Display a sorted list with respect to staff names.
2. Calculate the total staff salaries.
3. Output the number of staff of rank 1.
//Staff.h
#ifndef STAFF_H
#define STAFF_H
class Staff
{
private:
int ID;
char *FName;
4
char *LName;
int Rank;
double BS;
public:
Staff(int=0, char* ="", char* ="", int=0, double=0.0);
~Staff();
void setID(int);
void setFName(char *);
void setLName(char *);
void setRank(int);
void setBS(double);
int getID();
char * getFName();
char * getLName();
int getRank();
double getBS();
void print() const;
};
#endif
5
//staff.cpp
#include "Staff.h"
#include <iostream>
using namespace std;
Staff::Staff(int id, char * fn, char *ln, int r, double bs)
{
setID(id);
setFName(fn);
setLName(ln);
setRank(r);
setBS(bs);
}
Staff::~Staff()
{
ID = NULL;
delete[] FName;
delete[] LName;
Rank = NULL;
BS = 0.0;
}
void Staff::setID(int id)
{
ID = id;
}
void Staff::setFName(char*n)
{
FName = new char[strlen(n)+1];
strcpy(FName, n);
}
void Staff::setLName(char *n)
{
LName = new char[strlen(n)+1];
strcpy(LName, n);
}
void Staff::setRank(int r)
{
Rank = r;
}
int Staff::getID()
{
return ID;
}
void Staff::setBS(double bs)
{
BS = bs;
}
char *Staff::getFName()
{
6
return FName;
}
char *Staff::getLName()
{
return LName;
}
int Staff::getRank()
{
return Rank;
}
double Staff::getBS()
{
return BS;
}
void Staff::print() const
{
cout << "ID = " << ID << ", First Name = " << FName << ", Last
Name = " << LName << ", Rank = " << Rank << ", Base Salary = " << BS <<
endl;
}
//driver program
#include "Staff.h"
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "How many Staff(s) would you like to create?";
cin >> n;
Staff *staff = new Staff[n];
int id;
char fname[100];
char lname[100];
int rank;
double bs;
for (int i=0;i<n;i++)
{
cout << "Enter Staff's ID:";
cin >> id;
cout << "Enter Staff's first name:";
cin >> fname;
cout << "Enter Staff's last name:";
cin >> lname;
cout << "Enter Staff's rank:";
cin >> rank;
7
cout << "Enter Staff's basic salary:";
cin >> bs;
staff[i].setID(id);
staff[i].setFName(fname);
staff[i].setLName(lname);
staff[i].setRank(rank);
staff[i].setBS(bs);
}
Staff tempstaff;
for (int i=0;i<n;++i)
for (int j=0;j<n-1;++j)
if (strcmp(staff[j].getLName(),
staff[j+1].getLName())>0)
{
tempstaff = staff[j];
staff[j] = staff[j+1];
staff[j+1] = tempstaff;
}
cout << "After sorting by last Name : "<< endl;
for (int i=0;i<n;++i)
staff[i].print();
cout << endl;
cout << "Printing information about staff whose Rank = 1" <<
endl;
for (int i=0;i<n;++i)
if (staff[i].getRank() == 1)
staff[i].print();
cout << endl;
cout << "Staff Salaries : " ;
double totalBS = 0;
for (int i=0;i<n;++i)
totalBS += staff[i].getBS();
cout << totalBS << endl;
system("pause");
return 0;
}
8
9