CS 1713-001 Introduction to Computer Science
Fall 2003 -- Review for Exam 2 (Friday, 14 Nov 2003)

ALONG WITH ANSWERS TO REVIEW QUESTIONS
(Note: There are many possible correct answers.)

Primary material: Lecture 12 (Review)

Next in importance: Project 2 and its solution

Next in importance: Lectures 7 to 11: See Calendar

Things you will not be tested on:

Emphpasis on: Arrays and sorting and searching, as shown in the practice problems below.


Practice Problems:
  1. Assume that grades is an array of ints. Write a Java loops and other code segments to accomplish each of the following:
    1. Initialize grades so that it has the values 58, 92, 86, 75, 48, 79, and 67 (in that order).
    2. Add 3 to each element in grades.
    3. Square each element of grades.
    4. Find the largest value in grades.
    5. Find the position of the largest value in grades.
    6. Use the book's insertionSort to sort grades into order.

    Answers to Problem 1.
    Write separate Java methods to carry out items b., c., d., and e. above.
    Answers to Problem 1 Using Methods.

  2. Assume that fruits is an array of Strings. Write Java loops and other code segments to accomplish each of the following:
    1. Initialize fruits so that it has the values banana, orange, grapefruit, strawberry, apple, and melon (in that order).
    2. Set the element in position 5 (the 6th element) of fruits to watermelon
    3. Find the first element of fruits, that is, the element that comes first in alphabetic order. (Use the method compareTo.)
    4. Sort fruits into order by rewriting the book's insertionSort code, using the compareTo method of the String class to compare the strings.

    Answers to Problem 2.

  3. Write the following public methods.
    1. public boolean match(int [] a, int [] b) returns true if a and b are of the same length and their elements match.
    2. public boolean match(String [] a, String [] b).
    3. public int countValues(int [] a, int value) returns the number of times value appears in the array a.
    4. public int linearSearch(String [] a, String name) returns the position of an item that matches name in the array a.

    Put these all into a class and test them.
    Answers to Problem 3.

  4. Output the number of times each of the numbers 0 through 9 appear in a stream of 1000 random numbers that range from 0 to 19.
    Answer to Problem 4.

Revision date: 2003-11-10. (Use ISO 8601, an International Standard.)