Question

Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

Design a class named Triangle that extends GeometricObject class.
The class contains:
Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.
A no-arg constructor that creates a default triangle with color = "blue", filled = true.
A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true.
The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3().
A function named getArea() that returns the area of this triangle.
A function named getPerimeter() that returns the perimeter of the triangle.
class GeometricObject
{
public:
GeometricObject(string color, bool filled)
{
    this->color = color;
    this->filled = filled;
}

string getColor() const
{ return color; }
void setColor(string color)
{ this->color = color; }
bool isFilled() const
{ return filled; }
void setFilled(bool filled)
{ this->filled = filled;}
string toString() const
{
    return "Geometric object color " + color +
    " filled " + ((filled) ? "true" : "false");
}
private:
string color;
bool filled;
};

//main.cpp

#include <iostream>
#include <string>
#include "source.h"
using namespace std;
int main()
{
Triangle shape1;
cout << shape1.getSide1() << endl;
cout << shape1.getSide2() << endl;
cout << shape1.getSide3() << endl;
cout << shape1.getArea() << endl;
cout << shape1.getPerimeter() << endl;
cout << shape1.getColor() << endl;
cout << shape1.isFilled() << endl;

Triangle shape(1, 1.5, 1);
cout << shape.getColor() << endl;
cout << shape.isFilled() << endl;

shape.setColor("yellow");
shape.setFilled(true);

cout << shape.getSide1() << endl;
cout << shape.getSide2() << endl;
cout << shape.getSide3() << endl;
cout << shape.getArea() << endl;
cout << shape.getPerimeter() << endl;
cout << shape.getColor() << endl;
cout << shape.isFilled() << endl;

return 0;
}


//source.h

???

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I modified the code and here is the solution to the problem.

<source.h> header has the following code:

#include <iostream>
#include<math.h>
using namespace std;
// just one no-arg constructor is added to GeometricObject class. It is explained below why.
// remaining is kept the same
class GeometricObject
{
   public:
       GeometricObject()
       {
           color = "blue";
           filled = true;
       }
       GeometricObject(string color, bool filled)
       {
       this->color = color;
       this->filled = filled;
       }
       string getColor() const
       {
           return color;
       }
       void setColor(string color)
       {
           this->color = color;
       }
       bool isFilled() const
       {
           return filled;
       }
       void setFilled(bool filled)
       {
           this->filled = filled;
       }
       string toString() const
       {
       return "Geometric object color " + color +
       " filled " + ((filled) ? "true" : "false");
       }
       private:
       string color;
       bool filled;
};

//create a Triangle class which extends the GeometricObject class and performs it's own functions
class Triangle : public GeometricObject
{
   //declare the sides as private
   private:
       double side1,side2,side3;
   //remaining all functions are public
   public:
       // a no-arg constructor and initializing the sides to 1 each.
       /*This constructor will look for it's own parent no-arg contructor
       When found will execute the same.
       Hence, it's important to have a no-arg constructor in GeometricObject class*/
       Triangle()
       {
           side1 = 1;
           side2 = 1;
           side3 = 1;
       }
       // with given values change the parameter values using this constructor
       Triangle(double s1, double s2, double s3)
       {
           side1 = s1;
           side2 = s2;
           side3 = s3;
           setColor("blue");
           setFilled(true);
       }
       // return the sides using these getter functions
       double getSide1() const
       {
           return side1;
       }
       double getSide2() const
       {
           return side2;
       }
       double getSide3() const
       {
           return side3;
       }  
       //use this function to return area
       double getArea()
       {
           double s = getPerimeter()/2;
           double ar = sqrt(s*(s-side1)*(s-side2)*(s-side3));
           return ar;
       }
       //use this function to get the perimeter
       double getPerimeter()
       {
           return (side1+side2+side3);
       }
};
main.cpp has the following code:

#include"source.h"int main()
{
   Triangle shape1;
   cout << "The side1 of shape1 object is:\t"<<shape1.getSide1() << endl;
   cout << "The side2 of shape1 object is:\t"<<shape1.getSide2() << endl;
   cout << "The side1 of shape3 object is:\t"<<shape1.getSide3() << endl;
   cout << "The area of shape1 object is:\t"<<shape1.getArea() << endl;
   cout << "The perimeter of shape1 object is:\t"<<shape1.getPerimeter() << endl;
   cout << "The color of shape1 object is:\t"<<shape1.getColor() << endl;
   cout << "Is the triangle filled for shape1 object?\t";
   if(shape1.isFilled()==1)
   cout<<"Yes"<<endl;
   else
   cout<<"No"<<endl;
  
// *****New object creation*****
  
   cout << "\n\n****New Object***\n\n" << endl;
   Triangle shape(1, 1.5, 1);
   cout << "The color of shape object is:\t"<< shape.getColor() << endl;
   cout << "Is the triangle filled for shape object?\t";
   if(shape.isFilled()==1)
   cout<<"Yes"<<endl;
   else
   cout<<"No"<<endl;
   cout<<"Setting color for shape object to yellow...." << endl;
   shape.setColor("yellow");
   cout<<"Setting filled for shape object to true...." << endl;
   shape.setFilled(true);
  
   cout << "The side1 of shape object is:\t"<< shape.getSide1() << endl;
   cout << "The side2 of shape object is:\t"<< shape.getSide2() << endl;
   cout << "The side3 of shape object is:\t"<< shape.getSide3() << endl;
   cout << "The area of shape object is:\t"<< shape.getArea() << endl;
   cout << "The perimeter of shape object is:\t"<< shape.getPerimeter() << endl;
   cout << "The color of shape object is:\t"<< shape.getColor() << endl;
   cout << "Is the triangle filled for shape object?\t";
   if(shape.isFilled()==1)
   cout<<"Yes"<<endl;
   else
   cout<<"No"<<endl;
   return 0;
}

Output:



Add a comment
Know the answer?
Add Answer to:
Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.* A no-arg constructor that creates a default triangle.* A constructor that creates a triangle with the specified side1, side2, and side3.* The accessor methods for all three data fields.* A method named getArea() that returns...

  • Design a general class GeometricObject can be used to model all geometric objects. This class contains...

    Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...

  • Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task...

    Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task Create the following fields and methods for a Triangle class that extends the provided GeometricShapes class public class GeometricShapes { private String color = "red"; private boolean filled; private java.util.Date dateCreated;    public GeometricShapes() { dateCreated = new java.util.Date(); }    public GeometricShapes(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; }    public void setColor (String color)...

  • Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the...

    Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color;...

  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • Must be in Python 3.6 (please have a screen shot on the code) ex 2.14 :...

    Must be in Python 3.6 (please have a screen shot on the code) ex 2.14 : # Enter three points for a triangle x1, y1, x2, y2, x3, y3 = eval(input("Enter three points for a triangle: ")) # Compute the length of the three sides side1 = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) ** 0.5 side2 = ((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 -...

  • Type up the GeometricObject class (code given on the back of the paper). Name this file Geometric...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT