The Program Extention
A manufactured item can have one of the solid shapes cone,
cylinder or rectangle and can be made from one of a number of
materials, such as plastic or metal. Write a program to input
the density of the material and the shape and dimensions of the
item and to display the weight of the item.(Hint: Different
shapes will require different dimensions to be input and
different calculations to be performed to calculate the volume.
Firstly, ask the user to specify the shape, and then use a
switch statement to allow the program to perform input and
calculations specific to that shape.)
Shall I use a structure to store the item information? For example, store the length, width and height of a rectangle, even the equation to calculating the weight. That will have a good extention.
However, the problem is that the elements of the structure is changable. For example, a cyclinder has only two element: radius and height to calculate its cubage while a rectangle needs three: length, width, height. How can I solve this problem?
If the problem is solved, there is no need to modify the code when a new item is added. Otherwise, we have no choice but to rewrite the code.
Which way should I follow?
TimWang - China