This website is preserved for historical and scholarly reference and is no longer actively maintained.
Reading Assignments and Reading Homework
CPSC 210 -- Spring 2002
The following reading assignments are made.
Homework, in the form of a synopsis of the reading assignment or other designated assignment, will be collected at the beginning of the designated class meeting.
The intention of these assignments is
(1) to persuade everyone to read the text books,
(2) to allow the instructor more time to emphasize major concepts and critical points in the text material,
and (3) to allow class discussions to introduce material not covered in the text.
Homework not turned in when collected in class will be designated "late." Late homework will be accepted until 4 p.m. on the date the homework is due.
Homework will not be accepted after 4 p.m. on the date due except in exceptional circumstances. Late homework will receive a 2 point (out of 10 points) penalty.
Homework will not be accepted by email. If class is cancelled due to inclement weather, the homework will be due at the next class meeting.
The text of homework must be typed (unless specified otherwise) using new Roman Times or Times 12 pt. font, single spaced, and double spaced between paragraphs.
Figures may be (probably should be) hand-drawn.
Unless specified otherwise, it is expected that the synopsis be at least 1.5 pages in length.
Homework must have the name of the person submitting the homework typed at the top of the first page.
All pages must be stapled together. Failure to follow instructions will result in deduction of points
(1 pt. for no name, 1 pt. for not correctly stapled).
These homeworks will be valuable to you. You will probably be allowed to use
your synopses on some daily quizzes, on some part of some hour quizzes, and on some part of the final exam.
Therefore, you should organize your work so that you can make the best
use of it as a reference.
Homework is to be done individually. The purpose of the "read and
report" homework is to make it possible to cover the course material
effectively. Thus, it is important that each person read and
write for themselves. If the instructor believes that homework has
been jointly written, on the first occurrence the grade will be
shared jointly among the writers. A second occurrence will be
considered academic dishonesty and appropriate action will be taken.
#1 (due Friday, January 9)
Read Chapter 1 of Java Elements and answer the following questions
1. What integer will be produced by 12 % 5 ? (1 pt.)
2. What integer will be produced by 5 % 12 ? (1 pt.)
3. What integer will be produced by 5 % 5 ? (1 pt.)
4. What will be the result of 12 / 5 ? (1 pt.)
5. How would you have a program print the amount of time it takes to run ? Give code and state page reference. (2 pts.)
6. What is the result of "Clem" + "son" ? (1 pt.)
7. What is the name of the operation in #6 ? (1 pt.)
8. Write code that will produce a random number between 0 and 9 inclusive. (2 pts.)
#2 (due Monday, January 14)
Answer the following questions related to Chapter 1.
1. If the variable salary is of type float, write a statement that
stores the value in salary in the int variable intSalary. (1 pt.)
2. The code on pages 21-22 prints a point at 2 o'clock on a clock
face (the circle). What single line of the program needs to be changed
to print a point at 1 o'clock on the clock face? (1 pt.)
3. What should that line read? (1 pt.)
4. What convention is followed in choosing the first symbol of a
variable name with respect to capitalization? (1 pt.)
5. If 'A' is assigned to a variable varOne, what type should varOne be? (1 pt.)
6. If "A" is assigned to a variable varOne, what type should varOne be? (1 pt.)
Read Chapter 2 (omit section 2.1 on the Console window and all use of the Console window) of Java Elements and answer the following questions:
7. Write code to draw a square of side 100 with upper left hand corner at (25, 25) by drawing 4 lines. (1 pt.)
8. Write code to draw a square of side 100 with upper left hand corner at (25, 25) by drawing an object of type Rect. (1 pt.)
9. Draw the result after each execution of "d.fill" for the code on page 57. Each drawing should have all vertices of the bounding rectangle clearly shown. (2 pts.)
#3 (due Wednesday, January 16)
In problems 1 - 3, your code should execute to give exactly the same results
in all situations.
1. Rewrite the following code using a while-loop
do
{
x = x + inc;
n = n * n;
} while ( n < nMax );
2. Rewrite the code in problem 1 using a for-loop.
3. Rewrite the following code using a do-loop
while ( x <= xMax )
{
s = s / 10;
x = x + inc;
}
4. Rewrite the code in problem 3 using a for-loop.
5. Rewrite the following code using a while-loop.
for ( y = x ; z < x + y ; y = y - inc )
{
sum = sum + y;
}
6. Rewrite the code in problem 5 using a do-loop.
7. In a single sentence, when do you prefer the do-loop?
8. In a single sentence, when do you prefer the for-loop?
9. Rewrite the following code using a switch statement.
if ( c == 'a' || c == 'e' || c == 'i' || c == 'u' )
vowel = true;
else
vowel = false;
10. Read problem 3.1 on page 92. Was 1900 a leap year? Justify your answer.
#4 (due Friday, January 18)
1. In Chapter 1, Exercise 1.5 on page 23 asks for what is printed in two situations.
Write and execute a small program to find the answers.
State the answers and explain how the results were computed. (4 pts.)
2. Write a method that converts a lowercase letter to uppercase. (Exercise 4.7 on page 118, 3 pts.).
3. Write a method that swaps 'a' for 'z', 'b' for 'y', and so on. (Exercise 4.8 on page 118, 3 pts.).
#5 (due Wednesday, January 23)
1. Complete the following method:
public static int convert ( char c )
// pre: c is a character between '0' and '9' inclusive in ASCII code
// post: The integer corresponding to c is returned
2. Suppose String s is "Clemson University");
What is returned by s.charAt(1)?
What is returned by s.indexOf(' ')?
What is returned by s.substring(10)?
What is returned by s.substring(10,11)?
3. Work Exercise 5.10 on page 137.
#6 (due Friday, January 25)
1. Write code using a minimum of comparisons and no unnecessary assignment statements that will sort String variables varOne, varTwo, and varThree so that varOne <= varTwo <= varThree. Do not attempt to use a method to accomplish this task. Write a sequence of Java statements that might be in a main method. (5 pts.)
2. Complete the method below which returns the reverse of the input parameter. (5 pts.)
public static String reverseString ( String inputString )
// post: returns the reverse of inputString
{
#7 (due Monday, January 28)
1. Answer problem 5.1 on page 143. (2 pts.)
2. Write a method that, given a String s and a character w, removes every
instance of the character w. For example, if the String is "Hello" and
the character is 'l', the String "Heo" is returned. (8 pts.)
public static String removeChar ( String s, char w )
// pre: s is not null
// post: the String s, with all instances of w removed, is returned
#8 (due Wednesday, January 30)
1. State your grades for each of the first four daily quizzes, identifying each by quiz # and date. (4 pts.)
2. Write an itertive method (i.e., using a while loop} to compute the sum of the digits of a non-negative integer. (3 pts.)
public static int sumDigits ( int number )
// pre: number >= zero
// post: returns sum of digis of number
3. Write a recursive method to computer the sum of the digits of a non-negative integer. (3 pts.) This method will return the same result as the iterative method in question #2, but this method will not contain a loop. (Recursive methods may sometimes contain loops, but this example will not contain a loop.)
#9 (due Friday, February 1)
The following problems refer to chapter 7:
1. List the first 10 prime numbers. (2 pts.)
2. Write a method that creates an array of Strings having the following values: arrayList[0] = "So", arrayList[1] = "far", arrayList[2] = "life", arrayList[3] = "as", arrayList[4] = "a", arrayList[5] = "Java", arrayList[6] = "programmer", arrayList[7] = "seems", arrayList[8] = "simple".
(2 pts.)
A few lines of the method are as follows:
public static void createArray ( String [ ] stringList )
// post: returns array of Strings to calling program
{
... // initialize stringList here
}
3. Write a method that removes the first occurrance of the String toRemove without changing the order of the list. (2 pts.)
public static String removeString ( String [ ] target, String toRemove )
// pre: String toRemove exists and is not the empty String
// post: Removes first occurrance of toRemove, preserving relative
// order of remaining elements of array. An empty String is
// inserted as the last entry in target array.
4. Write a method that creates the same list of Strings as in question #2, but stores the list of Strings in a Vector instead of in an array. (2 pts.)
public static void createVector ( Vector stringList )
// pre: Vector stringList exists and is empty
// post: returns Vector of Strings to calling program
{
... // initialize stringList here
}
5. Write a method that removes the first occurrance of the String toRemove
from the Vector target, without changing the order of the remaining Strings
(2 pts.) The method should be:
public static void removeString ( Vector target, String toRemove )
// pre: String toRemove exists and is not the empty String
// post: if toRemove is an entry in target, toRemove is removed
// without changing the order of the remaining Strings in target.
// Only the first occurrance of toRemove is removed.
{
... // code here
}
#10 (due Monday, February 4)
These questions refer to Chapter 8 of Java Elements.
1. Answer question 8.2 on page 234. (What is an "abstract data type," and why is it considered an improvement over systems that don't support data abstraction? (3 pts.)
2 Answer question 8.5 on page 234 (7 pts.) The name of the class should be "Name". You will need three String variables to store the first, middle, and last names. (Should these be public or private?) You will need the following methods:
public Name ( String wholeName ) // a constructor
// pre: The first and last names are each separated from the middle
// name by at least one space, as in "Jane Smith Doe" .
public Name ( String first, String middle, String last ) // constructor
public String getFirst ( )
// post: returns the first name of the person
public String getMiddle ( )
// post: returns the middle name of the person
public String getLast ( )
// post: returns the last name of the person
You are not requested to include the main method, but an example of how the
main method might begin is:
public static void main ( String [ ] args )
// test driver for Name class
{
Name one = new Name ( "John Quincey Adams" );
System.out.println ("The last name of John Quincey Adams is "
+ one.getLast ( ) );
Name two = new Name ( "George", "Bernard", "Shaw" );
System.out.println ("The middle name of George Benard Shaw is "
+ two.getMiddle ( ) );
// more test code here
}
#11 (due Wednesday, February 6)
1. Assume that you have executed the following code:
public class Primes
{
static int [ ] primeArray;
static int primeCount;
static final int MAX_COUNT = 20;
public static void main ( String [ ] args )
{
primeArray = new int [ MAX_COUNT ];
primeArray [0] = 2;
primeArray [1] = 3;
primeArray [2] = 5;
primeArray [3] = 7;
primeArray [4] = 11;
primeCount = 5; // the number primes stored in the array primeArray
storeNextPrime ( );
// more code with more calls to storeNextPrime and print statements
}
write code for the method below which finds the next prime and stores it as the next element of primeArray. (10 pts.)
For this to work, primeArray and primeCount must be global to the class Primes.java, as shown. This method is very different from the method in the text and calls for a very different approach. Be sure to walk through your code to be sure that it works. Although correctness is the primary concern when writing code, if your code works correctly efficiency will be a factor in the grading.
private static void storeNextPrime ( )
// pre: primeCount < MAX_COUNT - 1
// post: primeArray[primeCount] is initialized to the next prime
// after primeArray[primeCount - 1] and primeCount is incremented.
#12 (due Friday, February 8)
Read Chapters 10 and 11 of Java Elements and submit answers to the following
questions. (10 pts.)
- 1. What is a container class?
- 2. What is the purpose of threads in Java?
- 3. What is a Java Virtual Machine and why is it important?
- 4. Why is the Turing machine an important concept?
- 5. What is a P-RAM? What is it used for?
No homework for Wednesday, February 13
#13 (due Friday, February 15)
In the handouts to the class the week of February 4, there was a
program using prime numbers. One of the methods that was not complete
was the resize method. Complete the resize method.
// names of global variables
static private int [ ] primeArray; // holds the prime numbers
static int primeCount; // how many primes have been stored
private static void resize ( )
// post: the size of the array holding the primes is doubled
{
// code goes here
}
#14 (due Friday, February 22)
1. Answer question 3.1 on page 47. Explain the difference between the size and capacity of a vector? (1 pt.) Which is more important to the user? Justify your answer. (1 pt.)
2. Answer question 3.5 on page 47.
Write a Vector method, indexOf, that returns the index of an object in the Vector. (4 pts.)
What does java.lang.Vector do if no "equals" object can be found? (2 pts.)
How long does this operation take to perform (in terms of N, the size of the Vector), on average? Justify (explain) your answer. (2 pts.)
#15 (due Monday, February 25)
1. Problem 4.3 on page 73 asks for the running time of the reduce method.
You are to prepare a table showing the value that will be returned by
the reduce method for each of n = 1, 2, 3, 4, 8, 16, and 32. (2 pts.)
2. Give the running time of the reduce method as
a function of n (big-oh). (2 pts.)
3. Work problem 4.2 on page 75. In each case your answer should give the time complexity as a function of the size of the Vector. (6 pts.)
#16 (due Friday, March 8)
1. Write a method of DoublyLinkedList, called reverse, that reverses the
(logical) order of the elements in the list. This method should be
destructive. What is the worst case time complexity of your method?
(5 pts.)
2. Write a List method, equals, that returns true exactly when the
elements of two lists are pair-wise equal. Ideally, your implementation
should work for any List implementation (SInglyLinkedList, DoublyLinkedList,
CircularList, etc.) without change. A possible way to approach this
problem is to have the public method equals call a private recursive
method that traverses the Lists. (5 pts.)
To answer the second question, we will assume use of the methods:
private ListElement first ( ) // in the List class
// post: returns a reference to the first Element in the List
private ListElement next ( ListElement current ) // in the ListElement class
// post: returns a reference to the the next ListElement after current
#17 (due Monday, April 1)
Using the array, data, shown below, produce a Figure in the same form as
Figure 7.1 on page 130, showing the sequence of call frames. (10 pts.)
Instead of stopping after five frames, your presentation should continue
until the data is sorted. The partition method can be found on page 89. Homework that is not in
the same format as that in Figure 7.1 will not be graded.
30 -2 15 18 -5 0 10 -35 65
[0] [1] [2] [3] [4] [5] [6] [7] [8]
#18 (due Friday, April 26)
Part 1 (3 pts.): List the 30 daily quiz grades, giving quiz number and date beside
each grade. (Quizzes #28, #29, and #30 will be returned Wednesday.) State the sum, the sum of the best 24 quiz grades, and the average of the best 24 grades to one decimal place.
All must be clearly labelled.
Part 2 (7 pts.): Draw expression trees for each of the following, clearly identifying each drawing.
- A = - B
- A = - B - C
- A = (-B) - C
- A = - (B - C)
- A = - B + ( - C )
State your rule for how to handle the unary minus and justify your rule.