CS 367: Introduction to Data Structures

Spring 1999

Assignment 2: Simple Java Programming

Due: Friday, February 12 (before Midnight)


  1. One of the most famous mathematical constants is pi, the ratio of the circumference of a circle to its diameter. It is known that pi has no finite decimal representation. In fact for centuries mathematicians (and later computer scientists) have computed pi to an ever-increasing number of digits.

    There are a number of formulas that can be used to compute pi. One of the simplest is the following infinite series:

             pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13 - 4/15 + ...
    

    You are to write a Java class, PiEstimator, that has a method called ComputePi. ComputePi takes a non-negative integer n and computes the sum that includes all values in the series above larger than 0.1 raised to the n-th power. For example, if n is zero, you should estimate pi as 4 - 4/3, since 4/5 is the first number in the series less than 1 (0.1 to the 0 power = 1). If n is 1, the series would include all the numbers up to but not including 4/41, which is the first number in the series less than 0.1.

    You can use the Math.pow(y,x) function in Java's built-in Math class. This function takes two arguments of type double, y and x, and returns a double representing the value of y raised to the x power. For example, Math.pow(0.5,2.0) is 0.25 since 1/2 squared is 1/4.

    You should also write a method called main(String args[]) that can be used to test ComputePi. Recall that args will be initialized to the parameters passed to PiEstimator when it is called. That is, if you enter
    java PiEstimator 0 1 2
    then args will be an array containing three strings, "0", "1" and "2".

    You should run ComputePi on each value in args, converting it to an integer. You may assume that only non-negative integers are supplied on the command line. For example, your program should print three different estimates of pi if you run the following command after you compile it:
    java PiEstimator 0 1 2

    For this assignment, don't worry about making the program interface very robust -- that is, it's ok if it does strange things when you input a floating point argument. This will not be considered acceptable program behavior in a couple of weeks, but for now, it simplifies what you need to know about input and output (I/O). You may use the following code fragment to convert a string in args to an integer: Integer.parseInt(args[i]).

    At the top of the PiEstimator code, in comments, put your Name, login id, and the status of the code, where status of the code includes: 1) whether it compiles correctly, 2) the test inputs that it successfully handled, and 3) the test inputs that you tried that caused it to fail. (Note that you get points for thorough testing, so you should list any cases that didn't work. We'll be checking whether your code works on cases that you didn't try.)

    Handin your PiEstimator.java code electronically.

  2. Copy the file ~cs367-4/public/assignments/a2/SortVector.java to your own directory.

    The code we've provided is designed to sort an array using the selection sort technique. Selection sort works by repeatedly picking the smallest element in the unsorted part of the array and adding it to the end of the sorted part by swapping with whatever happens to immediately follow the end of the sorted part. Don't worry too much about the details of how this exactly works; we've programmed it for you!

    The method SelectionSort calls two functions that you need to program: MinBetween and Swap. The precondition (what is true before execution) and postcondition (what must be true after execution) for each of these functions is specified in the code provided. Note: the notation [low, high] means that low and high ARE included in the range of values specified.

    Your job is to fill in the body of these two functions so that the selection sort routine works. Note that main in SelectionSort tests the methods it contains. This is common practice in Java -- classes often contain extensive testing code (in main) to demonstrate the correctness of the methods they implement.

    Hand in in your completed SelectionSort.java code electronically after inserting comments at the top of the file that give your name, login id, and a summary of the status of your code similar to the status summary for problem #1.

  3. Using the List Class.

    Copy the file ~cs367-4/public/assignments/a2/List.java to your own directory.

    This file contains classes to implement a ListNode, an encapsulated List type, and some test code in the Test class. Compile List.java and run the resulting test code using:
    java Test

    Part 1

    Part 2

    Part 3

    Handin your modified List.java program electronically.

What to turn in: