Write a C++ program which has a main-driver and
creat a polygon class Poly which has an array of n pairs of floats,
x[i] and y[i],
creat a derived class Triangle,
creat a derived class Quadrilateral class (which you may assume is
convex and points given clockwise)
You need to compute the area for the two derived classes
but use inheritance to compute perimeter in all these
classes.
Constructors, accessors, mutators, and anything else needed should
be written too.
Make sure to request points at constructor time in the
driver.
The driver should show a menu like: P for Polygon, T for Triangle,
and Q for Quadrilateral.
After recieving the data, the driver should print the perimeter and
area.
ScreenShot
-------------------------------------------
Program
//Header files
#include<iostream>
#include <iomanip>
using namespace std;
//Base class poly
class poly {
//Member variables
public:
int n;
float *x,*y;
//Constructor
poly(int size) {
n = size;
x=new float[n];
y=new float[n];
}
//Mutators
void setXvertices(float *xVer) {
*x = *xVer;
}
void setYvertices(float *yVer) {
*y = *yVer;
}
//Accessors
float* getXvertices() {
return x;
}
float* getYvertices() {
return y;
}
//Peimeter finding method
double Perimeter() {
double perimeter = 0;
for (int i = 0; i < n; i++)
{
perimeter +=
sqrt(pow((*x+(i + 1) - *x+i), 2) + pow((*y + (i + 1) - *y + i),
2));
}
return perimeter;
}
//Area finding method
double Area() {
double area = 0;
// Calculate value of shoelace
formula
int j = n - 1;
for (int i = 0; i < n;
i++)
{
area += (*x+j +
*x+i) * (*y+j - *y+i);
j = i; // j is
previous vertex to i
}
// Return absolute value
return abs(area / 2.0);
}
};
//Child class
class triangle:public poly {
//Member method
public:
//Constructor
triangle(int n) :poly(n) {
n = 3;
}
//Area calculator
double Area() {
//double b= sqrt(pow((*x + 2 - *x +
1), 2) + pow((*y + 2 - *y + 1), 2)))
double apothem= (sqrt(pow((*x + 3 -
*x + 2), 2) + pow((*y + 3 - *y + 2), 2)));
double area =(
(Perimeter()*apothem)/2);
// Calculate value of shoelace
formula
return area;
}
};
//Child class
class quadilateral :public poly {
//Member methods
public:
//Constructor
quadilateral(int n) :poly(n) {
n = 4;
}
//Area calculation
double Area() {
//double b= sqrt(pow((*x + 2 - *x +
1), 2) + pow((*y + 2 - *y + 1), 2)))
double apothem = (sqrt(pow((*x + 2
- *x + 1), 2) + pow((*y + 2 - *y + 1), 2)));
double area =
((Perimeter()*apothem) / 2);
// Calculate value of shoelace
formula
return area;
}
};
//Driver method
int main()
{
//Variables for choice and input and array
char ch;
int v;
float *x, *y;
cout << " User
menu :" << endl;
//Prompt user for choice
cout <<
"(P)olygon\n(T)riangle\n(Q)uadrilateral\n Enter your
choice:";
cin >> ch;
//If polygon
if (ch == 'p' || ch == 'P') {
//Prompt for number of
vertices
cout << "Enter the number of
vertices:";
cin >> v;
//Set array that size
x = new float[v];
y = new float[v];
//Object creation
poly p(v);
//Values of vertices
cout << "Enter x
values:";
for (int i = 0; i < v; i++)
{
cin >>
x[i];
}
cout << "Enter y
values:";
for (int i = 0; i < v; i++)
{
cin >>
y[i];
}
//Set vertices array
p.setXvertices(x);
p.setYvertices(y);
//Display area and perimeter
cout << "Perimeter of
Polygon=" << setprecision(2) << p.Perimeter() <<
endl;
cout << "Area of Polygon="
<< setprecision(2) << p.Area() << endl;
}
//If triangle
else if (ch == 't' || ch == 'T') {
//set array size
v = 3;
x = new float[v];
y = new float[v];
//object creation
triangle t(v);
//values
cout << "Enter x
values:";
for (int i = 0; i < v; i++)
{
cin >>
x[i];
}
cout << "Enter y
values:";
for (int i = 0; i < v; i++)
{
cin >>
y[i];
}
//set array
t.setXvertices(x);
t.setYvertices(y);
//Display result
cout << "Perimeter of
Triangle=" << setprecision(2) << t.Perimeter() <<
endl;
cout << "Area of Tringle="
<< setprecision(2) << t.Area() << endl;
}
//If quadrilateral
else if (ch == 'q' || ch == 'Q') {
//Set array size
v = 4;
x = new float[v];
y = new float[v];
//Object creation
quadilateral q(v);
cout << "Enter x
values:";
for (int i = 0; i < v; i++)
{
cin >>
x[i];
}
cout << "Enter y
values:";
for (int i = 0; i < v; i++)
{
cin >>
y[i];
}
//set values in arrays
q.setXvertices(x);
q.setYvertices(y);
//display result
cout << "Perimeter of
Quadrilateral=" << setprecision(2) << q.Perimeter()
<< endl;
cout << "Area of
Quadrilateral=" << setprecision(2) << q.Area() <<
endl;
}
//Wrong choice
else {
cout << "Wrong choice"
<< endl;
}
return 0;
}
-------------------------------------------------------------------------
Output
User menu :
(P)olygon
(T)riangle
(Q)uadrilateral
Enter your choice:q
Enter x values:1
2
3
4
Enter y values:5
6
7
8
Perimeter of Quadrilateral=23
Area of Quadrilateral=48
Press any key to continue . . .
Write a C++ program which has a main-driver and creat a polygon class Poly which has...
Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String input =""; System.out.println("Welcome to the easy square program"); while(true) { System.out.println("Enter the length of the side of a square or enter QUIT to quit"); try { input = keyboard.nextLine(); if(input.equalsIgnoreCase("quit")) break; int length = Integer.parseInt(input); Square s = new Square(); s.setLength(length); s.draw(); System.out.println("The area is "+s.getArea()); System.out.println("The perimeter is "+s.getPerimeter()); } catch(DimensionException e)...
N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and whose angles have the same degree. Write a class called NSidedPolygon that contains the following components: Private members to store the name of a polygon, the number of sides, and the length of each side. (You are expected to select the appropriate data types for each member.) A constructor that accepts the number of sides and the length of each side. A private method...
Java Lab In this lab, you will be implementing the following class hierarchy. Your concrete classes must call the superclass constructor with the proper number of sides (which is constant for each concrete shape). The perimeter method is implemented in the superclass as it does not change based on the number of sides. The area method must be overridden in each subclass as the area is dependent on the type of polygon. The areas of an equilateral triangle, square, and...
java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...
You will be creating a driver class and 5 class files for this assignment. The classes are Shape2D (the parent class), Circle, Triangle, and Rectangle (all children of Shape2D) and Square (child of Rectangle). Be sure to include a message to the user explaining the purpose of the program before any other printing to the screen. Within the driver class, please complete the following tasks: Instantiate a Circle object with 1 side and a radius of 4; print it Update...
Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations. Learning Objectives The focus of this assignment is on the following learning objectives: • Be able to identify the contents of...
Using C++, this assignment uses two classes which utilize the concept of aggregation. One class will be called TimeOff. This class makes use of another class called NumDays. Then you will create a driver (main) that will handle the personnel records for a company and generate a report. TimeOff class will keep track of an employee’s sick leave, vacation, and unpaid time off. It will have appropriate constructors and member functions for storing and retrieving data in any of the...
In C++, Step 1: Implement the Student Class and write a simple main() driver program to instantiate several objects of this class, populate each object, and display the information for each object. The defintion of the student class should be written in an individual header (i.e. student.h) file and the implementation of the methods of the student class should be written in a corresponding source (i.e. student.cpp) file. Student class - The name of the class should be Student. -...
J Inc. has a file with employee details. You are asked to write a C++ program to read the file and display the results with appropriate formatting. Read from the file a person's name, SSN, and hourly wage, the number of hours worked in a week, and his or her status and store it in appropriate vector of obiects. For the output, you must display the person's name, SSN, wage, hours worked, straight time pay, overtime pay, employee status and...
Program Challenge 10 Design a Ship class that the following members: · A field for the name of the ship (a string). · A field for the year that the ship was built (a string). · A constructor and appropriate accessors and mutators. · A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: · A field for the...