Project2 Sec33755

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Project 2: Inheritance

Due: September 10, 2013 CSCI1302, Fall 2013

In this project, you will 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 methods to access and modify the data. In addition, provide appropriate methods to compute each shapes circumference, area and volume when it applies. In your design, consider how shapes are related and thus where inheritance can be implemented. Please compile and run your program on our Linux server nike.

Project description:
There are various ways to use inheritance to relate these shapes but please follow the inheritance described in the table below. All shapes inherit from the abstract superclass Shape.
public abstract class Shape { abstract public double computeArea(); abstract public double computePerimeter(); //other methods you want to add; }

Your program will consist of the following classes: Shape, Circle, Triangle, Rectangle, Prism, Sphere, Cylinder and Tetrahedron. (Assume triangle is an equilateral triangle, and tetrahedron is a Triangle Pyramid, which has an equilateral triangle as the base). Three-D shapes implements the ThreeDShape interface.
public interface ThreeDShape { public double computeVolume(); }

Your classes may only have the class variable specified in the table below. You will implement the method toString( ) for each shape and return the information about the dimension(s), the area, the perimeter 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 method expand().
Class Shape Circle Triangle Rectangle Prism Cylinder Sphere Tetrahedron Class Variable None double radius double side double width, length double height double height None double height Constructor None Circle( double r ) Triangle( double side ) Rectangle( double wid, double len ) Prism( double wid, double len, double hei ) Cylinder( double r, double hei ) Sphere( double r ) Tetrahedron( double s, double hei ) Extends Shape Shape Shape Rectangle Circle Circle Triangle

Page 1 of 3

Test Class
Write a test class, ShapeTester, which will have several static methods. 1. Write a main method that creates an array and populates it with several different 3-D shapes (data for each shape are shown below). Then have main invoke the additional methods below. Sphere 7.4 Prism 5 4.5 4 Tetrahedron 4 5 Cylinder 3.14 4.5 Prism 1 2 3 Tetrahedron 2.34 3.56 Sphere 2.5 Cylinder 6.7 3.3 2. Write a method maxSurfaceArea( ) that takes an array and finds the shape which has the maximum surface area in the array and display the data of this shape. 3. Write a method expandAll( ) that takes an array with several shapes and expands all of them into twice as large in all directions. A sample output of your program can be as follows: Sphere: radius is 7.4 circumference is 46.5, area is 688.13 volume is 1697.4 Prism: width is 5, length is 4.5, height is 4 perimeter of base is 19, area is 121 volume is 90 Tetrahedron: side length is 4, height is 5 perimeter of base is 12, area is 37.72 volume is 11.55 Cylinder: radius is 3.14, height is 4.5 perimeter of base is 19.73, area is 150.73 volume is 139.39 Prism: width is 1, length is 2, height is 3 perimeter of base is 6, area is 22 volume is 6 Tetrahedron: side length is 2.34, height is 3.56 perimeter of base is 7.02, area is 15.09 volume is 2.81 Sphere: radius is 2.5 circumference is 15.71, area is 78.54 volume is 65.45 Cylinder: radius is 6.7, height is 3.3 perimeter of base is 42.1, area is 420.97 Page 2 of 3

volume is 465.39 The shape with the largest surface area in the array is: Sphere: radius is 7.4 circumference is 46.5, area is 688.13 volume is 1697.4 The size of each shape has been doubled: Sphere: radius is 14.8 circumference is 92.99, area is 2752.54 volume is 13579.19 Prism: width is 10, length is 9, height is 8 perimeter of base is 38, area is 484 volume is 720 Tetrahedron: side length is 8, height is 10 perimeter of base is 24, area is 150.87 volume is 92.38 Cylinder: radius is 6.28, height is 9 perimeter of base is 39.46, area is 602.92 volume is 1115.09 Prism: width is 2, length is 4, height is 6 perimeter of base is 12, area is 88 volume is 48 Tetrahedron: side length is 4.68, height is 7.12 perimeter of base is 14.04, area is 60.36 volume is 22.51 Sphere: radius is 5 circumference is 31.42, area is 314.16 volume is 523.6 Cylinder: radius is 13.4, height is 6.6 perimeter of base is 84.19, area is 1683.89 volume is 3723.09

Submit your Project2 directory to cs1302a on nike. You must use the submit command to submit your work, as shown below: $ submit Project2 cs1302a You should execute the submit command while being in the parent directory of Project2, i.e., CSCI1302

Up to 10% of your score will be the following things:


Follow good coding style (proper variable names, indentation, etc). Include comments in your program.

Page 3 of 3

You might also like