The screenshots below show the results of running the

This preview shows page 16 - 21 out of 24 pages.

The screenshots below show the results of running the program four different times: The squares which are drawn on the screen are represented by a class called Blocks . The dot is represented by a class called Location . For this question, you need to define a method called countInside() in the Location class.
Image of page 16

Subscribe to view the full document.

Question/Answer Sheet - Page 17 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED The source code for the JPanel class is given below: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MyJPanel extends JPanel { private Location position; private Blocks bs; public MyJPanel() { setBackground(Color.white); int x = (int)(Math.random()*300); int y = (int)(Math.random()*300); position = new Location(x, y); bs = new Blocks(5); } public void paintComponent(Graphics g) { super.paintComponent(g); position.draw(g); bs.draw(g); int count = position.countInside(bs); g.drawString("The point is inside " + count + " of the rectangles", 10, 20); } } The source code for the Blocks class is given below: import java.awt.*; public class Blocks { private Rectangle[] rects; public Rectangle[] getRects() { return rects; } public Blocks(int howMany) { rects = new Rectangle[howMany]; for (int i = 0; i < rects.length; i++) { int x = (int)(Math.random()*200); int y = (int)(Math.random()*200); rects[i] = new Rectangle(x, y, 30, 30); } } public void draw(Graphics g) { for (int i = 0; i < rects.length; i++) g.drawRect(rects[i].x, rects[i].y, rects[i].width, rects[i].height); } }
Image of page 17
Question/Answer Sheet - Page 18 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED The (incomplete) source code for the Location class is given below. You need to write the code for the countInside() method in the blank space provided, so that the program behaves as previously described. import java.awt.*; public class Location { private Point pos; public Location(int x, int y) { pos = new Point(x, y); } public void draw(Graphics g) { g.fillOval(pos.x-3, pos.y-3, 6, 6); } public int countInside(Blocks b) { int count = 0; Rectangle[] bs = b.getRects(); for (int i = 0; i < bs.length; i++) if (bs[i].contains(pos)) count++; return count; } (10 marks) }
Image of page 18

Subscribe to view the full document.

Question/Answer Sheet - Page 19 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 11 (6 marks) What is the output of the following program: public class Q11 { private static int[] question(int[][] vals) { int[] result = new int[vals[0].length]; for (int i = 0; i < result.length; i++) { for (int j = 0; j < vals.length; j++) result[i] += vals[j][i]; } return result; } public static void main(String[] args){ int[][] numbers = {{1,2,3}, {4,5,6}, {-2,4,1}, {0,8,5}}; int[] answer = question(numbers); for (int i = 0; i < answer.length; i++) System.out.print(answer[i] + " "); } } 3 19 15 (6 marks)
Image of page 19
Question/Answer Sheet - Page 20 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 12 (6 marks) Consider the following array of Strings, declared and initialised as below: String[] words = new String[5]; words[3] = new String("word"); a) Write a single statement which uses the array words but would generate a NullPointerException . The syntax of the statement must be correct, ie. the code you write must compile.
Image of page 20

Subscribe to view the full document.

Image of page 21
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