Question

In: Computer Science

Circle Class Write a Circle class that has the following member variables: • radius: a double...

Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the circle, which is calculated as area = pi * radius * radius • getDiameter. Returns the diameter of the circle, which is calculated as diameter = radius * 2 • getCircumference. Returns the circumference of the circle, which is calculated as circumference = 2 * pi * radius Write a program that demonstrates the Circle class by asking the user for the circle’s radius, creating a Circle object, and then reporting the circle’s area, diameter, and circumference. SAMPLE RUN #0: ./circle_Non-Interactive Interactive Session Standard Error (empty) Standard Output Hide Invisibles Highlight: NoneStandard Input OnlyPrompts OnlyStandard Output w/o PromptsFull Standard OutputAllShow Highlighted Only Calling·default·constructor:·The·circle's·radius·in·the·default·Circle·Object·is:·0↵ ↵ Calling·setRadius(20)·to·change·radius·of·default:↵ The·circle's·radius·in·the·default·Circle·Object·is·Now:·20↵ ↵ Creating·Circle·circle2(10):·↵ The·circle2's·radius·in·the·circle2·Object·is:·10↵ ↵ The·circle2's·area·is·therefore:·314.159↵ The·circle2's·diameter·is·therefore:·20↵ The·circle2's·circumference·is·therefore:·62.8318↵ out put must be like sample run. HELP PLEASE ANSWER IN C++

Solutions

Expert Solution

#include <iostream>
using namespace std;

class Circle
{
   private:
   double radius;
   double pi = 3.14159;
  
   public:

// constructors
   Circle()
   {
       radius = 0.0;
   }
   Circle(double radius)
   {
       this->radius = radius;
   }
   void setRadius(double radius)
   {
       this->radius = radius;
   }
   double getRadius()
   {
       return radius;
   }
   double getArea()
   {
       return pi*radius*radius;
   }
   double getDiameter()
   {
       return 2*radius;
   }
   double getCircumference()
   {
       return 2*pi*radius;
   }
};
  
int main() {
  
   Circle c;
   cout<<"Calling·default·constructor:";
   cout<<"\nThe·circle's·radius·in·the·default·Circle·Object·is:"<<c.getRadius();
   cout<<"\nCalling·setRadius(20)·to·change·radius·of·default:";
   c.setRadius(20);
   cout<<"\nThe·circle's·radius·in·the·default·Circle·Object·is·Now:"<<c.getRadius();
  

   cout<<"\nCreating·Circle·circle2(10):";
   Circle circle2(10);
      
   cout<<"\nThe·circle2's·radius·in·the·circle2·Object·is:"<<circle2.getRadius();
   cout<<"\nThe·circle2's·area·is·therefore:"<<circle2.getArea();
   cout<<"\nThe·circle2's·diameter·is·therefore:"<<circle2.getDiameter();
   cout<<"\nThe·circle2's·circumference·is·therefore:"<<circle2.getCircumference();
  
  
   return 0;
}

Output:

Calling·default·constructor:
The·circle's·radius·in·the·default·Circle·Object·is:0
Calling·setRadius(20)·to·change·radius·of·default:
The·circle's·radius·in·the·default·Circle·Object·is·Now:20
Creating·Circle·circle2(10):
The·circle2's·radius·in·the·circle2·Object·is:10
The·circle2's·area·is·therefore:314.159
The·circle2's·diameter·is·therefore:20
The·circle2's·circumference·is·therefore:62.8318

Do ask if any doubt. Please up-vote.


Related Solutions

Write a Circle class that has the following member variables: radius as a double PI as...
Write a Circle class that has the following member variables: radius as a double PI as a double initialized with 3.14159 The class should have the following member functions: Default constructor Sets the radius as 0.0 and pi as 3.14159 Constructor Accepts the radius of the circles as an argument setRadius A mutator getRadius An accessor getArea Returns the area of the circle getDiameter Returns the diameter of the circle getCircumference Returns the circumference of the circle Write a program...
Write a Circle Class that has the following fields: • radius: a double • PI: a...
Write a Circle Class that has the following fields: • radius: a double • PI: a final double initialized with the value 3.14159 • Constructor. Accepts the radius of the circle as an argument • Constructor. A no-arg constructor that sets the radius field to 0.0. • setRadius. A mutator method for the radius field. • getRadius. An accessor method for the radius field. • getArea. Returns the area of the circle, which is calculated as area = PI *...
Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the...
Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the Circle class, you should test this class from the main() method by passing objects of this class to a method “ public static void printAreas(Circle c, int times)” You should display the area of the circle object 5 times with different radii. 2 java.util.Random +Random() +Random(seed: long) +nextInt(): int +nextInt(n: int): int +nextLong(): long +nextDouble(): double +nextFloat(): float +nextBoolean(): boolean Constructs a Random object...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and complexNum of type double. Your class shall have following member functions Complex(); Complex(double, double); Complex add(Complex num1, Complex num2); Complex subtract(Complex num1, Complex num2); string printComplexNumber(); Write a driver program to test your code. Please make sure your code is separated into Complex.h Containing definition of the class Complex.cpp Containing the implementation ComplexTestProgram.cpp Containing main program Use the following directive in class definition to...
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this...
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this radius, the circumference of the circle, the area of the circle, the surface area of a sphere with that radius and the area of a sphere with that radius. Each of its computation functions returns its answers in call by reference parameters. Here are the formulas for computation: Circumference = 2 • π • r Circle Area = π • r2 Sphere Surface Area...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the class are: NUM_EMPLOYEES: a constant integer variable to hold the number of employees. employeeId. An array of integers to hold employee identification numbers. hours. An array of integers to hold the number of hours worked by each employee payRate. An array of doubles to hold each employee’s hourly pay rate wages. An array of seven doubles to hold each employee’s gross wages The class...
write the code in python Design a class named PersonData with the following member variables: lastName...
write the code in python Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool ....
Create an object called Circle. Declare the following integer variables for the object Circle, radius, diameter,...
Create an object called Circle. Declare the following integer variables for the object Circle, radius, diameter, and pi is declared as double. Create the following for the Circle object: ● Implicit constructor (default constructor) ● Void method Calculate (double pi, int radius) to calculate the area of the Circle object. The method must include a system.out statement that displays the area value. Use the following formula to calculate area of the circle: Area = pi * (r * r) Your...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function that returns r value of property variable -Put(int d): A function that stores d in attribute variable r *Declare a one-dimensional array of type Circle and in each array element Read and store integers from standard input device *Declare the swap() function to swap two elements with each other *Write a program that sorts the elements of a one-dimensional array of circle type in...
/*Design and code a class Calculator that has the following * tow integer member variables, num1...
/*Design and code a class Calculator that has the following * tow integer member variables, num1 and num2. * - a method to display the sum of the two integers * - a method to display the product * - a method to display the difference * - a method to display the quotient * - a method to display the modulo (num1%num2) * - a method toString() to return num1 and num2 in a string * - Test your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT