CMPS 134 Fall 2024
Prog. Assg. #3: Trapezoids
Due: 11:59pm, Thursday, Oct. 3

Background

Recall from Mathematics that a trapezoid is a quadrilateral that has two parallel sides (known as bases) and two nonparallel sides. A regular trapezoid is one in which the two non-parallel sides are of equal length, resulting in a shape that reminds one of a triangle that does not necessarily come to a point. Such trapezoids will be the focus of this assignment.

Consider the following crude renderings of some regular trapezoids "sitting" on one of their two bases, which are referred to as the top and bottom. Depending upon its orientation, we refer to a trapezoid as being either "narrow-top" or "narrow-bottom".

    ## 
   #### 
  ######  
 ########
##########
$$$$$$$$$$$$$
 $$$$$$$$$$$
  $$$$$$$$$
   $$$$$$$
    $$$$$
     $$$
*******
-*****-
--***--
---*---
....##.... 
...####... 
..######..
.########.
##########
AAAAAAAAAAAAAAA
^AAAAAAAAAAAAA^
^^AAAAAAAAAAA^^
^^^AAAAAAAAA^^^
^^^^AAAAAAA^^^^
^^^^^AAAAA^^^^^

Each rendering presents a trapezoid embedded within a "box" using a foreground character and a background character. In the two leftmost examples, the background character is a blank (i.e., space), but in the remaining examples the background character is a non-blank and hence is "visible".

To complete the characterization of a trapezoid, we need —in addition to its orientation and its foreground and background characters— its height and the length of its shorter/narrower base. For example, the leftmost trapezoid among those pictured above has a height of five and its shorter base has length two. The rightmost has a height of six and a shoter base of length five.

Assignment

In this assignment you are to provide implementations for a collection of static methods related to the rendering and measurement of trapezoids, as given in the Java class Trapezoids.

Among these stubbed methods are four that we refer to as "helpers". Each one is intended to compute the length of a run composed of either the foreground or background character on a specified line within the rendering of a trapezoid. (A run is a sequence of characters all of which are the same.)

As an example, consider the trapezoid above in which the foreground and background characters are, respectively, '#' and '.'. The value returned by the narrowTopForegroundLength() method for successive lines of the drawing should be 2, 4, 6, 8, and 10 (as those are the lengths of the runs of #'s appearing in the first, second, ..., and fifth lines, respectively). Meanwhile, the values returned by narrowTopBackgroundLength() for successive lines of that drawing should be 4, 3, 2, 1, and 0 (as those are the lengths of the runs of .'s appearing in the first, second, ..., and fifth lines, respectively).

As a second example, consider the trapezoid with foreground character '$' and background character ' ' (blank/space). The narrowBottomForegroundLength() method should produce values 13, 11, 9, 7, 5, and 3, respectively, in response to queries regarding line numbers 1, 2, ..., and 6. The narrowBottomBackgroundLength() method should produce 0, 1, ..., and 5, respectively.

Although it might not be immediately apparent, the four "helper" methods can be very useful in developing the remaining stubbed methods. The Java application TrapezoidsTool is provided to help you in developing and testing the four helper methods.

Enter 'T' or 'B' for orientation :> T
Enter foreground char :> $
Enter background char :> .
Enter length of narrow base :> 4
Enter height :> 9
........$$$$........
.......$$$$$$.......
......$$$$$$$$......
.....$$$$$$$$$$.....
....$$$$$$$$$$$$....
...$$$$$$$$$$$$$$...
..$$$$$$$$$$$$$$$$..
.$$$$$$$$$$$$$$$$$$.
$$$$$$$$$$$$$$$$$$$$
F:108, B:72

Enter 'T' or 'B' for orientation :> B
Enter foreground char :> #
Enter background char :> !
Enter length of narrow base :> 3
Enter height :> 6
#############
!###########!
!!#########!!
!!!#######!!!
!!!!#####!!!!
!!!!!###!!!!!
F:48, B:30

Enter 'T' or 'B' for orientation :> Glork 
Goodbye.

Also provided is the Java application program TrapezoidsApp. Its purpose is to help you test both the helper methods and the others you are responsible for completing (in the Trapezoids class, which lacks a main() method and hence cannot be run).

A sample dialog between a user and TrapezoidsApp is shown to the right. It prompts the user to enter the five pieces of data needed to describe a regular trapezoid, after which it "draws" that trapezoid and reports the foreground and background areas (i.e., the number of occurrences of the foreground and background characters, respectively, appearing in the drawing). The program terminates when the user enters something other than T or B in response to the first prompt.

Submission of Program

Use the appropriate Brightspace dropbox to submit your completed Java source program, which must be in a file named Trapezoids.java. (Do not submit the corresponding .class file.) Note that you can submit more than one time. Hence, if, after submitting, you improve your program (e.g., by fixing logic errors or by enhancing your comments), you should submit the newer version, but its name should remain Trapezoids.java!

The incomplete version of Trapezoids that was provided to you includes a comment describing its purpose; a comment identifying the course, semester, and assignment number; and comments intended to identify the program's authors (including you!) and those with whom you collaborated to develop the program. Also included is a comment intended for you to describe any known deficiencies of your program. Consider this to be a template that you are expected to follow in all subsequent assignments.