Question

In: Computer Science

Modify the program 7-5 (pr7-5.cpp) on page 425 by performing the following: 1. Add an overloaded...

Modify the program 7-5 (pr7-5.cpp) on page 425 by performing the following:

1. Add an overloaded constructor that has a parameter for radius. Negative values should result in radius set to 1.0. (see example 7-6 page 427)

2. Add a member function calcCircumference() to compute and return the circle circumference (2 * 3.14 * radius).

3. In main() "circle1" should be instantiated with a value for radius.

// This program uses a constructor to initialize a member variable.

#include <iostream>

#include <cmath>

using namespace std;

// Circle class declaration

class Circle

{ private:

double radius;

   public: // Member function prototypes

Circle();

void setRadius(double);

double calcArea();

};

// Circle member function implementation section

/********************************************

* Circle::Circle *

* This is the constructor. It initializes *

* the radius class member variable. *

********************************************/

Circle::Circle()

{ radius = 1.0;

}

/********************************************

* Circle::setRadius *

* This function validates the value passed *

* to it before assigning it to the radius *

* member variable. *

********************************************/

void Circle::setRadius(double r)

{ if (r >= 0.0)

radius = r;

// else leave it set to its previous value

}

  

/**********************************************

* Circle::calcArea *

* This function calculates and returns the *

* Circle object's area. It does not need any *

* parameters because it can directly access *

* the member variable radius. *

**********************************************/

double Circle::calcArea()

{ return 3.14 * pow(radius, 2);

}

/***************************************

* main *

* The main function creates and uses *

* 2 Circle objects. *

***************************************/

int main()

{

// Define a Circle object. Because the setRadius function

// is never called for it, it will keep the value set

// by the constructor.

Circle circle1;

// Define a second Circle object and set its radius to 2.5

Circle circle2;

circle2.setRadius(2.5);

// Get and display each circle's area

cout << "The area of circle1 is " << circle1.calcArea() << endl;

cout << "The area of circle2 is " << circle2.calcArea() << endl;

return 0;

}

4. In main() add two more output statements for showing the circumference of each of the circles (circle1 and circle2).

Example 7-6 on page 427 shows how to overload constructors.

Save and submit your file as pr7-5_Lab.cpp.

Solutions

Expert Solution

CODE FOR THE FOLLOWING PROGRAM:-

#include <iostream>
#include <cmath>
using namespace std;

// Circle class declaration
    class Circle{ 
        private:
        double radius;
        public: // Member function prototypes

        Circle();
        Circle(double);
        void setRadius(double);
        double calcArea();
        double calcCircumference();
};

// Circle member function implementation section
/********************************************
* Circle::Circle *
* This is the constructor. It initializes *
* the radius class member variable. *
********************************************/

    Circle::Circle()
    { 
        radius = 1.0;
    }

/********************************************
* Circle::Circle(double) *
* This is a parametrized constructor *
* that has a parameter for radius *
********************************************/

    Circle::Circle(double r)
    { 
        if(r<0){
         radius=1.0;   
        }else{
            radius=r;
        }
    }
/********************************************
* Circle::setRadius *
* This function validates the value passed *
* to it before assigning it to the radius *
* member variable. *
********************************************/

    void Circle::setRadius(double r)
    {    
        if (r >= 0.0)
        radius = r;
    // else leave it set to its previous value
}

/**********************************************
* Circle::calcArea *
* This function calculates and returns the *
* Circle object's area. It does not need any *
* parameters because it can directly access *
* the member variable radius. *
**********************************************/

    double Circle::calcArea()
    { 
        return 3.14 * pow(radius, 2);
    }
    
/**********************************************
* Circle::calcCircumference*
* This function calculates and returns the *
* Circle object's circumference. It does not need any *
* parameters because it can directly access *
* the member variable radius. *
**********************************************/
 double Circle::calcCircumference()
    { 
        return 2*3.14*radius;
    }
    
/***************************************
* main *
* The main function creates and uses *
* 2 Circle objects. *
***************************************/

    int main()
    {
    // Define a Circle object. Because the setRadius function
    // is never called for it, it will keep the value set
    // by the constructor.

    Circle circle1;
    // Define a second Circle object and set its radius to 2.5
    Circle circle2(2.5);
    // Get and display each circle's area
    cout << "The area of circle1 is " << circle1.calcArea() << endl;
    cout << "The area of circle2 is " << circle2.calcArea() << endl;
    // Get and display each circle's circumference
    cout << "The circumference of circle1 is " << circle1.calcCircumference() << endl;
    cout << "The circumference of circle2 is " << circle2.calcCircumference() << endl;
    return 0;
}

SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-

SAMPLE OUTPUT:-

HAPPY LEARNING


Related Solutions

Modify program 4-27 on page 219 as follows: 1) Add a new membership category for STUDENTS...
Modify program 4-27 on page 219 as follows: 1) Add a new membership category for STUDENTS with a monthly rate of $90.00. The menu should have one more choice, and the switch statement should implement one more case. 2) The input for the number of months should be validated for 1 -36 months. No charges should be displayed for incorrect number of months. Instead, an error should be displayed similar with the one for wrong menu choice. // This menu-driven...
C++ please Instructions Download and modify the Lab5.cpp program. Currently the program will read the contents...
C++ please Instructions Download and modify the Lab5.cpp program. Currently the program will read the contents of a file and display each line in the file to the screen. It will also display the line number followed by a colon. You will need to change the program so that it only display 24 lines from the file and waits for the user to press the enter key to continue. Do not use the system(“pause”) statement. Download Source Lab 5 File:...
Modify the program 5-13 from page 279 such that will also compute the class average. This...
Modify the program 5-13 from page 279 such that will also compute the class average. This class average is in addition to each individual student score average. To accomplish this additional requirement, you should do the following: 1. Add two more variables of type double: one for accumulating student averages, and one to hold the class average. Don't forget, accumulator variable should be initialized to 0.0. 2. Immediately after computing individual student average, add a statement that will accumulate the...
In C++ please modify the following program and add characters (char) and names (strings) to be...
In C++ please modify the following program and add characters (char) and names (strings) to be added to the linked list along with integers. The current demo program accepts only integer data, so you would ask the user to select the data type to be added to the linked list. The user should be given the following three choices: (a) whole numbers (b) single characters (c) strings Once the user makes a selection from the list above then your program...
Question 1 Refer to the operations below: Add (10 + 5) Add (4+8) Add (7*2) Add...
Question 1 Refer to the operations below: Add (10 + 5) Add (4+8) Add (7*2) Add (90 – 3) Print list Print peek Remove an item from the list Print list 1.1 Implement the operations above into a Queue structure called q1. 1.2 Implement the operations above into a Stack structure called s1. Name your program Question1_1 for the queue structure and Question1_2 for the stack structure JAVA Language to be used. Please give step by step explanation on how...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
1- Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of...
1- Complete/Modify the code given in quiz3.cpp to calculate the avg and the std deviation of the array a and write the output once to myquiz3outf.txt, and another time to a file to myquiz3outc.txt. (You must write one function for the mean(avg) and one function for the std. #include<iostream> #include<string> #include<fstream> #include<cstdlib> //new g++ is stricter - it rquires it for exit using namespace std; int max(int a[],int dsize){ if (dsize>0){ int max=a[0]; for(int i=0;i<dsize;i++){ if(a[i]>max){ max = a[i]; }...
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java...
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java * description of class Driver here. *these is the main class where it acts like parent class. Compiler will execute firstly static method. import java.util.Scanner; public class Driver {     public static void main (String[] args){         Scanner stdIn = new Scanner(System.in);         String user;         String computer;         char a = 'y';         do {             System.out.println("What kind of Computer would you like?");...
Modify the attached files to do the following in java: 1) Add all necessary Getters and...
Modify the attached files to do the following in java: 1) Add all necessary Getters and Setters to the Class file. 2) Add code to compare two instances of the class to see which one comes before the other one based on the zipcode. //Address1.java public class Address {    // attributes    private String street, aptNum, city, state;    private int zip;    // constructors    public Address(String street, String aptNum, String city, String state, int zip)    {...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ /* Hint: This Queue implementation will always dequeue from the first element of the array i.e, elements[0]. Therefore, remember to shift all elements toward front of the queue after each dequeue. */ public class QueueArray<T> { public static int CAPACITY = 100; private final T[] elements; private int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT