Wednesday, June 16, 2004

Exercise[27]

The following coding project is designed to provide practice with many of the important topics covered in class to date. It provides opportunities to practice reading and comprehending code and requirements as well as designing, implementing, using and testing software. If the project seems too easy, then expand the scope of the assignment (while continuing to satisfy all the requirements) by developing software to accommodate other types of shapes, such as kites, triangles, n-sided polygons, etc.

Coding Project


Design and implement code (in Java or JavaScript) that enables you to create instances of objects of type: Your code should meet the following requirements:
  • All objects should have a method called side defined in terms of a parameter called n; the method should return a floating point number representing the length of the side corresponding to the argument value passed to the method.
  • All objects should have a method called angle defined in terms of parameter called n; the method should return a floating point number representing the number of degrees of the angle corresponding to the argument value passed to the method.
  • All objects should have a method called perimeter that returns the length of the perimeter of the shape represented by the object.
  • All objects should have a method called area that returns the area of the shape represented by the object.
  • The class (Java) or constructor function (JavaScript) used to produce Quadrilateral objects should have the following static methods defined in terms of a parameter called q. Each method should return true or false; a method should return true if and only if the value of the parameter q is an object representing a valid instance of the shape connoted by the name of the method. The method names should be:
    • isParallelogram
    • isRhomboid
    • isRhombus
    • isRectangle
    • isSquare
  • Constructors should be defined in terms of parameters as follows.
    • Quadrilateral: s0, s1, s2, a1, a2
    • Parallelogram: s02, s13, a13
    • Rhomboid: s02, s13, a13
    • Rhombus: s0123, a13
    • Rectangle: s02, s13
    • Square: s0123
  • The parameter names listed above should correspond to the side and interior angle labels shown in the following image of a quadrilateral. For example, the value of the parameter s02 (above) is the length of sides s0 and s2 (below), and the parameter a1 (above) is the measure in degrees of angle a1 (below).
You are strongly encouraged to:
  • Use the Polygon JavaScript constructor function or Java class shown here.
  • Consider and verify the following statements:
    • A parallelogram is a quadrilateral.
    • A rhomboid is a parallelogram. (A rhomboid is not a rhombus.)
    • A rhombus is a parallelogram. (A rhombus is not a rhomboid.)
    • A rectangle is a parallelogram. (A rectangle may be a rhombus but cannot be a rhomboid.)
    • A square is a rectangle.

Recommended Reading


Composition vs. Inheritance: How to choose?

AP Computer Science Principles - JavaScript
  • JavaScript: The Definitive Guide, 6th Edition by David Flanagan
    • Chapter 7 Arrays (pp. 141-161)
AP Computer Science A - Java
  • Barron's AP Computer Science A, 7th Edition by Roselyn Teukolsky
    • Chapter 6 Arrays and Array Lists (pp. 233-256)