# Work on Static methods OBJECTIVES -continue to practice to write the .docx

14 de Sep de 2022
1 de 9

### Work on Static methods OBJECTIVES -continue to practice to write the .docx

• 1. Work on Static methods OBJECTIVES -continue to practice to write the code for the data type class -Learn what is static members in data type class and how to write static methods - Learn how to access static members of data type class from main() Provide the application that helps users to calculate the area of the following list of shapes. Square Rectangle Triangle Circle Trapezoid Rhombus Kite Parallelogram For each shape, you can ask for all necessary information of the selected shape to calculate its area from the keyboard. The result should be displayed in the following format, for example with selected shape is Rectangle -Manage the menu with the do., while loop to redisplayed the menu after finishing one shape -How to write the switch statement -How to format the output in columns and with 2 decimal digits -How to create the static method in data type class -How to access static method of data type class from main() -The formula to calculate the area of all above shapes - You have to provide the pseudo code of main() at the top of driver class or in the word document with party -Create a project named SU2016LAB6_PART2_YourLastName then add two classes in -Class SU2016LAB6_ShapeArea_yourLastName that includes all the static methods that receive the information input from main(), calculate the area of required shapes and return its area. -Driver class SU2016LAB6_AccessStaticMethod_yourLastName: this class include main() where you can provide the menu to allow users to select the shapes to calculate the area. For each shape, ask for and read the necessary information to calculate the area from the keyboard, then call the static method of class SU2016LAB6_ShapeArea_yourLastName to pass the information in to get the area. After that, display the result as requested format
• 2. Solution #include <iostream> #include <conio.h> #include <math.h> using namespace std; class Shape{ //shape class public: virtual double area(); }; class Rectangle : public Shape {//rectangle class private: double width, length; //sides public: Rectangle(double width, double length) { this->width = width; this->length = length; } double area() { // A = w * l return width * length; }
• 3. }; class Rhombus: public Shape{//rhombus class private: double width, length; //2 diagonals public: Rhombus(double width, double length) { this->width = width; this->length = length; } double area() { return 1/2*width * length; } }; class Kite:public Shape{//kite class private: double width, length; //2 diagonals public: Kite(double width, double length) { this->width = width; this->length = length; } double area() { return 1/2*width * length; } };
• 4. class Parallelogram:public Shape {//parallelogram class private:double width, length; //sides public: Parallelogram(double width, double length) { this->width = width; this->length = length; } double area() { return width * length; } }; class Circle:public Shape {//circle class private: double radius; public: Circle(double radius) { this->radius = radius; } double area() { // A = p r^2 return 22/7 * radius*radius; } }; class Square:public Shape{//square class private: double side; public:
• 5. Square(double side) { this->side = side; } double area() { return side*side; } }; class Triangle:public Shape{//triangle class private: double a, b, c; // sides public: Triangle(double a, double b, double c) { this->a = a; this->b = b; this->c = c; } double area() { // Heron's formula: // A = SquareRoot(s * (s - a) * (s - b) * (s - c)) // where s = (a + b + c) / 2, or 1/2 of the perimeter of the triangle double s = (a + b + c) / 2; return sqrt(s * (s - a) * (s - b) * (s - c)); } }; class Trapezoid:public Shape{//trapezoid class
• 6. private: double a, b, c; // sides public: Trapezoid(double a, double b, double c) { this->a = a; this->b = b; this->c = c; } double area() { return 1/2*(a+b)*c; } }; int main() { // Square test double side; cout<<"Enter the side"<<endl; cin>>side; Shape square = Square(side); cout<<"Shape : Square "<<"side = "<<side<<" Area = " << square.area()<< " "; // Rectangle test double width,length; cout<<"Enter the width and length"<<endl; cin>>width>>length;
• 7. Shape rectangle = Rectangle(width, length); cout<<"Shape : Rectangle "<< "Width = "<<width<<" Length = "<< length << " Area = " << rectangle.area()<< " "; // Triangle test double a, b, c; cout<<"enter the sides of th triangle"<<endl; cin>>a>>b>>c; Shape triangle = Triangle(a,b,c); cout<<"Shape : Triangle " << "Side1 = "<<a <<" "<< "Side2 = " << b<<" Side3 = "<< c << " Area = " << triangle.area()<< " "; // Circle test double radius; cout<<"Enter the radius"<<endl; cin>>radius; Shape circle = Circle(radius); cout<<"Shape : Circle " << "Radius = "<<radius << " Area = " << circle.area()<< " "; // Trapezoid test double a1, b1, c1; cout<<"enter the sides of th trapezoid"<<endl; cin>>a1>>b1>>c1;
• 8. Shape trapezoid =Trapezoid(a,b,c); cout<<"Shape : Trapezoid "<< "Base1 = "<<a1 <<" Base2 = "<< b1<<" Height = "<< c1 <<" Area = " <<trapezoid.area()<< " "; // Rhombus test double diag1,diag2; cout<<"Enter the diagonals"<<endl; cin>>diag1>>diag2; Shape rhombus =Rhombus(diag1, diag2); cout<<"Shape : Rhombus " << "diagonal1 = "<<diag1<<" diagonal2 = " << diag2 << " Area = "<< rhombus.area()<< " "; // Kite test double diag3,diag4; cout<<"Enter the diagonals"<<endl; cin>>diag1>>diag2; Shape kite =Kite(diag3, diag4); cout<<"Shape : Kite "<< "diagonal1 = "<<diag3<<" diagonal2 = " << diag4 <<" Area = "<< kite.area()<< " "; // Parallelogram test double width3, length3;
• 9. cout<<"Enter the width and length"<<endl; cin>>width3>>length3; Shape parallelogram = Parallelogram(width3, length3); cout<<"Shape : parallelogram " << "Base = "<<width3 <<" Length = "<< length3 <<" Area = " << parallelogram.area()<< " "; return 0; }