Algorithms and OOD (CSC 207 2013F) : Assignments

Assignment 2: Getting Started with Java and Testing


Due: 10:30 p.m., Monday, 9 September 2013

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: I encourage you to work in groups of size three. You may, however, work alone or work in a group of size two or size four. You may not work with people you worked with on the previous assignment. 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 http://bit.ly/207hw2pro 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 http://bit.ly/207hw2epi 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 me the address of that repository. Please title your email “CSC207 2013F 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" };

Assignment

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.
  • 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,

    @Test
    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).

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)

It's okay if this code doesn't compile. Just do your best.

Part D: Rational Tests

Write a set of tests for the methods you've created. (Yes, most of these tests will currently fail.) As in the cases above, you should use assertEquals in your test. (You may also have to create a few rational numbers along the way.)

Citations

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 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 http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.