Write a program that uses the curve class hierarchy

Assignment Help Programming Languages
Reference no: EM13791

1. Object Oriented Programming: Class hierarchies, inheritance and virtual functions

In this part of the assignment you are asked to develop a class structure that can be used to represent closed curves in a two-dimensional plane. Examples of such curves are rectangles, ellipses, squares, triangles, general polygons, etc. To limit the scope of the assignment, we will focus on four special types of curves: ellipses, circles, rectangles and squares. Moreover, we will only work with curves with axes that are parallel to the axes of the coordinate system. In the figure below, you see examples of these objects, as well as example of an object that is not allowed, since it is not parallel to the coordinate axis.

2470_Object Oriented Programming.png

Each object has a position, which can be represented by a coordinate in the (x,y) plane. For example, the place of a circle can be represented by the coordinate of its center. The place of a square can, e.g., be represented by the coordinate of its lower left corner. Further, each object has a size. The size of a circle can be represented by its radius, whereas two numbers are needed to represent the size of a rectangle - its lengths in the x and y direction respectively.

Moreover, there are several common fundamental properties that any closed curve has, e.g., an area and a circumference (i.e., the length of the curve).

It also has a parametric representation, which can be defined as a (non-unique) function p, which takes a number between 0 and 1 and
returns a coordinate, such that the whole curve can be "drawn" by plotting all p(a) for all a between 0 and 1. For example, a circle with center at (x,y) and radius r has the parametric representation p(a) = (x + rx*(2Πa),y + ry*(2Πa).


a) Define classes for a coordinate and for generic closed curves. You may use the following declarations if you wish

class coordinate
{
public:
coordinate() {}
coordinate(double xin,double yin) {x=xin;y=yin;}
double x;
double y;
};

class closedcurve
{
public:
closedcurve();
~closedcurve();
double area();
double circumference();
coordinate place(double a);
};


b) Define child classes ellipse, circle, rectangle and square. The class hierarchy should
be as follows:
class ellipse: public closedcurve
{ ...
}
class circle: public ellipse
{...
}
class rectangle: public closedcurve
{...
}
class square:public rectangle
{...
}

These definitions capture the fact that all these objects are closed curves, that a circle is a special case of an ellipse and that a square is a special case of a rectangle. Implement constructors and destructors, as well as the methods for area and circumference for each of these four object types. The formulas for these methods are standard (e.g., the area of a circle is Πr2 ), except for the formula for the circumference of an ellipse, which is nontrivial.

There are many methods to approximate the circumference of a general ellipse, with major radius of length a and minor radius of length b. One such formula, that you can use, is the so-called Hölder approximation, 4(as+ bs)1/s, where s = ln(2)/ ln? (Π/2)(feel free to use other approximations). Although this formula is, in fact, exact for the case where the ellipse is a circle, the precision will be much lower than the standard formula for the circle's circumference, 2Πr. You are therefore recommended to use the latter formula for circle objects.

c) Implement the method, coordinate place(double a), for the parametric representation, which takes a number between 0 and 1, and returns the coordinate of the curve, for each of the four types of objects. The formulas for rectangles and squares are easy and the formula for the circle was described above. For the ellipse, one parameterization is:

p(a) = (x + rx*(2Πa),y + ry*(2Πa)), where rx and ry are the radiuses in the x and y direction respectively.

d) Add a method void printcurve(int N,char Name[]) to the closedcurve class, which takes a number of points, N, and a file name, Name, as arguments and outputs (x,y) coordinates for N points on the curve to a file with name "Name." For example, in the figures below, the resulting points from a square and a circle were printed to files, using N=200 points. The points were then imported to Excel, and shown using a scatter chart.

When implementing the method, use the ofstream class.

Remember to add #include <fstream> and using namespace std; in the header.

If you have implemented your class hierarchy correctly you should only have to write one method, which works for any object of type closedcurve.

1319_Object Oriented Programming1.png

 
e) Implement a less-than (<) operator for the class that compares the area of one curve to another. As with printcurve above, you should only need to implement a single method.

f) Write a program that uses the curve class hierarchy. The program should define several different objects, output their area, circumference, etc. It should also use the printcurve function. Please provide charts of a couple of objects, similar to the ones above.

g) Incorporate virtual functions to get late binding of objects (if you haven't done so yet). Write a program that asks the user what type of object is needed, e.g., a circle or a square, and then calculates the area of that object. In the spirit of true polymorphism, only one
object should be defined in the program. You are allowed to use terminal input and output in this exercise.

Reference no: EM13791

Questions Cloud

Non-annual interest rates and annuities : Theory of Interest- Non-annual interest rates and annuities
Write pl-sql procedures and functions : Write PL/SQL procedures and functions to populate and query that database
Implement the lexical and syntactic analysis : Implement the lexical and syntactic analysis of Minifun programming language.
Construct a hard-core predicate : Construct a hard-core predicate
Write a program that uses the curve class hierarchy : Write a program that uses the curve class hierarchy. The program should define several different objects, output their area, circumference, etc. It should also use the printcurve function.
Internal audit : Describe and evaluate this type of internal audit. What types of organisation would it be most useful for?
Build the gui layout of the game : Build the Build the GUI layout of the game in java.
Evaluate the basis for the payment to the lender : Evaluate the basis for the payment to the lender and basis for the payment to the company-counterparty.
Analyze the processed data in statistical survey : Analyze the processed data in Statistical survey.

Reviews

Write a Review

 

Programming Languages Questions & Answers

  Create a application using the mvc architecture

create a application using the MVC architecture. No scripting elements are allowed in JSP pages.

  Write a vhdl code for soda vending machine

Write a VHDL code that implements the above soda machine. You have to turn in the following: A state diagram showing the implementation of your design. Clearly show all the states and the conditions on which transitions occur.

  Write functions

These 14 questions covers java class, Array, link list , generic class.

  Design a class-based solution to a problem

To design a class-based solution to a problem using multiple data structures.

  Design a program that assigns seats on an airplane

Write a program that allows an instructor to keep a grade book and also design and implement a program that assigns seats on an airplane.

  Write a paper on memory management

Write a paper on Memory Management

  Programming assignment

If the user wants to read the input from a file, then the output will also go into a different file . If the user wants to read the input interactively, then the output will go to the screen .

  Ethics and social responsibility

Ethics and social responsibility at McDonalds

  Write a prolog program using swi proglog

Write a Prolog program using swi proglog

  Development of the current strategic potential of airline

Evaluate the organisation's current external and internal strategic position

  Design a program that models the worms behavior

Design a program that models the worm's behavior.

  Explain the process to develop a web page locally

Explain the process to develop a Web page locally

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd