Objectives

The goal of this assignment is to learn the following concepts:

Overview

In this assignment you will create a program that computes area, volume, and other geometric attributes for a variety of 2D and 3D shapes. To do this, you will create a class hierarchy through inheritance. Your base class, Shape, will have derived classes TwoDShape and ThreeDShape. TwoDShape will have derived classes Circle, Square, Triangle, and 3DShape will have derived classes Sphere, Cube, and Pyramid. Because the 2D shapes and 3D shapes require many of the same things to complete the area and volume formulas, you can use inheritance to reuse the same variables and actions, thus creating a more robust program. In computer science, a robust program is one that that will perform well under undesirable conditions, such as low memory, or receiving invalid input.

Design Criteria

Class Shape

Class Shape will contain Getter and Setter actions for the following variables:

This class should also have a static variable, number pi = 3.1415.

Class TwoDShape

Class TwoDShape will contain actions that will ask for user input and use that input as the arguments for the Setter actions from base class Employee. Note: you will need to cast the input as type number, otherwise you will receive a compiler error.

Class ThreeDShape

Class ThreeDShape will do the same as class TwoDShape, except that the user should be queried to enter input for cube, pyramid, and sphere, instead of for square, triangle, and circle.

Class Circle

Class Circle is a derived class from class TwoDShape. It will have two actions:

action CalculateArea(number radius) returns number

and

action CalculateCircumference(number radius) returns number

Class Square

Class Square is a derived class from TwoDShape. It will have two actions:

action CalculateArea(number side) returns number

and

action CalculatePerimeter(number side) returns number

Class Triangle

Class Triangle is a derived class from TwoDShape. It will have one action:

action CalculateArea(number base, number height) returns number

Class Cube

Class Cube is derived from ThreeDShape. It will have two actions, one to calculate the volume of a cube, and one to calculate surface area of a cube.

Class Sphere

Class Sphere is derived from ThreeDShape. It will have two actions, one to calculate the volume of a sphere, and one to calculate the surface area of a sphere. Note: the variable pi that you made in class Shape can be accessed using the parent: keyword.

Class Pyramid

Class Pyramid is derived from ThreeDShape. It will have one action that calculates the volume of a pyramid.

Class Main

Class Main will have three actions. One will call all the actions from Circle, Square, and Triangle, and will report the calculations of each to the user. The second action will do the same as above, except will call the Sphere, Cube, and Pyramid actions, and report those calculations to the user. The third is action Main, which will call the previous two actions. When run, the program should prompt the user to input numbers for the variables created in class Shape. After receiving input, the program will do the various calculations, then output (say) the results to the user.

Sample Output

Report the calculations from all six classes. For example, the actions from Circle, Square, and Triangle may look similar to this:

The area of a square with side lengths of 5 is 25
The perimeter of a square with side lengths of 5 is 20
The area of a triangle with a base of 10 and a height of 5 is 25
The area of a circle with radius of 10 is 314
The circumference of a circle with a diameter of 10 is 31

Relevant Formulas

Next Tutorial

In the next tutorial, we will discuss Assignment 6.2, which describes using derived blueprint actions for superhero powers..