102 Homework Assignments

Summer 2001

Homework must be typed (unless specified otherwise) and will be collected at the beginning of class on the date the homework is due. No homework will be accepted after 4:00 p.m. on the day the homework is due.

If you are unable to come to class you may submit your homework at the class meeting before it is due or send the homework with another student. If you have an excused absence and turn the homework in late, the number of homeworks averaged will not include that homework.

Homework must be typed to be accepted. If more than one page is turned in, the pages must be stapled together.

Homework is to be done individually. The purpose of the "read and outline" homework is to make it possible to cover the course material in five weeks. 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 Wednesday, May 23) 10 pts.

Read Chapter 1 (pages 1 - 33). Write code for Self-help Exercise 11 on page 31. Include all documentation (precondition, postcondition, etc.) and error conditions thrown.


#2 (due Thursday, May 24) 10 pts.

Begin reading Chapter 2. You will find code for a class Throttle on pages 58-59. Write methods

          public boolean equals (Object obj)

public String toString( )
to complete the Throttle class.


#3 (due Friday, May 25) 10 pts.

Finish reading Chapter 2. How does passing Objects as parameters differ from passing predefined types as parameters? (5 pts.) Write a method

          public static Location midPoint (Location p1, Location p2)
that returns the Location that would be the midPoint of a line connecting p1 and p2 (5 pts.)


#4 (due Monday, May 28) 10 pts.

Read Chapter 3 through page 133. Write the following two methods:

          public static void insert (int [ ] array, int where, int item)
          // pre: 0 <= where < array.length
          //      and all entries of array have been initialized (don't execute throw on this part of preconditiion)
          // post: item is inserted at array[ where ].
          //       if necessary, elements of the array are moved to the right to make room for item and
          //       the last (rightmost) element of the array is removed (and lost).
        
          public static boolean remove (int [ ] array, int item)
          // pre:  all entries of array have been initialized (don't execute throw on this precondition)
          // post:  If item is present in array, the first occurrence of item is removed and "true" is returned;  
          //        Otherwise "false" is returned.
          //        The remaining elements of the array are moved to the left to fill in the place where item was 
          //        removed and the last (rightmost) element of the array is set to zero.


#5 (due Wednesday, May 30) Problem 1 on page 168 -- 10 pts.

Write an equals method for type IntArrayBag. This method tests that two IntArrayBags have identical contents, but not necessarily stored in the same order. This method ignores the allocated size of the data structure. Throw an exception if obj is not of type IntArrayBag and assign obj to a local variable of type IntArrayBag, as demonstrated in the text.
      public boolean equals ( Object obj )
      // post:  Returns true if this contains the same elements as obj;
      //        Otherwise, returns false.


#6 (due Thursday, May 31 ) Problem 2 on page 238 -- 10 pts.

Write a new static method for the node class. The method has one parameter which is a head node for a linked list of integers. The method computes a new linked list, which is the same as the original list, but in which all repetitions are removed. The mthod's return value is a head reference for the new list.

#7 (due Friday, June 1) Problem 3 on page 238 -- 10 pts.

Write a method with three parameters. The first parameter is a head reference for a linked list of integers, and the next two parameters are integers x and y. The method should write a line to System.out, containing all integers in the list that are between the first occurrence of x and the first occurrence of y.

#8 (due Monday, June 4) 10 pts.

Complete the following two methods for use in an applications program. These methods may not use the instance variables of the IntNode class, but must instead use methods of the IntNode class.
          public static void exchangeWithFirst ( IntNode head, IntNode cursor)
          // pre:  Neither head nor cursor is null
          // post:  Exchanges the information fields in the IntNode referenced 
          //        by head with the information field in the IntNode
          //        referenced by cursor

          public static IntNode nextToLast ( IntNode head )
          // pre:  head is not null
          // post:  Returns a reference to the nextToLast IntNode in the
          //        list referenced by head.  If the list contains only
          //        IntNode, null will be returned.
The next to last IntNode is the IntNode containing a link to the last IntNode.


Tuesday, June 5 -- Hour Quiz 1


#9 (due Wednesday, June 6) 10 pts.

Pages 227 - 233 of the text contain a description of a class DoubleLinkedSeq. You are to type (into a file named DoubleLinkedSeq.java) the "outline" of the code for the class. You should declare all the instance variable you intend to use at the beginning of the class. You should then write the signatures of all the constructors and public methods of the class. Each method should have a clearly stated postcondition. If there is any circulmstance that the method will not work, an appropriate precondition should be included. If the specification for the method (Figure 5.1) indicates that the method throws an exception, code the exception in the method (provided that inserting the exception does not require writing the code for the method). If the method returns a value, return an arbitrary value of the required type. Otherwise, do not write any code. For a start:
          // your name
          // CPSC 102

          public class DoubleLinkedSeq
          {
             // instance variables
  
             // default constructor

             // other constructor(s) and methods
          }
You should be able to compile the class without error. Turn in the listing from the Unix file in class.


Return to Eleanor Hare's home page.

Return to Department of Computer Science Home page.