Candidates name 10 compsci 101 test continued 8 study

This preview shows page 10 - 14 out of 14 pages.

Candidate's Name:___________________ 10 COMPSCI 101 TEST CONTINUED 8. Study the following class definition: public class Word{ private static int number = 0; private String theWord; private int value; public Word(String word, int val){ theWord = word; value = val; number = number + val; } public void setValue(int val){ number = number - value; value = val; number = number + value; } public void display(){ System.out.println(value + “ - ” + theWord + “ - ” + number); } public static void setNumber(int num){ number = num; } } What is the output when the following application is executed? import java.util.*; public class Q8{ public static void main(String[] args){ Word word1, word2, word3; Word.setNumber(100); word1 = new Word("Press", 2); word1.display(); word2 = new Word("Intention", 4); word3 = new Word("Enigmatic",7); word2.setValue(5); word2.display(); word3.setValue(6); word3.display(); } } 2 - Press - 102 5 - Intention - 114 6 - Enigmatic - 113 (7 marks)
Image of page 10

Subscribe to view the full document.

Candidate's Name:___________________ 11 COMPSCI 101 TEST CONTINUED SECTION C 9. Complete the actionPerformed () method of the following applet. When a value is entered in the top TextField , the bottom TextField should display the value which is one larger than the value entered. Screenshots included here show an example of the applet with a value entered into the first TextField , and the result produced in the second TextField when the enter key is pressed. import java.awt.*; import java.applet.*; import java.awt.event.*; public class Q9 extends Applet implements ActionListener { private TextField tInput; private TextField tOutput; public void init() { tInput = new TextField(20); tOutput = new TextField(20); tInput.addActionListener(this); add(tInput); add(tOutput); } public void actionPerformed(ActionEvent e) { int value = Integer.parseInt(tInput.getText()); tOutput.setText("" + (value + 1)); } } (5 marks)
Image of page 11
Candidate's Name:___________________ 12 COMPSCI 101 TEST CONTINUED Q10 Complete the sum() method below. It is passed an array of ints as a parameter and returns the sum of all the values in the array. import java.awt.*; import java.applet.*; import java.awt.event.*; public class Q10 extends Applet { public int sum(int[] values) { int result = 0; for (int i = 0; i < values.length; i++) result += values[i]; return result; } public void init() { int[] numbers = {4, 6, 2, 8, 3, 1}; int result = sum(numbers); System.out.println("The sum is: " + result); } } If the sum method is written correctly, then the applet above will produce 24 as output (given the numbers chosen in the array). (5 marks)
Image of page 12

Subscribe to view the full document.

Candidate's Name:___________________ 13 COMPSCI 101 TEST CONTINUED Overflow Sheet 1 Write the question number next to your answer. You must ALSO indicate in the allotted space that you have used the overflow sheet.
Image of page 13
Candidate's Name:___________________ 14 COMPSCI 101 Overflow Sheet 2 Write the question number next to your answer. You must ALSO indicate in the allotted space that you have used the overflow sheet. _______________________
Image of page 14
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Subroutine, formal methods, syntax error, Logic error

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