Algorithms and OOD (CSC 207 2014S) : Assignments

Assignment 2: Getting Started with Java and Testing

Due: 10:30 p.m., Wednesday, 29 January 2014

Summary: You will explore a variety of issues related to Java programming

Purposes: To get you used to programming in Java. To get you used to submitting homework via GitHub. To help you start thinking about objects. To get you accustomed to test-focused development.

Collaboration: You are required to work in a group. I encourage you to work in groups of size three. You may, however, work in a group of size two or size four. You may discuss this assignment with anyone, provided you credit such discussions when you submit the assignment.

Wrapper (Prologue): Individually read through this assignment and make sure that you understand what is required. Then use the form available at to indicate (a) how long you think this assignment will take and (b) what you think will be the most challenging aspect of this assignment.

Wrapper (Epilogue): When you are done with the assignment, fill out the form available at to indicate (a) how long the assignment took, (b) what the most challenging part of the assignment was, and (c) something important you learned from doing the assignment. If you find that the assignment took much less or much more time than you expected, also include (d) a note as to what might have led to that difference.

Submitting: Please put all of your work in a GitHub repository named csc207-hw2. Email the address of that repository to Please use a subject of “CSC207 2014S Assignment 2 (Your Name)”.

Warning: So that this assignment is a learning experience for everyone, we may spend class time publicly critiquing your work.

A Quick Note on Arrays

A few problems in this assignment ask you to work with arrays. Arrays in Java work much like arrays in C except, except that you can find out the length of an array with array.length and you use the following approaches to initialize arrays.

     // Create an array of 10 integer values.
     int[] vals = new int[10];
     // Create an array with particular strings
     String[] profs = { "Davis", "Rebelsky", "Stone", "Walker", "Weiman" };


Part A: Java as Imperative Language

Consider the six following static methods.

  • isMultiple, which takes as input two variables, a and b, of type long, and returns true if and only if there is an integer, i, such that a == b*i.
  • isOdd, which takes as input a variable, i, of type int returns true if i is odd and false if i is not odd. In solving this problem, you may not use multiplication, modulus, or division. You also may not use isMultiple.
  • oddSumTo, which takes as input a variable, n, of type int, and returns the sum of all positive odd numbers less than n.
  • isOddProd, which takes as input ints, an array of int values and returns true if any pair of numbers in the array has a product that is odd and false otherwise. Note that you can use ints.length to determine the length of the array.
  • allDistinct, which take as input an array of int values and returns true if no two elements have equal values and false otherwise.
  • reverseInts, which takes as input an array of int values and reverses their order in the same array.

We will group all of these in a class we call TwoA. (How's that for creative naming?)

1. Write a class, TestTwoA, which contains six methods, each of which tests one of the methods above. Your tests should use the assertEquals(String message, type expected, type actual) method. For example, I might write,

    void test_isOdd(void) {
      assertEquals("negative even", false, TwoA.isOdd(-4));
      // Integer.MAX_VALUE is 2^31 - 1, which is odd
      assertEquals("MAX_VALUE", true, TwoA.isOdd(Integer.MAX_VALUE));
    } // test_isOdd

2. Implement all of the methods in TwoA.

Part B: Averaging

The following method has at least one subtle bug. Identify and correct that bug.

public class TwoB {
   * Compute the average of two integers.  Rounds toward zero if the
   * average is not a whole number.
  public static int 
    average (int left, int right) 
    return (left + right) / 2;
  } // average(int,int)
} // class TwoB

Part C: Designing Objects

When designing objects, a good first step is to consider what natural operations are associated with the object. Traditionally, we consider three kinds of operations:

  • Observers extract information from an object, but do not modify the object. For example, an might have an observer that extracts the value of a counter object.
  • Mutators modify objects. For example, we might have a mutator that increments the value of a counter object.
  • Constructors build new objects. For example, we are likely to want a constructor that creates a new counter object.

Some methods do more than one thing. For example, an object that represents an idea might have a method that creates a derivative idea. Such a method is a constructor, because it creates a new idea. But it's also likely to mutate our original idea (at the very least, by creating a link to the derivative).

Sketch a class that provides all of the methods you think would be useful for working with rational numbers. For each method, indicate you would want such a class to support. For each method, indicate name, inputs, output, general purpose, and type of method (observer, mutator, constructor).

You sketch should be code that compiles, but need do anything. You should use a stub for the bodies of methods. That is, just return some default value. If you need to return an object, return the special value null.

Note that the object is implicit for methods associated with that object. Hence, you need not specify the rational number you're working with. For example,

   * Get the numerator of this rational number.  (Observer)
  public int numerator () 
    return 0;        // STUB
  } // numerator

   * Multiply this rational number by an integer.  (Observer/Constructor)
  public Rational multiply (int multiplicand) 
    return null;     // STUB
  } // multiply (int)


The six methods in part A are adapted from an assignment by Jerod Weinman. Prof. Weinman in turn adapted those from section 1.0 of M. T. Goodrich and R. T. Tamassia's Data Structures & Algorithms in Java, 4/e (Wiley, 2005).

The Rational problems are based on problems I wrote for CSC 152 in the distant past.

Copyright (c) 2013-14 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.