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...
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...
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?");...
a_list = [1, 2, 3, 4, 5] list_index = 7 #----------------------------------------------------------- #You may modify the lines...
a_list = [1, 2, 3, 4, 5] list_index = 7 #----------------------------------------------------------- #You may modify the lines of code above, but don't move them! #When you Submit your code, we'll change these lines to #assign different values to the variables. #In this problem, we're going to use some unfamiliar syntax. #You'll learn more about this syntax in Unit 4. For now, #though, you don't need to understand the syntax. All you #need to know is that right now, this code will...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to double to cost of the ticket in a construction zone. // This project will calculate a speeding ticket between 0 to 150 mph. // 1. Ask for speed. // 2. Input speed of vehicle // 3. Calculate ticket cost (50$ if over 50mph with an additional 5$ for every mph over). // 4. Display cost of ticket. #include<iostream> using namespace std; int main() {...
1. Chapter 5, Programming Challenge #8, Conversion Program (page 314). Program Name: FinalExamConversion. Write a program...
1. Chapter 5, Programming Challenge #8, Conversion Program (page 314). Program Name: FinalExamConversion. Write a program that asks the user to enter a distance in meters. The program will then present the following menu of selection: 1. Convert to Kilometers 2. Convert to Inches 3. Convert to Feet 4. Quit the Program The program will convert the distance to kilometers, inches or feet, depending on the user’s selection. Write the following methods: • getInput: This method prompts user to enter...
1. Some bene?ts of the OAS program are means-tested, while the CPP program is universal. Discuss...
1. Some bene?ts of the OAS program are means-tested, while the CPP program is universal. Discuss two reasons why a universal program might be preferable over a means-tested program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT