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:
- No 2-dimensional arrays.
- No arrays of classes, except for arrays of Strings.
Emphpasis on: Arrays and sorting and searching, as shown
in the practice problems below.
Practice Problems:
- Assume that grades is an array of ints.
Write a Java loops and other code segments to accomplish each of the following:
- Initialize grades so that it has the values
58, 92, 86, 75, 48, 79, and 67 (in that order).
- Add 3 to each element in grades.
- Square each element of grades.
- Find the largest value in grades.
- Find the position of the largest value in grades.
- 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.
- Assume that
fruits is an array of Strings. Write Java
loops and other code segments to accomplish each of the following:
- Initialize fruits so that it has the values
banana, orange,
grapefruit, strawberry,
apple, and melon (in that order).
- Set the element in position 5 (the 6th element)
of fruits to watermelon
- Find the first element of fruits,
that is, the element that comes first in alphabetic order.
(Use the method compareTo.)
- 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.
- Write the following public methods.
- public boolean match(int [] a, int [] b) returns true
if a and b are of the same length and their elements match.
- public boolean match(String [] a, String [] b).
- public int countValues(int [] a, int value) returns the number
of times value appears in the array a.
- 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.
- 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.)