Question

In: Computer Science

C++ - When working on this assignment, focus on memorizing the syntax for writing classes. Write...

C++ - When working on this assignment, focus on memorizing the syntax for writing classes.

Write a simple class named Circle, with three private variables: doubles named x, y and radius. The center of the circle is denoted by coordinates (x,y), and the radius of the circle is denoted by radius. It should have public member functions with the following signatures:

void setRadius(double r)
void setX(double value)
void setY(double value)
double getRadius()
double getX()
double getY()
double getArea()
bool containsPoint(double xValue, double yValue)

The first six member functions are straightforward functions to set and get the private member variables.

double getArea()

This member function should return the area of the circle. When you are calculating the area you can use 3.14 for pi.  The formula is radius * radius * pi.

bool containsPoint(double xValue, double yValue)

This member function should return true if the point at (xValue, yValue) is contained in the circle, and false if not. You can determine whether or not a point is in a circle by calculating the distance from the center of the circle (its x and y coordinates) to the point (parameters xValue, yValue). If this distance is less than or equal to the radius then the point is inside the circle. For your reference, here is how to calculate distance between two points (Links to an external site.).

For example, let's say we have instantiated a Circle object named myCircle which has x=2.0, y=3.0, and radius=2.0.

  • myCircle.containsPoint(2.0, 4.0) should return true because (2.0, 4.0) is contained in myCircle.
  • myCircle.containsPoint(2.0, 10.0) should return false because (2.0, 10.0) is not contained in myCircle.

Write a main function that tests your class. It should instantiate a number of circle objects with different radius values and centers. You should test all your member functions until you are confident that they work. At minimum, make sure you try each of the following and provide sample output for each of the below test cases:

  • Create a local circle object and set its x, y, and radius. Verify that its area is calculated correctly.
  • Create a circle pointer, and point it at your local circle object. Use this pointer to set its x, y, and radius values to new values.
  • Using your pointer, verify that your containsPoint() function works by trying a point which is in fact in your circle, and showing it returns true. Also, try a different point which is not in your circle and show it returns false.

Implement the Circle class in separate .h and .cpp files. Circle.h contains your class declaration and Circle.cpp contains your class implementation, i.e. the member function implementations. Don't forget the #ifndef and #define preprocessor directives as necessary. Put your main program into a separate file as well called main.cpp, and put your sample output at the end of main.cpp.

To summarize, at the end of the assignment you should have three files with the following names:

  • Circle.h
  • Circle.cpp
  • main.cpp

When you are finished, create a zip file containing these three files, and submit the zip file via Canvas. Do not submit the three files separately.

Solutions

Expert Solution

Here is C++ Code:

Note: Please put all files in same directory.

circle.h

// File circle.h
// Circle class definition

#ifndef CIRCLE_H
#define CIRCLE_H

class circle
{
public:
  // Member Functions
  // constructor
  circle();

  // Set center coordinates
  void setCoord(double, double);

  // Set radius
  void setRadius(double);

  // Compute the area
  void computeArea();

  // Compute the perimeter
  void computePerimeter();

  // Display attributes
  void displayCircle() const;

  // accessor functions
  double getX() const;
  double getY() const;
  double getRadius() const;
  double getArea() const;
  double getPerimeter() const;

  // setters
  void setX(double);
  void setY(double);

  bool containsPoint(double, double);

private:
  // Data members (attributes)
  double x;
  double y;
  double radius;
  double area;
  double perimeter;
};

#endif // CIRCLE_H

circle.cpp

// File circle.cpp
// Circle class implementation

#include "circle.h"
#include <iostream>
using namespace std;

const double pi = 3.14159;

// Member Functions...
// constructor
circle::circle()
{
  x = 0;
  y = 0;
  radius = 0;
  area = 0.0;
  perimeter = 0.0;
}

// Set center position
void circle::setCoord(double xArg, double yArg)
{
  x = xArg;
  y = yArg;
}

// Set radius
void circle::setRadius(double r)
{
  radius = r;
  computeArea();
  computePerimeter();
}

// Compute the area
void circle::computeArea()
{
  area = pi * radius * radius;
}

// Compute the perimeter
void circle::computePerimeter()
{
  perimeter = 2 * pi * radius;
}

// Setters
void circle::setX(double value)
{
  x = value;
}

void circle::setY(double value)
{
  y = value;
}

// check if circle have point
bool circle::containsPoint(double xValue, double yValue)
{
  if ((xValue - getX()) * (xValue - getX()) +
          (yValue - getY()) * (yValue - getY()) <=
      getRadius() * getRadius())
    return true;
  else
    return false;
}

// Display attributes
void circle::displayCircle() const
{
  cout << "x-coordinate is " << x << endl;
  cout << "y-coordinate is " << y << endl;
  cout << "area is " << area << endl;
  cout << "perimeter is " << perimeter << endl;
}

// accessor functions   

double circle::getX() const
{
  return x;
}

double circle::getY() const
{
  return y;
}

double circle::getRadius() const
{
  return radius;
}

double circle::getArea() const
{
  return area;
}

double circle::getPerimeter() const
{
  return perimeter;
}

main.cpp

// File circletest.cpp
// Tests the Circle class

#include "circle.h"
#include "circle.cpp"
#include <iostream>
using namespace std;

int main()
{
  circle myCircle;

  // Set circle attributes.
  myCircle.setCoord(2.0, 3.0);
  myCircle.setRadius(2.0);

  // Compute area and perimeter
  myCircle.computeArea();
  myCircle.computePerimeter();

  // Display the circle attributes.
  cout << "The circle attributes follow:" << endl;
  myCircle.displayCircle();

  // Check points
  if (myCircle.containsPoint(2.0, 4.0))
  {
    cout << "Circle contains this point.";
  }
  else
  {
    cout << "Circle does not contain this point";
  }
  if (myCircle.containsPoint(2.0, 10.0))
  {
    cout << "Circle contains this point.";
  }
  else
  {
    cout << "Circle does not contain this point";
  }

  return 0;
}

To compile:

g++ main.cpp

./a

Output:


Related Solutions

Focus on clear and crisp writing, with proper grammar, punctuation, and syntax. Each question should be...
Focus on clear and crisp writing, with proper grammar, punctuation, and syntax. Each question should be around 500 words. Put the emphasis on quality over quality. Tell me what you think – and why! Always substantiate your response with evidence. Provide relevant examples from (books, lectures, WSJ), your other courses, outside experience, etc. Q#1 Detail (7) specific differences between global and domestic business and provide concrete examples of each.
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
IN C++, IN C++, IN C++, IN C++, IN C++ Write the A and B classes,...
IN C++, IN C++, IN C++, IN C++, IN C++ Write the A and B classes, the properties of which will produce the expected output with the test code provided, and the characteristics of which are specified below. Note the types of pointers used in the test code. Methods of class A: - void hi () - Prints “Hi A” on the screen. - void selam() - Prints "Selam A" on the screen. Class B must inherit class A. Class...
C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be...
C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be used         - how to instantiate and initialize data members that are user-defined types at the same time
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects •...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects • Methods • Arrays of Primitive Values • Arrays of Objects • Recursion • for and if Statements • Insertion Sort 2. Use the following Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for...
write a report about travel agency in c++ not a program writing just report writing
write a report about travel agency in c++ not a program writing just report writing
Instructions for C++ project This Assignment will Focus on Software Engineering: Using Functions in a Menu...
Instructions for C++ project This Assignment will Focus on Software Engineering: Using Functions in a Menu Driven Program Hint: Chapter 6 Program 6-10 is a great example of this type of programs. Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1...
Writing Assignment - Part I Choose an example of a Change in Accounting principles. Write a...
Writing Assignment - Part I Choose an example of a Change in Accounting principles. Write a statement paper on how this change would affect the financial statements of a company as they adopt the new principle. Prepare this written statement as if to the Board of Directors of your company. Writing Assignment - Part II Your CEO has inquired about the differences between the direct method and the indirect method of presenting the statement of cash flows. Prepare a memo...
writing Subject This week's assignment is to write a piece of fiction using characters or settings...
writing Subject This week's assignment is to write a piece of fiction using characters or settings created by someone else, or using public figures as your main characters. Below are a few options for how to proceed. Take a story that you know really well and write it from the point of view of a different character. Try writing an alternative ending to your favorite story. Take characters from different mediums and tell a story about them. What might an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT