201 Assignment 3 Solution

1. (10 points) Chapter 3: Self-check problems #4, #10, #14, #20, and #23.
If you use 2nd edition: Self-check problems #1, #7, #11, #14 and #17.
For your benefit, I suggest you complete all of the self-check problems on your own.
However, I will just collect the answers to the above five problems. Please type the answers into a document.
a. NO NEED to copy original questions, just put down the question number and the answer.
b. Name the file as “LastnameFirstname3.doc” (or “.docx” or “.pdf”, where “Lastname” is your last name, and “Firstname” is your first name), submit it online.
2. (10 points) “Greetings”
a. Download the attached “Greetings.java” file.
b. Implement the “getGreetings” method, so that the code prompts the user to enter the first name, the last name, and year of birth, then it returns a greetings message in proper format (see the example below). The “main” method will then print it out. Here is an example dialogue with the user:
Please enter your first name: tom
Please enter your last name: cruise
Please enter your year of birth: 1962
Greetings, T. Cruise! You are about 54 years old.
Note that the greetings message need to be in the exact format as shown above (for example, use the initial of the first name and the first letter of the last name with capitalization).
c. Submit the final “Greetings.java” file (DO NOT change the file name) online.
3. (30 points) Ehrenstein Illusion
a. Download the attached “Illusion.java” file, and the accompanying “DrawingPanel.java” file.
b. In your IDE, put “Illusion.java” and “DrawingPanel.java” under the SAME folder (package).
c. Implement the “drawFigures” method inside the “Illusion.java” so that the code draws a specific figure of grids of concentric circles. Your program should exactly reproduce the figure shown below.
CS 210 W. Li Assignment 3
d. The overall drawing panel is of size 500 x 400. Its background is cyan.
i. There is a basic “subfigure” that occurs throughout, containing concentric circles and a square. The subfigure is repeated to form larger grids.
ii. The square area behind the grids is light gray, and the background of the circles is yellow. The squares are outlined in red, and the circles are outlined in black. A diamond outlined in black covers each set of concentric circles.
iii. The six structures on the panel should have the following properties:
(x, y) position
size of subfigure
circles per subfigure
rows / cols
(0, 0)
90 x 90
(120, 10)
90 x 90
(250, 50)
80 x80
(350, 20)
40 x 40
3 x 3
(10, 120)
100 x 100
2 x 2
(240, 160)
50 x 50
4 x 4
CS 210 W. Li Assignment 3
e. Development strategy: write your code in stages, continually making small improvements. Start by drawing only the top-left subfigure, and then generalize it. You should develop two methods:
i. A method to draw one concentric subfigure (a set of yellow and black concentric circles and a black diamond). Different subfigures will have different sizes, positions, and so on. Therefore, your method should accept several parameters so that you can call it many times, each time to draw a different subfigure on the screen.
ii. A method to draw a grid. Once you have completed the method that produces one subfigure, write another method that produces a square grid of subfigures (assume each grid has an equal number of rows and columns). You will call this method three times to produce the three grids on the bottom. Pay special attention to the drawing order (remember the last draw “wins”).
iii. Your two methods should work together to remove redundancy.
f. For this assignment you are limited to the language features in Chapters 1-3 of the textbook.
g. Remember, your program will be graded both on “external correctness” (whether your program compiles and produces exactly the expected outputs), and “internal design and style” (whether your source code follows the style guide).
h. Submit the final “Illusion.java” file (DO NOT change the file name) online. DO NOT turn in “DrawingPanel.java”.
Powered by