In: Computer Science
Do the problems found week 4 - Programs 1,2,3,4,5,6 and 7.
* Be sure to add your name as a cout in the first lines of each program - else 0 credit.
* Add constructors - a default and parameterized constructor to each.
* Write an .h interface and a .cpp implementation for each class
* Write an Drive/Test file that tests the constructors and functions
* Write a UML class diagram for each class\!!!!!!
Program 6
#include<iostream>
using namespace std;
class Box
{
public:
Box() {
Length = 0;
Width = 0;
Height = 0;
}
double Length;
double Width;
double Height;
double Volume();
double SurfaceArea();
void setWidth ( double n ) {Width = n;}
void setDepth ( double n ) {Length = n;}
void setHeight ( double n ) {Height = n;}
double getWidth () {return Width;}
double getHeight () {return Height;}
double getDepth () {return Length;}
double calcArea () {return 2*(Length*(Width+Height)+Width*Height);}
double calcVolume () {return Length*Width*Height;}
};
int main() {
cout<<"-------- ";
Box B1;
B1.setWidth(2);
B1.setDepth(4);
B1.setHeight(9);
cout << "Height = " << B1.getHeight() << endl;
cout << "Area = " << B1.calcArea() << endl;
cout << "Volume = " << B1.calcVolume() << endl;
Box B2;
B2.setWidth(3);
B2.setDepth(5);
B2.setHeight(10);
cout << "Height = " << B1.getHeight() << endl;
cout << "Area = " << B1. calcArea() << endl;
cout << "Volume = " << B1.calcVolume() << endl;
}
program 7
#include <iostream>
using namespace std;
int main() {
/*
* Circumference = π × diameter = 2 × π × radius
* Area : π r²
* Diameter : 2r
*/
cout<< "Name: "<< endl;
double radius = 0;
cout << "Enter radius of circle: ";
cin >> radius;
if (radius <= 0) {
cout << "Please Enter radius greater than zero" << endl;
return 0;
}
double pie = 22/7.0;
double circumference = 2 * pie * radius;
double area = pie * radius * radius;
double diameter = 2 * radius;
cout << "Circumference of Circle:" << circumference << endl;
cout << "Area of Circle:" << area << endl;
cout << "Diameter of Circle:" << diameter << endl;
cout << "Radius of Circle:" << radius << endl;
return 0;
}
program 2
#include <iostream>
#include <string>
using namespace std;
class myClass{
public:
void setName(string x){
name = x ;
}
string getName(){
return name;
}
private:
string name;
};
int main()
{
cout<<"----";
myClass to;
to.setName("stuff are cool ");
cout << to.getName();
return 0;
}
Program 6:
//Box.h
#include<iostream>
using namespace std;
class Box
{
public:
double Length;
double Width;
double Height;
public:
//Default constructor
Box();
//Parameterized constructor
Box(double ,double,double);
//Setter methods
void setWidth ( double n ) ;
void setDepth ( double n ) ;
void setHeight ( double n );
//getter methods
double getWidth () ;
double getHeight ();
double getDepth () ;
//Methods to calculate area and volume of box
double calcArea () ;
double calcVolume () ;
};
--------------------------------------------------
//Box.cpp
#include <iostream>
#include "Box.h"
using namespace std;
//default constructor
Box::Box()
{
Length = 0;
Width = 0;
Height = 0;
}
//parameterized constructor
Box::Box(double l,double w, double h)
{
Length = l;
Width = w;
Height = h;
}
void Box::setWidth ( double n )
{
Width = n;
}
void Box::setDepth ( double n )
{
Length = n;
}
void Box::setHeight ( double n )
{
Height = n;
}
double Box::getWidth ()
{
return Width;
}
double Box::getHeight ()
{
return Height;
}
double Box::getDepth ()
{
return Length;
}
double Box::calcArea ()
{
return 2*(Length*(Width+Height)+Width*Height);
}
double Box::calcVolume ()
{
return Length*Width*Height;
}
--------------------------------------------------
//main.cpp
#include<iostream>
#include<string>
#include "Box.h"
using namespace std;
//start of main method
int main()
{
string myName= "yourname";
cout<<"Name:
"<<myName<<endl;
//default constructor
Box B1;
cout<<"Testing default
constructor"<<endl;
cout << "Height = " <<
B1.getHeight() << endl;
cout << "Area = " << B1.calcArea()
<< endl;
cout << "Volume = " <<
B1.calcVolume() << endl;
//parmeter constructor
Box B2(1,2,3);
cout<<"Testing parameterized
constructor"<<endl;
cout << "Height = " <<
B2.getHeight() << endl;
cout << "Area = " << B2.calcArea()
<< endl;
cout << "Volume = " <<
B2.calcVolume() << endl;
Box B3;
B3.setWidth(3);
B3.setDepth(5);
B3.setHeight(10);
cout<<"Testing functions "<<endl;
cout << "Height = " <<
B3.getHeight() << endl;
cout << "Area = " << B3. calcArea()
<< endl;
cout << "Volume = " <<
B3.calcVolume() << endl;
system("pause");
return 0;
}
--------------------------------------------------
Sample Output:
Name: yourname
Testing default constructor
Height = 0
Area = 0
Volume = 0
Testing parameterized constructor
Height = 3
Area = 22
Volume = 6
Testing functions
Height = 10
Area = 190
Volume = 150
UML class diagram for Box
-------------------------------------------------
Program 7:
//Circle.h
#include<iostream>
using namespace std;
class Circle
{
public:
double radius;
public:
//Default constructor
Circle();
//Parameterized constructor
Circle(double );
//Setter methods
void setRadius ( double n ) ;
//Setter methods
double getRadius () ;
//Methods to calculate area and volume of box
double calcCircumference() ;
double calcArea () ;
double calDiameter();
};
-----------------------------------------------
//Circle.cpp
#include<iostream>
#include "Circle.h"
using namespace std;
//Default constructor
Circle::Circle()
{
radius=0;
}
//Parameterized constructor
Circle::Circle(double r)
{
radius=r;
}
//Setter methods
void Circle::setRadius ( double r )
{
radius=r;
}
//Getter method
double Circle::getRadius ( )
{
return radius;
}
double Circle::calcCircumference()
{
return 2 * 3.14 * radius;
}
double Circle::calcArea ()
{
return 3.14 * radius * radius;
}
double Circle::calDiameter()
{
return 2 * radius;
}
-----------------------------------------------
//main2.cpp
#include <iostream>
#include "Circle.h"
using namespace std;
int main()
{
cout<< "Name: "<< endl;
double radius = 0;
cout << "Enter radius of circle: ";
cin >> radius;
//declare Circle class object,c
Circle c;
c.setRadius(radius);
cout << "Radius of Circle:" <<
c.getRadius()<< endl;
cout << "Circumference of Circle:" <<
c.calcCircumference() << endl;
cout << "Area of Circle:" << c.calcArea()
<< endl;
cout << "Diameter of Circle:" <<
c.calDiameter() << endl;
system("pause");
return 0;
}
-----------------------------------------------
Sample Output:
Name:
Enter radius of circle: 10
Radius of Circle:10
Circumference of Circle:62.8
Area of Circle:314
Diameter of Circle:20
UML class digram for Circle class:
-----------------------------------------------
Program 2:
//myClass.h
#include <iostream>
#include <string>
using namespace std;
class myClass
{
public:
void setName(string x);
string getName();
private:
string name;
};
-----------------------------------------------
//myClass.cpp
#include<iostream>
#include<string>
#include "myClass.h"
using namespace std;
void myClass::setName(string x)
{
name = x ;
}
string myClass::getName()
{
return name;
}
-----------------------------------------------
//main3.cpp
#include<iostream>
#include<string>
#include "myClass.h"
using namespace std;
int main()
{
cout<<"----";
myClass to;
to.setName("stuff are cool ");
cout << to.getName();
system("pause");
return 0;
}
-----------------------------------------------
Sample Output:
"stuff are cool
UML class diagram for myClass class