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 4:
#define SIZE 7
#include<string>
#include <iostream>
using namespace std;
class PickColor
{
private:
string myColors[SIZE];
public:
void setElement( int index, string color)
{
myColors[index]=color;
}
void printColors()
{
cout<<"All the colors in the array are : "<<endl;
for(int i=0;i<SIZE;i++)
cout<< myColors[i]<<" ";
cout<<endl;
}
void printRandomColors()
{
cout<<"Randomly pick One color from the array is ";
cout<< myColors[(rand()%7)]<<endl;
}
};
int main()
{
cout<<"NAME-DATE";
PickColor p1;
p1.setElement(0,"red");
p1.setElement(1,"orange");
p1.setElement(2,"yellow");
p1.setElement(3,"green");
p1.setElement(4,"blue");
p1.setElement(5,"indigo");
p1.setElement(6,"violet");
p1.printColors();
p1.printRandomColors();
p1.printRandomColors();
return 0;
}
Program 5:
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area () {return width*height;}
};
void Rectangle::set_values (int x, int y) {
width = x;
height = y;
}
int main () {
cout<<"NAME-DATE";
Rectangle rect, rectb;
rect.set_values (2,8);
rectb.set_values (2,3);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
Program 4:
// PickColor.h
#ifndef PICKCOLOR_H_
#define PICKCOLOR_H_
#include <iostream>
using namespace std;
#define SIZE 7
class PickColor
{
private:
string myColors[SIZE];
public:
PickColor();
PickColor(string color[SIZE]);
void setElement( int index, string color);
void printColors();
void printRandomColors();
};
#endif /* PICKCOLOR_H_ */
//end of PickColor.h
// PickColor.cpp
#include "PickColor.h"
#include <cstdlib>
PickColor::PickColor()
{}
PickColor::PickColor(string color[SIZE])
{
for(int i=0;i<SIZE;i++)
myColors[i] = color[i];
}
void PickColor:: setElement( int index, string color)
{
myColors[index]=color;
}
void PickColor:: printColors()
{
cout<<"All the colors in the array are : "<<endl;
for(int i=0;i<SIZE;i++)
cout<< myColors[i]<<" ";
cout<<endl;
}
void PickColor:: printRandomColors()
{
cout<<"Randomly pick One color from the array is ";
cout<< myColors[(rand()%7)]<<endl;
}
//end of PickColor.cpp
// main.cpp : Driver program to test PickColor class
#include "PickColor.h"
#include <iostream>
using namespace std;
int main()
{
cout<<"NAME-DATE"<<endl;
PickColor p1; // test default constructor
p1.setElement(0,"red");
p1.setElement(1,"orange");
p1.setElement(2,"yellow");
p1.setElement(3,"green");
p1.setElement(4,"blue");
p1.setElement(5,"indigo");
p1.setElement(6,"violet");
p1.printColors();
p1.printRandomColors();
p1.printRandomColors();
string color[] = {"red","blue","green","yellow","black","white","brown"};
PickColor p2(color); // test parameterized constructor
p2.printColors();
p2.setElement(3,"purple");
p2.printColors();
p2.printRandomColors();
p2.printRandomColors();
return 0;
}
//end of main.cpp
Output:
UML of PickColor:
Program 5:
// Rectangle.h
#ifndef RECTANGLE_H_
#define RECTANGLE_H_
class Rectangle
{
int width, height;
public:
Rectangle();
Rectangle(int inWidth, int inHeight);
void set_values (int,int);
int area () ;
};
#endif /* RECTANGLE_H_ */
//end of Rectangle.h
// Rectangle.cpp
#include "Rectangle.h"
Rectangle::Rectangle()
{
width = 0;
height = 0;
}
Rectangle::Rectangle(int inWidth, int inHeight)
{
width = inWidth;
height = inHeight;
}
void Rectangle::set_values (int x, int y) {
width = x;
height = y;
}
int Rectangle::area()
{
return width*height;
}
//end of Rectangle.cpp
//main.cpp : Driver program to test the Rectangle class
#include "Rectangle.h"
#include <iostream>
using namespace std;
int main()
{
cout<<"NAME-DATE"<<endl;
Rectangle rect, rectb(2,3); // test default and parameterized constructor
rect.set_values (2,8);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
//end of main.cpp
Output:
UML of Rectangle class: