The scoret textfield displays the result ie the

This preview shows page 21 - 26 out of 34 pages.

The scoreT textfield displays the result, i.e., the number of correct guesses together with the total guesses the user has made so far. For example, if the user has pressed the CHECK button a total of 6 times and has guessed the correct number twice, the scoreT textfield displays the String, "2 / 6" . You are required to complete the following JPanel definition so that the JPanel behaves as described above. You MUST use the variables given in the code.
Image of page 21

Subscribe to view the full document.

Question/Answer Sheet - 22 - CompSci 101 ID: ........................................................... CONTINUED The screenshots below show examples of the JPanel after the user presses the NEXT button, enters a guess in the guessT textfield and presses the CHECK button. The user now continues with the process. import java.awt.*; import javax.swing.*; import java.awt.event.* public class AJPanel extends JPanel { private JTextField promptT, guessT, scoreT; private JButton nextB, checkB; private int computerNumber, userScore, totalGuesses; public AJPanel() { promptT = new JTextField(1); guessT = new JTextField(1); scoreT = new JTextField(" 0 / 0 "); nextB = new JButton("NEXT"); checkB = new JButton("CHECK"); add(new JLabel("Guess the number (between 0 and 2)")); add(promptT); add(new JLabel("User guess")); add(guessT); implements ActionListener The user presses the NEXT button, enters 1 in the guessT textfield and presses the CHECK button. The user presses the NEXT button, enters 2 in the guessT textfield and presses the CHECK button. The user presses the NEXT button, enters 2 in the guessT textfield and presses the CHECK button.
Image of page 22
Question/Answer Sheet - 23 - CompSci 101 ID: ........................................................... CONTINUED add(nextB); add(checkB); add(scoreT); userScore = 0; totalGuesses = 0; nextB.addActionListener(this); checkB.addActionListener(this); } public void actionPerformed(ActionEvent e) { int userNumber; if(e.getSource() == nextB) { computerNumber = (int)(Math.random() * 3); promptT.setText("?"); guessT.setText(""); } else { totalGuesses++; userNumber = Integer.parseInt(guessT.getText()); if (computerNumber == userNumber) { userScore++; } promptT.setText(""); scoreT.setText("" + userScore + " / " + totalGuesses); }
Image of page 23

Subscribe to view the full document.

Question/Answer Sheet - 24 - CompSci 101 ID: ........................................................... CONTINUED } (10 marks)
Image of page 24
Question/Answer Sheet - 25 - CompSci 101 ID: ........................................................... CONTINUED Question 11 (10 marks) Complete the following JPanel which uses a Timer object. The Timer object is created with a delay of 50 milliseconds and the Timer starts as soon as the JPanel is created. The JPanel displays a black square of size 10 pixels which initially is set at the position given by the Rectangle constant, INITIAL_AREA . With every tick of the timer the black square moves 10 pixels. The square first moves five times (given by the constant, MAX_MOVES_IN_DIRECTION ) to the right, then it moves five times upwards, then five times to the right, then five times upwards and so on. The path taken by the black square is shown on the right. The user is able to stop/start the black square moving by pressing the UP arrow key. If the timer is running, pressing the UP arrow key stops the timer. If the timer is not running, pressing the UP arrow key starts the timer again. The user is able to reset the black square back to its initial position (given by the Rectangle constant, INITIAL_AREA ) by pressing the DOWN arrow key. The screenshot at the top of the page shows the JPanel when it is first displayed. Note that the black square starts moving straight away to the right.
Image of page 25

Subscribe to view the full document.

Image of page 26
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Method, ........., JPanel, Mutator method

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