5 marks candidates name 14 compsci 101 scst 10 you

This preview shows page 13 - 18 out of 22 pages.

(5 marks)
Image of page 13

Subscribe to view the full document.

Candidate's Name:___________________ 14 COMPSCI 101 SC/ST 10. You need to complete the actionPerformed() and paint() methods for the applet described below. When the applet first starts, a TextField and 3 Button s appear along the top of the window and a collection of 10 tanks (all initially facing up) are drawn in a line, as shown below: The 10 Tank objects are created and stored in an array. The source code for the Tank class is provided - you will need to examine this class carefully. Notice that tanks can face either up or down. By typing a number in the TextField and pressing one of the Button s, the user can change the direction in which a tank is facing or can destroy a tank so that it no longer appears on the screen. The number in the TextField will indicate the index of the Tank in the array of tanks. The leftmost tank on the screen is at index 0 and the rightmost tank is at index 9. For example, in the screen shot below, the user has typed the number 3 into the TextField and then clicked the "DOWN" button. As you can see the tank at index 3 is now facing down:
Image of page 14
Candidate's Name:___________________ 15 COMPSCI 101 SC/ST The screen shot below shows the applet after the user presses the " UP " button when the value "3" is still in the TextField . The tank faces up again: The screen shot below shows the applet after the user has changed the number in the TextField to "8" and then clicked the button labelled "DOWN": When the button labelled "DESTROY" is clicked, the corresponding tank should disappear from the screen. The screenshot below shows the applet after the user types "8" into the TextField and then clicks the "DESTROY" button:
Image of page 15

Subscribe to view the full document.

Candidate's Name:___________________ 16 COMPSCI 101 SC/ST You must make sure that the code you write will not generate any NullPointerExceptions. The only assumption you can make is that the user will only ever enter values between 0 and 9 into the TextField . The source code for the Tank class is given below: import java.awt.*; public class Tank { public static final int UP = 0; public static final int DOWN = 1; public static final int SIZE = 20; private int xPos, yPos; private int direction; public Tank(int x, int y) { xPos = x; yPos = y; direction = UP; } public void draw(Graphics g) { g.drawRect(xPos-SIZE/2, yPos-SIZE/2, SIZE, SIZE); if (direction == UP) g.drawRect(xPos - SIZE/6, yPos - 5*SIZE/6, SIZE/3, SIZE/3); else g.drawRect(xPos - SIZE/6, yPos + SIZE/2, SIZE/3, SIZE/3); } public void faceUp() { direction = UP; } public void faceDown() { direction = DOWN; } } You must complete the actionPerformed() and paint() methods in the source code for the applet below: import java.awt.*; import java.applet.*; import java.awt.event.*; public class Q10 extends Applet implements ActionListener { public static final int NUM_TANKS = 10; private Tank[] tanks; private TextField id; private Button bUp, bDown, bDestroy;
Image of page 16
Candidate's Name:___________________ 17 COMPSCI 101 SC/ST public void init() { tanks = new Tank[NUM_TANKS]; for (int i = 0; i < NUM_TANKS; i++) tanks[i] = new Tank((i+1)*40, 80); id = new TextField(5); add(id); bUp = new Button("UP"); bUp.addActionListener(this); add(bUp); bDown = new Button("DOWN"); bDown.addActionListener(this); add(bDown); bDestroy = new Button("DESTROY"); bDestroy.addActionListener(this);
Image of page 17

Subscribe to view the full document.

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