This website is preserved for historical and scholarly reference and is no longer actively maintained.

Homework Assignments

CPSC 210 -- Fall 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. Figures may be (probably should be) hand-drawn. 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 (0.5 pt. for no name, 0.1 pt. for not correctly stapled).

These homeworks will be valuable to you. You will probably be allowed to use your homework 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 homework is to make it possible to cover the course material effectively. Thus, it is important that each person read and worked 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, August 22)

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, August 25)

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, August 27)

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, August 29)

Run a Java program to find the results of the println statements in Exercise 1.5 on page 23. Turn in the result of each println statement and an explanation (in English) of why each println statement gave the result it did. Your answers to this problem should be typed, but the typing is not required to be done on a word processor.

#5 (due Friday, September 6)

Reread Chapter 5. Work problem 5.6 on page 143. You are to turn in the typed method, including postcondition and precondition (if required). It is not necessary to run this method/program, but you are more likely to write correct code if you do so.

#6 (due Monday, September 9)

Read Chapter 6. Write a solution (typed, please) to problem 6.10 on page 159. "Write a recursive method to compute the remainder ... "

#7 (due Friday, September 13)

Read Chapter 7. Write answers to problems 7.6 and 7.8 on page 190. In Problem 7.8, if set A contains the elements 1 and 3, then the elements of the array representing set A would be false, true, false, true, false, false, ... If set B contains the elements 1 and 2, then the elements of the array representing set B would be false, true, true, false, false, ... The union of sets A and B would contain 1, 2, and 3, and would be represented be an array false, true, true, true, false, false, ... There is no requirement that the sets A and B have the same number of elements.

#8 (due Monday, September 16)

Continuing in Chapter 7, finish reading the chapter if you have not already done so. (1) Write a method that initializes a Vector to n random Integers (Note that Integer is a class that holds ints.). The value of n should be passed to the method as a parameter and the return type should be Vector. (2) Write a program that shifts the entire contents of the Vector one position to the right. For example, if the name of the Vector is myVec, then the contents of myVec[0] will be moved to the location of myVec[1], the contents of myVec[1] will be moved to the location of myVec[2], ..., the contents of myVec[myVec.size()-2] will be moved to the location of myVec[myVec.size()-1], and the contents of myVec[myVec.size()-1] will be moved to the location of myVec[0]. Of course, you cannot access myVec[whatever] using array subscripting and must use the Vector methods. Use only Vector methods given in the text in Chapter 7 or in Appendix D.7 (pages 303-304). Also, no information in the Vector is to be destroyed in this process. Remeber to include post conditions on your methods.

#9 (due Wednesday, September 18)

Write a method
      public static void insertSorted ( Vector myVec, int newVal )
      // pre: Vector myVec is sorted in non-descending order.
      //      i.e., for all subscripts j, the entry at index j-1 is
      //      less than or equal to the entry at index j
      // post:  An Integer containing newVal is inserted into myVec
      //      and myVec remains in non-descending order
The only Vector methods you are allowed to use are those in Appendix D.7 (pages 303-304). The only Integer methods you may use are the constructor (new Integer ( x )), intValue() which returns the value of this Integer as an int, and compareTo which compares the values of to Integers numerically. You are not required to use compareTo because you may convert the Integers to ints and then compare them using <, <=, >, >=, and == operators.

#10 (due Wednesday, September 25)

Repeat assignment #9. Also, demonstrate with a walk-through of the code that your method works for the following sequence of test cases:
      insertSorted ( myVec, 25 );  // when myVec is an empty Vector
      insertSorted ( myVec, 2 );  // should NOT be treated as a special case
      insertSorted ( myVec, 10 );
      insertSorted ( myVec, 30 );
      insertSorted ( myVec, -5 );

#11 (due Monday, September 30)

Read Chapters 10 and 11 of Java Elements and submit answers to the following questions. This homework should be typed.

#12 (due Wednesday, October 2)

Read chapters 0, 1 and 2 of Java Structures and submit answers to the following questions. This homework should be typed.

#13 (due Wednesday, Octobe 9)

Read about 2-dimensional arrays (page 175-183 of Java Elements) and the implementation using Vectors (pages 43-47 of Java Structurees).
Also, solve the problem posed by email:
   
 
// homework for Wednesday, October 9
// This program sometimes reverses a Vector and sometimes does not
// You are to find out what does not work and explain why it does
// not work.  Pictures would probably be helpful.


import java.util.Vector;

public class BadVector
{
   public static void reverseVector ( Vector myVec )
   {
      if ( myVec.size() < 2 ) return;

      if ( myVec.size() == 2 )
      {
         Object first = myVec.firstElement();
         myVec.removeElementAt(0);
         myVec.addElement(first);
         return;
      }

      // myVec.size( ) >= 2
      Vector newVec = new Vector ( );
      for (int k = myVec.size()-1 ; k >= 0 ; k-- )
      {
         newVec.addElement(myVec.elementAt(k));
      }
      myVec = newVec;
   }

   public static void printVector ( Vector myVec )
   {
      System.out.println ("Contents of Vector: ");
      for ( int k = 0 ; k < myVec.size( ) ; k++ )
      {
         System.out.print ("  " + myVec.elementAt (k) );
         if ( k != 0 && k % 10 == 0 )        
            System.out.println( );
      }
      System.out.println ( );
   }

   public static void main ( String [] args )
   {
      Vector myVec = new Vector ( );
      myVec.addElement ( new Character ('a') );
      printVector (myVec);
      System.out.println( );

      myVec.addElement ( new Character ('b') );
      printVector (myVec);
      reverseVector (myVec);
      System.out.println ("Call reverseVector");
      printVector (myVec);
      System.out.println( );

      myVec.addElement ( new Character ('c') );
      printVector (myVec);
      reverseVector (myVec);
      System.out.println ("Call reverseVector");
      printVector (myVec);

   }
}

#14 (due Friday, October 11)

Read Chapter 5 (Sorting). pages 77 - 96. Write a recursive Selection Sort algorithm. If you prefer to write a recursive program to do Selection Sort, you may do that instead.

#15 (due Monday, October 14)

Begin reading Chapter 6. Read to the middle of page 116, the end of the section on Singly Linked Lists.

From the reading, answer the following questions:
1. Question 6.1 on page 124.
2. What does it mean for a class to be "abstract"?
3. How is unreferenced memory recycled?
4. Does the list interface provide information on how the list is actually implemented?

#16 (due Monday, October 21)

Read pages 127-136 (stacks) in chapter 7. Answer questions 7.3 and 7.4 on page 152.

#17 (due Wednesday, October 23)

Read pages 136-152 (queues) in chapter 7. Answer questions 7.5 and 7.12.

#18 (due Friday, October 25)

(You might want to do this assignment early if you are going to the game Thursday night.) Read chapter 8 (iterators) and work problem 8.2 on page 165.

#19 (due Wednesday, October 30)

Read (or reread) chapter 9 (Ordered Structures). Answer questions 9.1 and 9.2 on page 185 by drawing the data structures, including labeling of all fields. Answer question 9.4 with a drawing that has a pointer to the oldest value and newest value. Draw the OrderedList with a total of three duplicate items. Answer 9.5 in some detail. You should examine the growth rates of the permissable operations (methods.).


Return to Eleanor Hare's home page.

Return to Department of Computer Science Home page.