CS 307Assignment, Topic Arrays and 2
Dimensional Arrays
| Programming Assignment 2 | Individual Assignment. You must complete this assignment on
your own. You may not discuss their work with anyone except the instructor
and other members of the instructional staff (TA, section leader, or lab
proctor). You may not acquire from any source (e.g., another student or an
internet site) a partial or complete solution to a problem or project that
has been assigned. You may not show another student your
solution to an assignment. You may not have another person
(current student, former student, tutor, friend, anyone) “walk you through”
how to solve an assignment. Review the
class policy on collaboration
from the syllabus. Placed online: Friday, September 5 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Description | The purpose of this assignment is:
Complete the 8 methods in ArrayProblems.java. The methods are explained in further detail in the Tips section of this page. When completing the methods you are encouraged to break the problem up into smaller problems and create helper methods of your own to solve these smaller problems. VERY IMPORTANT: For this assignment the only methods and classes from the Java standard library you may use in your final solutions are:
Note all the methods require that the parameter not be altered. ArrayProblems.java includes a main method with some tests for the methods. Some of these tests may be incorrect. You are required to find and fix any incorrect tests. Use the class listserv to share information about other students about which tests are in error and how to correct them. Your methods must pass the corrected tests. Add many more tests to the main method of ArrayProblems.java. You are encouraged to send your tests to the class listserv for other students to use. Fill in the header for ArrayProblems.java. Replace <NAME> with your name. Note, you are stating, on your honor, that you did the assignment on your own, as required. When finished turn in your ArrayProblems.java program using the turnin program. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Files |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Checklist | Did you remember to:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Tips |
1. Hamming Distance. "Hamming distance between two strings of equal length is the number of positions for which the corresponding symbols are different. Put another way, it measures the number of substitutions required to change one into the other, or the number of errors that transformed one string into the other." From the Wikipedia article on Hamming Distance. For this problem you will be working with arrays of ints instead of String objects. Complete the following method in ArrayProblems.java
For example given the array {1, 2, 3, 4, 5, 4, 3, 2, 1} and the array {1, 2, 2, 4, 5, 4, 3, 1, 1} the Hamming distance is 2. 2. Permutations. This method determines if one int array is a permutation of another int array. /* "A permutation, also called an "arrangement number" or "order," is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself." [mathworld.wolfram.com] For example the list {1, 2} has the following permutations; {1, 2} and {2, 1}. Note the elements of listA and listB are lists, not sets, so duplicate items could appear. So for example given the list {1, 2, 2} the unique permutations are {1, 2, 2}, {2, 1, 2}, and {2, 2, 1}. {2, 1} is not a permutation of {1, 2, 2}. Another example of lists that are not permutations of each other: {2, 1, 1} is not a permutation of {2, 2, 1}. Hint: Do not try to solve the problem by taking one the arrays and generating all the permutations of that array and then check to see if the other array is one of those permutations. That is much too inefficient except for arrays with a very small number of items. 3. Majority element. This method determines if an array of ints contains a majority element.
A majority element in an array A of size N is an element that appears more than N/2 times. Thus there is at most one such element. For example the array index 0 1
2 3 4 5 6 7 has a majority element equal to 4. Note your method returns the index of the first occurrence of the majority element if one exists. If there is no majority element your method returns -1. The following array does not have a majority element: index 0 1
2 3 4 5 6 7 To be a majority element it must appear more than N/2 times where N is equal to the list. With a list of length 8 there must be at least 5 elements equal to one another for there to be a majority element. Your method shall return -1 if an array does not contain a majority element. 4. Nearest Neighbor. Given an array of ints, find the indices of the pair of integers that have the smallest difference between them. /** Find the two values
in an array that are closest to For example, consider the following array: {19, 0, -5, 4, 7, 10} Here are the distances (absolute
value of the difference) between the various pairs of elements.
Given the above values there are two pairs of ints that have a
distance of 3 apart. The pairs are 4 and 7, and 7 and 10. The method
would return an array with the indices 3 and 4 which are the indices of
4 and 7 in the array named 5. String with the most vowels. That determines which String in an array of Strings contains the most vowels. For this method vowels are defined to 'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', and 'u'. The method is not trying to determine which String has the largest number of distinct vowels. Thus "aaaaaa" has more vowels that "aeiou". "aaaaaa" has 6 vowels while the String "aeiou" only has 5 vowels. You can use whatever String methods you want when completing this method.
6. Max Sum. Complete a method that determines which row or column in a 2d array of ints has the largest sum.
7. Are the queens Safe? There is a chess and programming problem called the 8 queens problem. The goal is to place eight queens on a chess board so that none of them may attack any other queen. That is, no two queens are in the same row, column, or diagonal. In chess a queen may move any number of spaces straight up, down, left, right, or along any of the 4 diagonals. In the method you are completing the board is square (same number of rows as columns) but is not necessarily 8 by 8. Consider the following board:
A queen's position is designated with the Q. The red arrows show the squares that queen can attack. Thus if there were a queen in any of those squares this would be an unsafe board. So the following set up is unsafe.
The following set up is safe, but the number of other safe squares is going down fast. .. Complete a method that checks if a given board represents a safe placement of Queens. /* 8. Skyline. (Based on a problem developed by James Goodwin.) Given a 2 dimensional array of ints that represents the height of the buildings in a downtown area create and return a String that shows what the what the downtown area would like when viewed from the West towards the East. The top, left corner of the 2d array of ints that contains the height of the buildings is the Northwest corner of the area. Instead of using true perspective (where buildings would look smaller in the distance), show a simple projection of the buildings. Each visible building should be represented with an integer from 1 to 9 (inclusive) indicating the distance of the building from the observer, with 1 being the closest possible building (i.e., one that's in the leftmost column of the overhead view). The 2d array of ints that contains the heights of the building will have between 1 and 9 columns inclusive. All elements of the 2d array will be between 0 and 9 inclusive. The output view should always be as high as the tallest building, with periods used to fill in space above buildings that are not as tall as the tallest building.
Here is an example. Consider the following 2d array of ints of the heights of
the buildings.
Recall that the character for a building is its distance from the viewer. Thus all buildings in the left most column will be represented with 1s, all buildings in the next columns will be represented with 2s and so forth. The resulting String would be:
|