CS 178 Programming with Multimedia Objects

Fall 2004

Practice Problems for Exam 2

Prepared by: Aditya Mathur.

Exam 2 schedule: Oct 25, 2004. 7:8:30pm. Room: EE 270.

Latest update: Oct 20, 2004.

Part A: Closed Book. 10 multiple choice questions. Total: 20 points. Time: allowed: 30 minutes. It will cover all material covered during Exam 1 and material from the textbook: Chapter 5.1, 5.2 (variables and identifiers). 5.3, 5.4, (pages 194-198), Chapter 8.1, 8.2 (exclude multidimensional arrays), 8.3 (exclude Insertion sort and Quicksort). You should be comfortable with loops and single dimensional arrays to excel in Part A and Part B.

Part B: Open Book. Total: 80 points. Time allowed: 60 minutes.Two questions. Question 1: You will be asked to code a given algorithm into a Java applet. This and the next question will require knowledge of loops and single dimensional arrays and the ability to code simple array operations as methods (see practice problems below).

Question 2: You will be asked to write Java code for a few methods that perform simple array operations. The practice problems below should help you prepare to deal with this question.

Sample question for Part A:

planetName is an array with 19 as the highest subscript (subscript is also known as index into the array). Which of the following is the correct declaration for planetName.

(a) String planetName=new String("Mars");

(b) String [ ]planetName=new String[19];

(c) String [ ]planetName=new String("Mars");

(d) String [ ]planetName=new String[20];

Preparation for Part B:

  1. Examine the array-related applets presented during lectures (these are linked to the schedule page). For each applet, try writing the code on your own with help from the textbook, with help from Eclipse, without help from anyone, and without consulting your notes, then you are well prepared for Exam 2.
  2. Code the following methods in Java.Te each method you write using Eclipse to make sure that it is correct..
    • public void initArray(String s)
    • This method initializes each element of a global array proteinNames to the string s.

    • public void copyAray(int[] A, int [] B)
    • This method copies elements of array B into the corresponding elements of array A. For example, B[0] is copied into A[0, B[1] into A[1] and so on.

    • public boolean exists(String [] names, String s)
    • This method returns true if the string s exists in the array names, otherwise it returns false.

    • public boolean exists(String [] names, String s)
    • This method returns true if the string s exists in the array names, otherwise it returns false.

    • public void initTextFields(TextField [] t, String s)
    • This method creates each element of the TextField array t and sets its label to string s. Notice that t is an array of TextField implying that each element of t is of type TextField. Recall that we use the new keyword in Java to create an object.

    • public void initTextFields(TextField [] t, String [] s)
    • This method creates each element of the TextField array t and sets its label to the corresponding element in the string array s. For example, TextField t[0] is created and its label set to s[0], TextField t[1] is created and its label set to s[1], and so on.

    • public double findAverage(double[] data)
    • This method returns the average of all elements in the data array.

    • public double findAverage(double[] data, int k)
    • This method returns the average of all elements in the data array starting from element at position 0 to the element at position k-1.

    • public int findZeroes(double[] data)
    • This method returns the number of zeroes in the data array.

    • public int findLarger(double[] atomicWeight, double value)
    • This method returns the number of elements in the atomicWeight array that are greater than value.

    • public void initButtons(Button[] b, String [] s)
    • This method creates each element of the Button array b and sets its label to the corresponding element in the string array s. For example, Button b[0] is created and its label set to s[0], Button b[1] is created and its label set to s[1], and so on.