2007S1ExamQuestions

The squares continually move from side to side along

• Notes
• UltraLightningCobra8457
• 28

This preview shows page 21 - 25 out of 28 pages.

The squares continually move from side to side along this top edge of the window. When a square reaches the right edge of the window, it starts moving towards the left, and when a square reaches the left edge of the window, it starts moving towards the right. The next screenshot shows the window a short time after the first square has reached the right hand edge of the window, and is now moving back towards the left.

Subscribe to view the full document.

VERSION 18832768 - 22 - COMPSCI 101 Question and Answer Booklet ID .......................................... CONTINUED Each square is represented by an object of type Square . The constructor of the Square class is passed one int parameter which indicates the speed at which the square should move. The value of this parameter is the number of pixels that the square should move each time its move() method is called. The window itself is 400 pixels wide and 200 pixels high. The complete JPanel class is given below: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyJPanel extends JPanel implements ActionListener { private Square s1, s2; private Timer t; public MyJPanel() { int speed1 = 2; int speed2 = 4; s1 = new Square(speed1); s2 = new Square(speed2); t = new Timer(25, this); t.start(); } public void actionPerformed(ActionEvent e) { s1.move(); s2.move(); repaint(); } public void paintComponent(Graphics g){ super.paintComponent(g); s1.draw(g); s2.draw(g); } }
VERSION 18832768 - 23 - COMPSCI 101 Question and Answer Booklet ID .......................................... CONTINUED Complete the definition of the Square class below: import java.awt.*; public class Square { private Rectangle box; private int speed; public Square(int speed) { } public void move() { } public void draw(Graphics g) { } } (10 marks)

Subscribe to view the full document.

VERSION 18832768 - 24 - COMPSCI 101 Question and Answer Booklet ID .......................................... CONTINUED 21. The screenshot below illustrates a program that displays a square which is exactly centred in the middle of the window. The window is 400 pixels wide and 400 pixels high, and so the centre of the square is at position (200, 200). The size of the square is calculated randomly each time the program is run, and is between 10 and 400 inclusive. In other words, the largest possible square will be the same size as the window, and the smallest possible square will be 10 pixels wide and 10 pixels high. However, the square will always be centred at position (200, 200). The position of the square is stored in a Rectangle object, called square , which is an instance variable of the program. One other instance variable, called isVisible , is declared which is used to determine whether or not the square should be displayed. The only interaction the user has with this program is that they can use the mouse to make the square disappear . If the mouse button is pressed when the mouse is anywhere inside the square, then the square should disappear. Nothing should happen if the mouse is pressed outside the square, or if the square has already disappeared.
You've reached the end of this preview.

{[ 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