Question

In: Computer Science

Do the problems found week 4 - Programs 1,2,3,4,5,6 and 7.        * Be sure to...

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;

    }

Solutions

Expert Solution

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:


Related Solutions

This week will discuss TRICARE and managed care workers compensation programs. Be sure to answer questions...
This week will discuss TRICARE and managed care workers compensation programs. Be sure to answer questions for both of these topics in your initial response to the Discussion Board. TRICARE: What are three health care options of TRICARE? What is the role of a TRICARE PCM (Primary Care Manager)? Workers Compensation: Review the definition of managed care in Chapter 3, and also read page 619 before responding to the following: List at least 3 benefits from incorporating managed care into...
Provide a brief explanation of the accrediting or regulatory body you found in week 7; what...
Provide a brief explanation of the accrediting or regulatory body you found in week 7; what area of health care does the agency work in, what are their primary aims and what is its brief history? Who accredits the accreditors? J ust think about that one for a moment. Mostly it is the government (state and federal) but are there other quasi-governmental agencies or groups? What is the value of regulation and accreditation? Does regulation help or hinder efficiency? Quality?...
Business ethics This week we are covering the textbook topics found in Chapter 4, The Nature...
Business ethics This week we are covering the textbook topics found in Chapter 4, The Nature of Capitalism (beginning on page 117) and Chapter 5 Corporation (beginning on page 156). For this week’s discussion you need to post your thoughts on the following questions. Per our textbook, there are four key features of capitalism which include the existence of companies, the goal of making a profit, a competitive market, and private property ownership. How do these four features of capitalism...
Use the financial statements below for problems 4 to 7                               Balance Sheet Assets
Use the financial statements below for problems 4 to 7                               Balance Sheet Assets 2017 2018 Cash $14,000 $9,282 Short-term investments. 48,600 18,000 Accounts receivable 351,200 632,160 Inventories 710,200 1,287,360    Total current assets $1,124,000 $1,946,802 Gross fixed assets 491,000 1,202,950 Less: accumulated depreciation 146,200 263,160    Net fixed assets $344,800 $939,790 Total assets $1,468,800 $2,886,592 Liabilities and equity 2017 2018 Accounts payable $145,600 $324,000 Notes payable 200,000 720,000 Accruals 136,000 284,960    Total current liabilities $481,600 $1,328,960 Long-term debt...
Please do both problems. They are short. I will rate for sure. Thanks! Question# 1 Let...
Please do both problems. They are short. I will rate for sure. Thanks! Question# 1 Let p(x) = x^3 + 3x + 1 = (x + 3)^2 (x + 4) in Z5[x]. (a) Perform the following computations in Z5[x]/(p(x)). Give your answers in the form [r(x)] where r(x) has degree as small as possible. i. [4x] + [3x^2 + x + 2] ii. [x^2 ][2x^2 + 1] (b) Show that Z5[x]/(p(x)) has zero divisors Question #2 Let p(x) = x^3...
Question 2 (7 marks) (Note this question is from the Week 4 Tutorial) (a) What are...
Question 2 (Note this question is from the Week 4 Tutorial) (a) What are the most common reasons for a corporation to reduce its share capital? (b) What are the allowable methods of reducing share capital? (c) Discuss the differences between a share buyback and a capital reduction. (d) What are the different types of debt instruments discussed in this unit? (1 mark)
Week 4 lab A& P the muscular system 7. T or F The influx of sodium...
Week 4 lab A& P the muscular system 7. T or F The influx of sodium ions generates an action potential at the motor end plate 8. A ____ consist of a motor nerve fiber and all the muscle fibers it stimulates at neuromuscular junctions. 9. What is ACh? Explain what it is and it's function during the sequence of events at the neuromuscular junction. 18. The extensor digitorum longus muscle is located in the ___ compatment of the leg....
Module 4 Worksheet—Chapters 6 & 7 Please complete the problems in Excel on this worksheet and...
Module 4 Worksheet—Chapters 6 & 7 Please complete the problems in Excel on this worksheet and upload to the drop box for module 4 no later than Sunday, 11:55PM. Don’t round – use four decimal places until the final answer. 1. AA Corporation’s stock has a beta of 1.6. The risk-free rate is 2.5% and the expected return on the market is 14.5%. What is the required rate of return on AA’s stock? 3. Suppose you manage a $6 million...
The following data apply to Computational Problems 7‐1 through 7‐4. Assume expected returns and standard deviations...
The following data apply to Computational Problems 7‐1 through 7‐4. Assume expected returns and standard deviations as follows: EG&G Return (%)   25 Standard deviation (%)   30 Covariance   112.5 The correlation coefficient, ρ, is 0.15. GF 23 25 EG&G wi 1.0 0.8 0.6 0.2 0.0 GF wj = (1 − wi) 0.0 0.2 0.4 0.8 1.0 (1) Portfolio Expected Returns (%) 25.0 24.6 24.2 23.4 23.0 (2) Variance 900 637 478 472 625 (3) Standard Deviation (%) 30.0 25.2 21.9 21.7...
Why do lactose intolerant people have problems when they consume dairy products? Be sure to frame...
Why do lactose intolerant people have problems when they consume dairy products? Be sure to frame your answer within the context of the genetics and biochemistry involved.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT