Drawrectboundx boundy boundwidth boundheight then the

This preview shows page 15 - 19 out of 28 pages.

g.drawRect(bound.x, bound.y, bound.width, bound.height); } then the window would appear as shown below: Define the boundingRect() method in the space provided below. Remember, this method should return a new Rectangle object which represents the smallest possible rectangle that completely contains all Rectangle objects in the array passed to the method as a parameter. Notice that the rectangle bound has been created by the boundingRect() method so that it just encloses all the other rectangles.
Image of page 15

Subscribe to view the full document.

Question/Answer Sheet - Page 16 - CompSci 101 SS C SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED You must define the boundingRect() method below so that it works for any array of Rectangle objects. You can assume that the array contains at least one Rectangle object. private Rectangle boundingRect(Rectangle[] rects) { int minX = rects[0].x; int minY = rects[0].y; int maxX = rects[0].x + rects[0].width; int maxY = rects[0].y + rects[0].height; for (int i = 1; i < rects.length; i++) { if (minX > rects[i].x) minX = rects[i].x; if (minY > rects[i].y) minY = rects[i].y; if (maxX < rects[i].x + rects[i].width) maxX = rects[i].x + rects[i].width; if (maxY < rects[i].y + rects[i].height) maxY = rects[i].y + rects[i].height; } Rectangle result = new Rectangle(minX, minY, maxX - minX, maxY - minY); return result; (10 marks) }
Image of page 16
Question/Answer Sheet - Page 17 - CompSci 101 SS C SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 7 (10 marks) Examine the source code for the following application carefully: public class Q7 { public static void main(String[] args) { NumberList a = new NumberList(); a.add(2); a.add(4); NumberList b = new NumberList(); b.add(2); b.add(4); b.add(6); NumberList c = new NumberList(); c.add(4); c.add(2); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(a.equals(b)); System.out.println(a.equals(c)); a.add(6); System.out.println(a); System.out.println(b); System.out.println(a.equals(b)); } } The application Q7 above uses a NumberList class to create several NumberList objects. A NumberList object represents a list of integer numbers to which the user can add additional numbers by calling the add() method. The NumberList class must store the numbers using an array of int s. Given a correct implementation of the NumberList class, the output from the application above should be exactly as shown below: > java Q7 List contains 2 values: 2 4 List contains 3 values: 2 4 6 List contains 2 values: 4 2 false false List contains 3 values: 2 4 6 List contains 3 values: 2 4 6 true When a NumberList object is constructed, the list it represents initially contains no numbers. One of the instance variables in the NumberList class is an array of int s of size 1000 and is created to store the numbers that will be added to the list. You can assume that the size of this array is always large enough so you do not need to check for a NumberList object running out of storage space.
Image of page 17

Subscribe to view the full document.

Question/Answer Sheet - Page 18 - CompSci 101 SS C SURNAME: ...................................................... FORENAMES: ...........................................................
Image of page 18
Image of page 19
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern