CS 46A Fall 2022 Homework 08 Requirements 1: Name Your
Cs 46a Fall 2022 Homework 08requirements1 You Must Name Your Classes
Write a Java application RandomRectangles which prompts for a positive integer then draws that number of random rectangles. If the input is not a positive integer, the program will display a message and prompt for input again until a positive integer is entered. The program also finds the rectangle with the smallest area and displays the number of rectangles and the smallest area. Draw the squares in red. After drawing all rectangles, fill the rectangle with the smallest area with yellow. Use the predefined colors from the Color class. All rectangles have random widths and heights at random locations, with coordinates and sizes generated within specified bounds using a fixed seed. Use only one for loop to draw the rectangles and find the smallest; do not store them in an array or list. Use one Scanner object for input processing. Do not use try-catch blocks or parse methods.
Paper For Above instruction
The programming task involves creating a Java application named RandomRectangles that generates a series of random rectangles based on user input. The key focus is on input validation, graphical representation, and efficient processing within a single loop. The design adheres to specific constraints regarding class naming, randomness, and graphics, which are fundamental to ensuring compatibility with the grading and testing environment.
Firstly, the program must prompt the user to input a positive integer representing the number of rectangles to be drawn. The program uses a Scanner object solely and mandates no exception handling through try-catch blocks. Instead, it relies on calling Scanner methods directly and verifying input correctness. If the input is invalid (non-integer or not positive), it displays an appropriate message and reprompts until valid input is received. This loop ensures robust input validation, preventing the program from proceeding until valid data is entered.
Once a valid number is obtained, the program proceeds to generate and draw that many rectangles on a graphical window. The drawing involves random position and size, with the position based on random x and y coordinates within the window bounds and sizes within specified minimums and maximums. All randomness must be generated consistently using the seed 202210. This ensures reproducibility across different executions with the same input.
The rectangle specifications are generated in a fixed sequence: x coordinate, y coordinate, width, and height, for each rectangle within a single for loop. The problem explicitly forbids using arrays or ArrayLists to store rectangle objects, promoting an efficient, procedural approach. During the loop, the program calculates each rectangle’s area, tracks the smallest area encountered, and keeps note of the last rectangle with that smallest area in case of ties. The last rectangle with the minimal area supersedes earlier ones.
Graphically, the program must render each rectangle with random colors from the Color class. Squares should be filled in red, but other rectangles should perhaps be drawn with a different color or default. After drawing all rectangles, the program fills the rectangle with the smallest area with yellow, highlighting it distinctly. This final step visually emphasizes the rectangle with the minimal area, making it easily identifiable.
The graphical window should be sufficiently large, with constants MAX_X set to 100, MAX_Y to 500, and sizes constrained by minimum and maximum constants: widths between 30 and 99, heights between 20 and 79. These constants should be declared as public static final fields for clarity and adherence to instructions.
The implementation requires importing the graphics package for drawing, utilizing a consistent random number generator with the given seed, and performing all calculations within the scope of a single loop. At the end of execution, the program outputs the total number of rectangles and the smallest area found, and displays the graphical window with all rectangles, with the smallest filled in yellow.
References
- Gaddis, T. (2018). Starting Out with Java: From Control Structures through Data Structures. Pearson.
- Deitel, P., & Deitel, H. (2017). Java: How to Program (10th Edition). Pearson.
- Oracle. (n.d.). The Color Class. Oracle Java documentation. https://docs.oracle.com/javase/8/docs/api/java/awt/Color.html
- Deitel, P., & Deitel, H. (2017). Graphics Programming in Java. Java Programming.
- Dobbs, R. (2002). The Java Graphics Tutorial. Dr. Dobb’s Journal.
- Tanenbaum, A. S., & Bos, H. (2014). Modern Operating Systems (4th Edition). Pearson. (for understanding GUI and event handling concepts)
- Green, M. (2006). The Art of Java Swing. O'Reilly Media.
- Pratt, C., & McClure, S. (2017). Java Program Design. McGraw-Hill Education.
- Perry, H., & Miller, J. (2008). Java for Programmers. Addison-Wesley.
- Java Platform, Standard Edition Documentation. (2020). Creating Graphics with Java. https://docs.oracle.com/javase/tutorial/2d/overview.html