Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a program to represent geometric shapes and some operations that can be pe

ID: 3768857 • Letter: W

Question

write a program to represent geometric shapes and some operations that can be performed on them. For each shape, store fundamental data about its size, and provide member functions to access and modify the data. In addition, provide appropriate functions to compute each shape’s area (note that for 3D shape, the area is the surface area) and volume when it applies (for 3D shape only). In your design, consider how shapes are related and thus where inheritance can be implemented. There are various ways to use inheritance to relate these shapes but please follow the inheritance described in the table shown on the next page. The abstract base class Shape is declared as follows:

class Shape

{

public:

    Shape() {}

    virtual double computeArea()=0;

    virtual void expand(int factor)=0;

    virtual void display()=0;

    virtual ~Shape() {}

       //other member functions if you want to add;

};

Your program will consist of the following classes: Shape, Circle, Rectangle, Cuboid, Sphere and Cylinder.Your classes may only have the class variables specified in the table below. Each 3D shape contains a member function computeVolume() to calculate the volume of the shape. You will implement the member function display () for each shape and return the information about the dimension(s), the area and/or volume(for 3D shapes only). Any attempts to set dimensions to be a negative or zero value should display the warning message. Almost all shapes are Expandable, meaning all dimensions can be increased by a (multiplicative) factor such as 2 (which would make the shape twice as large in all directions) by invoking function expand().

After writing the definitions of classes described in the table, you need to write your main function to create objects of different shapes:

1. Write a function maxSurfaceArea () that passes in a vector of Shape pointers which point at Shape objects and finds the Shape object which has the maximum surface area among them and displays the data of this Shape object.

2. Write a function expandAll() that passes in a vector of Shape pointes which point at Shape objects, and a given (multiplicative) factor so that all shape objects are expanded by this given factor in each dimension. Note the factor should be a positive integer. If it is not, display the warning message, and then do nothing.

3. Write your main function that creates a vector of Shape pointers, each points at one Shape object created from the data you read from an input file (each line of the input file contains a shape name and its corresponding size for each dimension). Thefollowing shows an example of shapes input file:

Rectangle 2.44    3.6

Sphere 2.44

Cuboid 1.22    0.6    1.4

Cylinder 2.45    2.44

Rectangle 1.22    1.8

Circle 2.45

Cuboid 2.44   1.2   2.8

Circle 1.22

Sphere 1.22

Cylinder 1.22 2.3

Ask the user to type the name of the input file, which contains the shape name and its corresponding size for each dimension separated by blank space, at each line. You can assume “good data” from the input file, which means a valid shape name followed by its corresponding size information. Provide at maximum of three times for user to try to type a valid input file name, if all fail, simply quit the program. If the input file has been open successfully, print the information about each shape object, and calculate its surface area, volume, then find and display the maximum surface area among all objects. After calling expandAll() with a user input factor, display the information about each shape object after being expanded, then find and display the maximum surface area among all objects after being expanded.

A sample output of your program can be as follows:

Please type the file name for the shape information...

what?

Open file with name "what": failure!

Please type the file name for the shape information...

test.txt?

Open file with name "test.txt": failure!

Please type the file name for the shape information...

testshape.txt?

Open file with name "testshape.txt": failure!

You have tried the maximum times, Bye...

Press ENTER to continue.

A sample output of running your program again can be as follows:

Please type the file name for the shape information...

testShapes.txt?

Rectangle: (length = 2.44, width = 3.6 )

The area is: 8.784

Sphere: (radius = 2.44 )

The area is: 74.8153

The volume is: 34.228

Cuboid: (length = 1.22, width = 0.6, height = 1.4 )

The area is: 6.56

The volume is: 1.0248

Cylinder: (radius = 2.45 , height = 2.44 )

The area is: 75.2759

The volume is: 46.0122

Rectangle: (length = 1.22, width = 1.8 )

The area is: 2.196

Circle: (radius = 2.45 )

The area is: 18.8575

Cuboid: (length = 2.44, width = 1.2, height = 2.8 )

The area is: 26.24

The volume is: 8.1984

Circle: (radius = 1.22 )

The area is: 4.67596

Sphere: (radius = 1.22 )

The area is: 18.7038

The volume is: 4.2785

Cylinder: (radius = 1.22 , height = 2.3 )

The area is: 26.9826

The volume is: 10.7547

The shape with the largest surface area in the vector is:

Cylinder: (radius = 2.45 , height = 2.44)

The area is: 75.2759

The volume is: 46.0122

Please input a positive integer as the factor to expand:

10?

Rectangle: (length = 24.4, width = 36 )

The area is: 878.4

Sphere: (radius = 24.4 )

The area is: 7481.53

The volume is: 34228

Cuboid: (length = 12.2, width = 6, height = 14 )

The area is: 656

The volume is: 1024.8

Cylinder: (radius = 24.5 , height = 24.4 )

The area is: 7527.59

The volume is: 46012.2

Rectangle: (length = 12.2, width = 18 )

The area is: 219.6

Circle: (radius = 24.5 )

The area is: 1885.75

Cuboid: (length = 24.4, width = 12, height = 28 )

The area is: 2624

The volume is: 8198.4

Circle: (radius = 12.2 )

The area is: 467.596

Sphere: (radius = 12.2 )

The area is: 1870.38

The volume is: 4278.5

Cylinder: (radius = 12.2 , height = 23 )

The area is: 2698.26

The volume is: 10754.7

The shape with the largest surface area in the vector is:

Cylinder: (radius = 24.5 , height = 24.4 )

The area is: 7527.59

The volume is: 46012.2

Press ENTER to continue.

Note that the bold part is the user input, and ? represents return key.

Comment

Class Shape Circle Rectangle Cuboid Cylinder Sphere Class Variable None double radius double length, width double height double height None Constructor None Circle( double r) Rectangle( double len, double wid) Cuboid( double len, double wid, double hei) Cylinder( double r, double hei) Sphere( double r Inherits Shape Shape Rectangle Circle Circle

Explanation / Answer

class Mammal

{

protected int age;

protected float weight;

Mammal(float w, int a)

{

    age = a;

    weight = w;

}

public float getWeight()

{

    return weight;

}

public int getAge()

{

    return age;

}

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//in Human.java:

class Human extends Mammal

{

protected float height;

protected String nationality;

Human(float h, int a)

{

    age = a;

    height = h;

}

public float getHeight()

{

    return height;

}

public int getNationality()

{

    return nationality;

}

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

//in Doctor.java:

class Doctor extends Human

{

private School school; // School is a class not shown

private Specialty specialty; // Specialty is a class not shown

Doctor(School sc, Specialty sp)

{

    school = sc;

    specialty = sp;

}

public float getSchool()

{

    return height;

}

public int getSpecialty()

{

    return nationality;

}

public float calcSalary()

{

    return school.getValue() + specialty.getValue + getAge();

}

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

class Soldier extends Human

{

private Branch branch; // Branch is a class not shown

private Rank rank; // Rank is a class not shown

Soldier(Rank r, Branch a)

{

    rank = a;

    branch = b;

}

public float getRank()

{

    return height;

}

public int getBranch()

{

    return branch;

}

public float calcSalary()

{

    return rank.getValue() + branch.getValue + getAge();

}

}

class Mammal

{

protected int age;

protected float weight;

Mammal(float w, int a)

{

    age = a;

    weight = w;

}

public float getWeight()

{

    return weight;

}

public int getAge()

{

    return age;

}

}