Week 4 Assignment (Lego Mania)General Rules for Homework Assignments You are strongly encouraged to add comments throughout the program. Doingso will help your facilitator to understand your programming logic and grade youmore accurately. You must work on your assignments individually. You are not allowed to copy theanswers from the others. However, you are encouraged to discuss approachesto the homework assignment with your section mates and the facilitator in yoursection via the discussion board. Each assignment has a strict deadline. However, you are still allowed to submityour assignment within 2 days after the deadline with a penalty. 15% of the creditwill be deducted unless you made previous arrangements with your facilitatorand professor. Assignments submitted 2 days after the deadline will not begraded. When the term lastName is referenced in an assignment, please replace it withyour last name.You are strongly encouraged to add comments into your program!Create a new Java Project in Eclipse named HW4_lastName. Create a package namedcs520.hw4 and complete the following by writing the appropriate classes under thispackage.Part 1 (50 points)Create a Swing GUI application that displays the following graphics of red and bluelego blocks. The tower is always built from bottom row to top row, left to right in eachrow.Write a class named Legos1 extending the JFrame class with the followingspecifications.a. Declare the integer type instance variables startX, startY, legoWidth,and legoHeight. The first two values represent the top-left coordinatesof the first red block in the bottom row. The next two values are thewidth and height of each block.b. Declate another integer instance variable, baseLength. This representsthe number of blocks in the bottom row.c. In the constructor, specify the appropriate window title using yourlastName and assign the above instance variables with the values 20,300, 50, 20, and 10, respectively.d. In the paint method, using nested loops, fill the pattern from the bottomrow all the way to the top. Notice, that the number of blocks decreasesby one for each row. Use the baseLength variable to control the loops.e. In the main method, create the application object, set its size to 550 by325 and its visibility to true to test the above graphics.Note: For each block, use the fillRoundRect method of the Graphics object.Use a value of 2 for arcWidth and arcHeight.http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics.htmlPart 2 (50 points)Modify part1 assuming that now you have a bucket of lego blocks with various colors.Write a newclass Legos2 in the same package for this part.Use an array of Color objects and initialize the array with the colors:Color.red, Color.blue, Color.yellow,Color.green, Color.pink,Color.black, Color.magenta,Color.orange, and Color.cyan.For picking the next color for the block, use a java.util.Random object and generate arandom integer using the nextInt method. Use the length of the colors array as anargument (do not hard code the array length).http://docs.oracle.com/javase/6/docs/api/java/util/Random.html#nextInt(int)Since the colors are picked at random, the output varies for each invocation. Asample output is shown below.BONUS (10 points)Modify part2 and write the Legos3 version so that the same color is not pickedconsecutively. A sample output is shown below.SubmissionCreate an archive of your Eclipse project using the following steps. Select theHW4_lastName project in the Eclipse IDEs Package Explorer or the Navigator window.Click File-Export. Select the General-Archive File option. Click Next.Specify the To archive file: entry as say, C:TempHW4_lastName.zip.The zip file will be created and stored in the C:Temp folder.Submit this zip file as an attachment in the Assignment Section.
5/5 – (1 vote)
Reviews
There are no reviews yet.