CSCI 201: Intro to Programming (Java)

Assignment 6.    Due: Wed, Nov 15

Please put your work in directory cs201 on your Linux account before 11:59pm of the day above.

  1. (25 points)   Write the following function that reads a two-dimensional array from file hw6_1.dat (create this file by yourself and add it to the project) and tests whether it has four consecutive numbers of the same value, either horizontally, vertically, or diagonally.

    public static boolean isConsecutiveFour(int[][] a)

    Your program should print True if the array contains four consecutive numbers with the same value. Otherwise, display False. Here are some examples of the true cases:

    Your program must work for any array, not just for the above examples.

    Name the project Hw6_1
    Solution

  2. (25 points)   Design a Java application to implement the following algorithm for sorting an array of integers. According to this algorithm, the sort is accomplished by repeatedly taking the next item from the array (pivot) and inserting it into the final array in its proper order with respect to the items already inserted.

    Example:

    Given the following array a for sorting, it is traversed just once, the position of the currently processing pivot is indicated by "^". In each step the marked pivot is inserted to its proper place in array b. The resulting sorted array is b.

        a: 14 5 7 12 8      b: (empty)
    
    step1: 14 5 7 12 8      b: 14
           ^
    
    step2: 14 5 7 12 8      b: 5 14
              ^
    
    step3: 14 5 7 12 8      b: 5 7 14
                ^
    
    step4: 14 5 7 12 8      b: 5 7 12 14
                  ^
    
    step5: 14 5 7 12 8      b: 5 7 8 12 14
                     ^
    

    Your program should contain method int[] inSort(int[]) that takes the original array (array a in the above example) as a parameter and returns the sorted array (array b in the above example). The original and sorted arrays must be printed out.

    Name the project Hw6_2
    Solution

  3. (25 points)   Design a class named MyInteger. The class must include the following features:

    Implement the class. Write another class with main() method that would ask the user to enter an integer, instantiate class MyInteger based on that number, and test all methods in MyInteger class.

    Name the project Hw6_3
    Solution

  4. (25 points)   Define the Circle2D class that contains:

    Implement the class. Write a test program that creates a Circle2D object c1 (new Circle2D(2, 2, 5.5)), displays its area and perimeter, and displays the result of c1.contains(3, 3), c1.contains(new Circle2D(4, 5, 10.5)), and c1.overlaps(new Circle2D(3, 5, 2.3)).

    Name the project Hw6_4
    Solution