2001FCExamQuestions

# Question 6 10 marks you need to complete the code for

• Notes
• UltraLightningCobra8457
• 20

This preview shows page 8 - 13 out of 20 pages.

Question 6 (10 marks) You need to complete the code for the following applet, which uses a Vector to store a collection of Rectangle objects. When the applet first starts, a Button appears with the label " Add ", as shown in the screen shot on the left below. As soon as the user presses the Button, a rectangle appears at a random location on the applet, as shown in the screen shot on the right below: Every time the " Add " button is pressed, another rectangle is created and drawn to the applet. The screen shot below shows the applet after the " Add " button has been pressed a further 9 times: Complete the source code on the following page for the applet as described above. A method, randomRect() , has been written for you that you can use to create Rectangle objects with random locations and sizes. The applet window is 200 pixels wide and 200 pixels high. CONTINUED

Subscribe to view the full document.

Question/Answer Sheet - Page 9 - CompSci 101 FC SURNAME: ...................................................... FORENAMES: ........................................................... import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*; public class Q6 extends Applet implements ActionListener { Vector rects; Button bAdd; private Rectangle randomRect() { final int APPLET_SIZE = 200; int randX, randY, randWidth, randHeight; randX = (int)(Math.random() * (APPLET_SIZE - 5)); randY = (int)(Math.random() * (APPLET_SIZE - 5)); randWidth = (int)(Math.random()*(APPLET_SIZE-randX-5)) + 5; randHeight = (int)(Math.random()*(APPLET_SIZE-randY-5))+5; return new Rectangle(randX, randY, randWidth, randHeight); } public void init() { } public void actionPerformed(ActionEvent e) { } public void paint(Graphics g) { } (10 marks) } CONTINUED
Question/Answer Sheet - Page 10 - CompSci 101 FC SURNAME: ...................................................... FORENAMES: ........................................................... Question 7 (5 marks) What is the output of these Java code segments? They compile and run without error. a) int value = 0; for (int i = 1; i <= 8; i++) for (int j = 0; j <= 10; j += 2) value++; System.out.println(value); (2 marks) b) double value = 0.0; for (int j = 0; j < 10; j++) for (int i = j; i < 10; i++) value += 1.0; System.out.println(value); (3 marks) CONTINUED

Subscribe to view the full document.

Question/Answer Sheet - Page 11 - CompSci 101 FC SURNAME: ...................................................... FORENAMES: ........................................................... Question 8 (5 marks) What is the output of this applet? import java.applet.*; import java.util.*; public class Q8 extends Applet { Vector v; public void init() { String a, b, c; v = new Vector(); a = new String("hello"); b = ""; c = a; v.addElement(a); v.addElement(new String("X")); v.addElement(b); v.addElement(new String("Y")); v.addElement(c); a = (String)v.elementAt(1); b = (String)v.elementAt(3); System.out.println(b.length()); v.removeElement(a); System.out.println(b); b = (String)v.elementAt(3); System.out.println(b.length()); System.out.println(a); System.out.println(c); } } (5 marks) CONTINUED
Question/Answer Sheet - Page 12 - CompSci 101 FC SURNAME: ...................................................... FORENAMES: ........................................................... Question 9 (10 marks) The following applet uses a Thing class. It creates two Thing objects and draws them. Whenever the user clicks the button labelled " Swap ", the bottom shape moves to the position of the top shape, and vice versa, as shown in the screen shots below. The screen shot on the left shows what the applet looks like when it initially starts, and the screen shot on the right shows what happens when " Swap " is clicked. You need to complete the code for the Thing class.

Subscribe to view the full document.

You've reached the end of this preview.
• Summer '12
• Computer Science, ........., University Of Auckland

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern