/* TrapezoidApp.java ** ** Java application that utilizes methods of the Trapezoids class to produce ** character-based renderings of regular trapezoids oriented with either a ** narrow-top or a narrow-bottom of the given length and of the given height. ** The figures themselves are drawn a specified foreground char, along with a ** specified background char, such that what is drawn is within a box. ** ** CMPS 134 - Fall 2024 ** Author: P. M. J. ** Last Modified: September 25, 2024 ** Collaborated with: R. W. M. ** Known flaws: None known. ** */ import java.util.Scanner; public class TrapezoidsApp { private static final char TOP = 'T'; //Used to indicate Top private static final char BOTTOM = 'B'; //Used to indicate Bottom private static Scanner input; public static void main(String[] args) { input = new Scanner(System.in); boolean keepGoing; // Loop to continue prompting for and processing input until an input // other than TOP or BOTTOM is entered. do { keepGoing = makeAnotherTrapezoid(); } while (keepGoing); System.out.println("Goodbye."); } /* Prompts the user to enter inputs describing a regular trapezoid, ** renders it, and reports its foreground and background areas. ** Returns false if the user's input indicates that they want to quit, ** true otherwise. */ public static boolean makeAnotherTrapezoid() { boolean result = true; char orientation = getChar("Enter 'T' or 'B' for orientation :> "); //Check for non-terminating condition if (orientation != TOP && orientation != BOTTOM) { result = false; // user chooses to quit } else { char foreground, background; int narrowLength, height; foreground = getChar("Enter foreground char :> "); background = getChar("Enter background char :> "); narrowLength = getInt("Enter length of narrow base :> "); height = getInt("Enter height :> "); // Draw the figure described by the inputs. if(orientation == TOP) { Trapezoids.drawNarrowTopTrapezoid(narrowLength, height, foreground, background); } else { // orientation == BOTTOM Trapezoids.drawNarrowBottomTrapezoid(narrowLength, height, foreground, background); } // Compute the foreground and background areas int foreArea = Trapezoids.foregroundAreaOfTrapezoid(narrowLength, height); int backArea = Trapezoids.backgroundAreaOfTrapezoid(narrowLength, height); // Report those areas System.out.println("F:" + foreArea + ", B:" + backArea + "\n"); } return result; } /* Prints the given string (intended to be a user prompt) and returns ** the response (interpreted to be of type int) read by the input Scanner. */ private static int getInt(String prompt) { System.out.print(prompt); int result = input.nextInt(); input.nextLine(); return result; } /* Prints the given string (intended to be a user prompt) and returns ** the response (interpreted to be of type char) entered at the keyboard. */ private static char getChar(String prompt) { System.out.print(prompt); return (input.nextLine() + " ").charAt(0); } }