Question

In: Computer Science

1.) First Question on Class – the class Circle Given the code below, modify it so...

1.) First Question on Class – the class Circle Given the code below, modify it so that it runs. This will require you to add a class declaration and definition for Circle. For the constructor of Circle that takes no arguments, set the radius of the circle to be 10. You are to design the class Circle around the main method. You may NOT modify the body of the main method in anyway, if you do your code WILL NOT BE ACCEPTED, AND WILL BE GRADED AS ALL WRONG. For this question, YOU MUST capture the output of a run of your program and submit it with your source code as your solution. (TIP: the formula to find the area of a Circle is pi times r squared, or PI * r * r).

#include using namespace std;

const float PI = 3.1416; i

nt main() {

Circle c1, c2, c3; c

1.setRadius(1.0);

c3.setRadius(4.5);

Circle circles[] = {c1, c2, c3};

for (int i = 0; i < 3; i++) {

float rad, diam, area;

Circle c = circles[i];

rad = c.getRadius();

diam = c.getDiameter();

area = c.getArea();

cout << "circle " << (i) << " has a radius of: " << rad << ", a diameter of: " << diam << ", and an area of: " << area << endl;

}

return 0;

The language is C++, thanks in advance

Solutions

Expert Solution

Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.

CODE

#include<iostream>
using namespace std;

//constant variable PI
const float PI = 3.1416;

class Circle
{
   private:
       //private variable radius
       float radius;
   public:
       //constructor used to set radius as 10
       Circle()
       {
           radius = 10;
       }
       //setRadius sets argument as radius
       void setRadius(float r)
       {
           radius = r;
       }
       //getRadius returns radius
       float getRadius()
       {
           return radius;
       }
       //getDiameter returns diameter
       float getDiameter()
       {
           return radius * 2;
       }
       //getArea returns area
       float getArea()
       {
           return PI * radius *radius;
       }
};

int main()
{
   //objects of circles
   Circle c1, c2, c3;
  
   //setting radius for object c1
   c1.setRadius(1.0);
  
   //setting radius for object c3
   c3.setRadius(4.5);
  
   //creating array of objects
   Circle circles[] = {c1, c2, c3};
  
   for (int i = 0; i < 3; i++)
   {
       //variables
       float rad, diam, area;
      
       //calling each object
       Circle c = circles[i];
      
       //getting radius
       rad = c.getRadius();
      
       //getting diameter
       diam = c.getDiameter();
      
       //getting area
       area = c.getArea();
      
       //printing outputs
       cout << "circle " << (i) << " has a radius of: " << rad << ", a diameter of: " << diam << ", and an area of: " << area << endl;
   }
return 0;
}

OUTPUT

CODE SCREEN SHOT


Related Solutions

Exercise 1 (a) Use javascript modify the code below, so that in addition to outputting the...
Exercise 1 (a) Use javascript modify the code below, so that in addition to outputting the selection to the web page, the selection is also placed in the browser’s local storage. Use ‘select’ as the local-storage key. The value will be the name of the category that was selected or the empty string is no selection was made. (d) Add a button called ‘retrieve’. When it is clicked the local storage is read and the prior selection is shown on...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use shallow copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
Given C++ Stack Code, Modify the code to work like a Queue.(First in First out) Stack...
Given C++ Stack Code, Modify the code to work like a Queue.(First in First out) Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal =...
Note- can you rewrite the code in C++. Circle Class c++ code Write a circle class...
Note- can you rewrite the code in C++. Circle Class c++ code 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 acccssor function...
The code below is giving an arithmetic overflow. Correct the below given the below code, so...
The code below is giving an arithmetic overflow. Correct the below given the below code, so that the value of s0 is printed before calling the function fun1, inside the function Fun1 and after returning from Fun1 in main. Upload the corrected .asm or .s file in the drop box. .text main: addi $s0,$zero,2 jal Disp jal Fun1 jal Disp j Exit Fun1: addi $sp,$sp,-4 sw $s0,0($sp) addi $s0,$s0,15 jal Disp lw $s0,0($sp) addi $sp,$sp,4 jr $ra Disp: li $v0,1...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha so the main function is working properly. #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class alpha { private: int data; public: //YOUR CODE }; //////////////////////////////////////////////////////////////// int main() { alpha a1(37); alpha a2; a2 = a1; cout << "\na2="; a2.display(); //display a2 alpha a3(a1); //invoke copy constructor cout << "\na3="; a3.display(); //display a3 alpha a4 = a1; cout << "\na4="; a4.display(); cout << endl; return 0;...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates the Fibonacci sequence and writes the sequence to the shared-memory object. The Consumer.c file should then output the sequence. Producer.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <zconf.h> int main() { /* The size (in bytes) of shared-memory object */ const int SIZE = 4096; /* The name of shared-memory object */ const char *Obj =...
c++ question Question 2: X = 7777 Modify the code below and use conditional branching (if,...
c++ question Question 2: X = 7777 Modify the code below and use conditional branching (if, else, etc.) to print the following: if X is between 0 and 33333 then print the word: red if X is between 33334 and 66666 then print the word: blue if X is between 66667 and 99999 then print the word: green if none of the above condition matches then print the word: white
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT