Object Oriented Programming: Lecture Notes 08
Author: A. El-Gadi/Faculty of Computer Engineering/Tripoli University
Below is the code for assignment 01 along with some commentary in the form of code comments. Make sure to read it through.
const double PI=3.14159;
/*The cup material and the liquid are included here as separate classes to as a design measure to make room for any future expansion of the concepts of liquid and solid material. Another design direction is to include their densities in the cup class.*/
class SolidMaterial{public: double density;
SolidMaterial(){density=0;};
SolidMaterial(double dens){density=dens;}};
class Liquid{public: double density;
Liquid(){density=0;};
Liquid(double dens){density=dens;}};
/*A cylinder object to use for cylinder proprieties; because our cup can be thought of as two coaxial cylinders.*/
class Cylinder{public: double radius; double height;
double volume(){return PI*radius*radius*height;}
Cylinder(){radius=0; height=0;};
Cylinder(double r, double h){radius=r; height=h;}
};
class Cup{private:
/*The inner cylinder, which constitutes inner cavity of the cup*/
Cylinder innerCylinder;
/*The outer cylinder, determined by the inner cylinder and the cup thickness.*/
Cylinder outerCylinder(){return Cylinder(innerCylinder.radius+thickness,innerCylinder.height+thickness);}
double thickness;
SolidMaterial cupMaterial;
Liquid lq;
double lqamount;
public:
/*Setting the cup attributes. The diameter is used because real life cylinders are normally given by their diameter. Using the radius instead is also fine.*/
void setCup(double diameter, double innerht, double t, double cupmatdens){
innerCylinder.radius=diameter/2; innerCylinder.height=innerht;
thickness=t; cupMaterial.density=cupmatdens; lqamount=0;}
/*Set liquid density. We can also use a liquid object.*/
void setLiquid(double density){lq.density=density;}
/*Fill the cup with a certain amount. If the amount exceeds the remaining capacity of the cup fill to brim.*/
void fill(double amount){
if(amount<=innerCylinder.volume()-lqamount){
lqamount+=amount;
}
else
{fill();}
}
void fill(){lqamount=innerCylinder.volume();}
/*Empty a certain amount from the cup. If the amount to be poured exceeds the amount in the cup, empty the cup.*/
void empty(double amount){
if(amount<=lqamount){
lqamount-=amount;
}
else
{empty();}
}
void empty(){lqamount=0;}
/*Take note of how we got the outer cylinder volume. Try to explain it to yourself.*/
double tare(){return (outerCylinder().volume()-innerCylinder.volume())*cupMaterial.density;}
double lqweight(){return lqamount*lq.density;}
double weight(){return tare()+lqweight();}
Cup(){setCup(0,0,0,0); lqamount=0;}
/*Construct an empty cup*/
Cup(double diameter, double innerht, double t, double cupmatdens, double lqdens){
setCup(diameter,innerht,t,cupmatdens); lqamount=0; lq.density=lqdens;}
/*Construct a cup that has a certain amount of liquid*/
Cup(double diameter, double innerht, double t, double cupmatdens, double lqdens, double amnt){
setCup(diameter,innerht,t,cupmatdens); fill(amnt); lq.density=lqdens;}
};
/*The cup material and the liquid are included here as separate classes to as a design measure to make room for any future expansion of the concepts of liquid and solid material. Another design direction is to include their densities in the cup class.*/
class SolidMaterial{public: double density;
SolidMaterial(){density=0;};
SolidMaterial(double dens){density=dens;}};
class Liquid{public: double density;
Liquid(){density=0;};
Liquid(double dens){density=dens;}};
/*A cylinder object to use for cylinder proprieties; because our cup can be thought of as two coaxial cylinders.*/
class Cylinder{public: double radius; double height;
double volume(){return PI*radius*radius*height;}
Cylinder(){radius=0; height=0;};
Cylinder(double r, double h){radius=r; height=h;}
};
class Cup{private:
/*The inner cylinder, which constitutes inner cavity of the cup*/
Cylinder innerCylinder;
/*The outer cylinder, determined by the inner cylinder and the cup thickness.*/
Cylinder outerCylinder(){return Cylinder(innerCylinder.radius+thickness,innerCylinder.height+thickness);}
double thickness;
SolidMaterial cupMaterial;
Liquid lq;
double lqamount;
public:
/*Setting the cup attributes. The diameter is used because real life cylinders are normally given by their diameter. Using the radius instead is also fine.*/
void setCup(double diameter, double innerht, double t, double cupmatdens){
innerCylinder.radius=diameter/2; innerCylinder.height=innerht;
thickness=t; cupMaterial.density=cupmatdens; lqamount=0;}
/*Set liquid density. We can also use a liquid object.*/
void setLiquid(double density){lq.density=density;}
/*Fill the cup with a certain amount. If the amount exceeds the remaining capacity of the cup fill to brim.*/
void fill(double amount){
if(amount<=innerCylinder.volume()-lqamount){
lqamount+=amount;
}
else
{fill();}
}
void fill(){lqamount=innerCylinder.volume();}
/*Empty a certain amount from the cup. If the amount to be poured exceeds the amount in the cup, empty the cup.*/
void empty(double amount){
if(amount<=lqamount){
lqamount-=amount;
}
else
{empty();}
}
void empty(){lqamount=0;}
/*Take note of how we got the outer cylinder volume. Try to explain it to yourself.*/
double tare(){return (outerCylinder().volume()-innerCylinder.volume())*cupMaterial.density;}
double lqweight(){return lqamount*lq.density;}
double weight(){return tare()+lqweight();}
Cup(){setCup(0,0,0,0); lqamount=0;}
/*Construct an empty cup*/
Cup(double diameter, double innerht, double t, double cupmatdens, double lqdens){
setCup(diameter,innerht,t,cupmatdens); lqamount=0; lq.density=lqdens;}
/*Construct a cup that has a certain amount of liquid*/
Cup(double diameter, double innerht, double t, double cupmatdens, double lqdens, double amnt){
setCup(diameter,innerht,t,cupmatdens); fill(amnt); lq.density=lqdens;}
};
No comments:
Post a Comment