PLEASE CODE IN JAVA!!!!! NOT C OR JAVA SCRIPT need asap! please do all and make
ID: 3701329 • Letter: P
Question
PLEASE CODE IN JAVA!!!!! NOT C OR JAVA SCRIPT need asap! please do all and make sure they work!!
public class ArrayPractice {
/* sets every item in A[] to initialValue */
public static void initialize(int A[], int initialValue) {
}
/* returns the average of the items in A
* Be careful: A[] is an array of int and the method returns
* double. What do we do to handle this? */
public static double average(int A[]) {
return 0.0;
}
/* returns the number of times that x appears in A[] */
public static int numOccurrences(int A[], int x) {
return 0;
}
/* returns the index of the first occurrence of
* x in A[] or -1 if x doesn't exist in A[] */
public static int find(int A[], int x) {
return -1;
}
/* returns the index of the last occurrence of
* x in A[] or -1 if x doesn't exist in A[] */
public static int findLast(int A[], int x) {
return -1;
}
/* returns the smallest item found in A */
public static int smallest(int A[]) {
return 0;
}
/* returns the index of the smallest item found in A */
public static int indexOfSmallest(int A[]) {
return 0;
}
/* returns the index of the smallest odd number
* in A[] or -1 if A[] contains no odd numbers */
public static int indexOfSmallestOdd(int A[]) {
return -1;
}
/* removes the element in A[] at the given index,
* by shifting all remaining elements to the left
* if necessary and filling in the right-hand
* side with 0s.
*
* returns the element removed.
*
* For example, if we call remove(A, 1)
* on the array:
*
* |---+---+----+----+-----+-----+-----+---+---+---|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
* |---+---+----+----+-----+-----+-----+---+---+---|
* | 5 | 9 | 32 | 97 | 101 | 142 | 157 | 0 | 0 | 0 |
* |---+---+----+----+-----+-----+-----+---+---+---|
*
* the array becomes:
*
* |---+----+----+-----+-----+-----+---+---+---+---|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
* |---+----+----+-----+-----+-----+---+---+---+---|
* | 5 | 32 | 97 | 101 | 142 | 157 | 0 | 0 | 0 | 0 |
* |---+----+----+-----+-----+-----+---+---+---+---|
*
*
* and we return the value 9.
*
* It is up to the caller to make sure that the
* index passed to the function is valid.
* */
public static int remove(int A[], int index) {
return 0;
}
/* returns a new array consisting of all of the
* elements of A[] */
public static int[] copy(int A[]) {
return null;
}
/* Returns a new array consisting of all of the
* elements from index s (for "start") inclusive
* and index e (for "end") exclusive
*
* if s>e, return null
*
* (to do this, the syntax is really just "return null")
*
* for example, if the array is:
* |---+----+----+----+----+----|
* | 0 | 1 | 2 | 3 | 4 | 5 |
* |---+----+----+----+----+----|
* | 0 | 11 | 22 | 33 | 44 | 55 |
* |---+----+----+----+----+----|
*
* with s=2 and e=5, the function returns:
* |----+----+----|
* | 0 | 3 | 4 |
* |----+----+----|
* | 22 | 33 | 44 |
* |----+----+----|
*
*/
public static int[] chunk(int A[], int s, int e) {
return null;
}
/* returns a new array consisting of the same elements in
* A[] repeated numTimes
*
* for example, if A[] refers to the array:
*
* |----+----+----|
* | 0 | 1 | 2 |
* |----+----+----|
* | 11 | 22 | 33 |
* |----+----+----|
*
* and numTimes is 3, the function returns:
*
* |----+----+----+----+----+----+----+----+----|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* |----+----+----+----+----+----+----+----+----|
* | 11 | 11 | 11 | 22 | 22 | 22 | 33 | 33 | 33 |
* |----+----+----+----+----+----+----+----+----|
*
*
* if numTimes <=1, the method returns a copy of A[]
* */
public static int[] repeat(int A[], int numTimes) {
return null;
}
/* returns a new array consisting of all of the
* elements of A[] followed by all of the
* elements of B[]. For example, if
A[] is: {10,20,30} and
B[] is: {5, 9, 38}, the method returns the
array : {10,20,30,5,9,38} */
public static int[] copyAll(int A[], int B[]) {
return null;
}
/* reverses the order of the elements in A[].
* For example, if A[] is:
{10,20,30,40,50}, after the method, A[] would
be {50,40,30,20,10} */
public static void reverse(int A[]) {
}
/* reverses the ordering of the elements of each
* row of the two-dimensionl array to which A refers.
*
* For example, if A refers to the array:
*
*
* |----+----+----|
* | 10 | 20 | 30 |
* |----+----+----+----|
* | 40 | 50 | 60 | 70 |
* |----+----+----+----|
* | 80 | 90 |
* |----+----+
*
*
* the method returns:
*
* |----+----+----+
* | 30 | 20 | 10 |
* |----+----+----+----|
* | 70 | 60 | 50 | 40 |
* |----+----+----+----|
* | 90 | 80 |
* |----+----+
*
* */
public static void reverseAll(int A[][]) {
}
/* scores contains a collection of quiz grades ranging
* from 0 to maxScore inclusive. The method returns
* a new array where the value at index i is the number
* of times that grade i appeared in scores.
*
* For example, if scores refers to the array:
* |---+---+---+----+---+---+---+---+---+---+----+----+----+----+----|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
* |---+---+---+----+---+---+---+---+---+---+----+----+----+----+----|
* | 9 | 3 | 8 | 10 | 7 | 9 | 8 | 9 | 7 | 6 | 8 | 7 | 8 | 9 | 6 |
* |---+---+---+----+---+---+---+---+---+---+----+----+----+----+----|
*
* The method returns the array:
* |---+---+---+---+---+---+---+---+---+---+----|
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
* |---+---+---+---+---+---+---+---+---+---+----|
* | 0 | 0 | 0 | 1 | 0 | 0 | 2 | 3 | 4 | 4 | 1 |
* |---+---+---+---+---+---+---+---+---+---+----|
*
* */
public static int[] counts(int scores[], int maxScore) {
return null;
}
/* Extra credit:
*
* Returns a new array consisting of all of the
* elements of A, but with no duplicates. For example,
* if A[] is {10,20,5,32,5,10,9,32,8}, the method returns
* the array {10,20,5,32,9,8} */
public static int[] uniques(int A[]) {
return null;
}
}
Explanation / Answer
// -------------- ArrayPractice.java------------
public class ArrayPractice {
/* sets every item in A[] to initialValue */
public static void initialize(int A[], int initialValue) {
for(int i=0; i< A.length;i++) {
A[i] = initialValue;
}
}
/* returns the average of the items in A */
public static double average(int A[]) {
double average = 0.0;
for(int i=0; i< A.length;i++) {
average = (double) average + A[i];
}
average = average / A.length;
return average;
}
/* returns the number of times that x appears in A[] */
public static int numOccurrences(int A[], int x) {
int occurences = 0;
for(int i=0; i< A.length;i++) {
if(A[i] == x) {
occurences = occurences + 1;
}
}
return occurences;
}
/* returns the index of the first occurrence of
* x in A[] or -1 if x doesn't exist in A[] */
public static int find(int A[], int x) {
int firstOccurence = -1;
for(int i=0; i< A.length;i++) {
if(A[i] == x) {
firstOccurence = i;
break;
}
}
return firstOccurence;
}
/* returns the index of the last occurrence of
* x in A[] or -1 if x doesn't exist in A[] */
public static int findLast(int A[], int x) {
int lastOccurence = -1;
for(int i=0; i< A.length;i++) {
if(A[i] == x) {
lastOccurence = i;
}
}
return lastOccurence;
}
/* returns the smallest item found in A */
public static int smallest(int A[]) {
int smallestItem = A[0];
for(int i=1; i< A.length;i++) {
if(A[i] < smallestItem) {
smallestItem = A[i];
}
}
return smallestItem;
}
/* returns the index of the smallest item found in A */
public static int indexOfSmallest(int A[]) {
int smallestItemIndex = -1;
int smallestItem = A[0];
for(int i=1; i< A.length;i++) {
if(A[i] < smallestItem) {
smallestItem = A[i];
smallestItemIndex = i;
}
}
return smallestItemIndex;
}
/* returns the index of the smallest odd number
* in A[] or -1 if A[] contains no odd numbers */
public static int indexOfSmallestOdd(int A[]) {
int smallestOddNumberIndex = -1;
int smallestOddNumber = A[0];
for(int i=0; i< A.length;i++) {
if((A[i] % 2) == 1 && A[i] <= smallestOddNumber) {
smallestOddNumber = A[i];
smallestOddNumberIndex = i;
}
}
if(smallestOddNumber == A[0] && (A[0] % 2)!=1) {
smallestOddNumberIndex = -1;
}
return smallestOddNumberIndex;
}
/* removes the element in A[] at the given index,
* by shifting all remaining elements to the left
* if necessary and filling in the right-hand
* side with 0s.
*
* returns the element removed.
*
* It is up to the caller to make sure that the
* index passed to the function is valid.
* */
public static int remove(int A[], int index) {
int removedElement = A[index];
for(int i=index;i<A.length;i++) {
if(i == A.length - 1) {
A[i] = 0;
} else {
A[i] = A[i+1];
}
}
return removedElement;
}
/* returns a new array consisting of all of the
* elements of A[] */
public static int[] copy(int A[]) {
int[] copy = new int[A.length];
for(int i = 0;i<A.length;i++) {
copy[i] = A[i];
}
return copy;
}
/* Returns a new array consisting of all of the
* elements from index s (for "start") inclusive
* and index e (for "end") exclusive
*
* if s>e, return null
*
*/
public static int[] chunk(int A[], int s, int e) {
if(s > e) {
return null;
}
int newLength = e-s;
int[] copy = new int[newLength];
for(int i=s;i<e;i++) {
copy[i] = A[i];
}
return copy;
}
/* returns a new array consisting of the same elements in
* A[] repeated numTimes
*
* if numTimes <=1, the method returns a copy of A[]
* */
public static int[] repeat(int A[], int numTimes) {
int[] copy;
if(numTimes <= 1) {
copy = A.clone();
return copy;
}
int newLength = A.length * numTimes;
copy = new int[newLength];
for(int i=0;i<A.length;i++) {
for(int j=0;j<numTimes;j++) {
copy[i*numTimes+j] = A[i];
}
}
return copy;
}
/* returns a new array consisting of all of the
* elements of A[] followed by all of the
* elements of B[]. For example, if
* */
public static int[] copyAll(int A[], int B[]) {
int[] copy = new int[A.length + B.length];
for(int i=0;i<A.length;i++) {
copy[i] = A[i];
}
for(int j=A.length;j<A.length + B.length;j++) {
copy[A.length] = B[j];
}
return copy;
}
/* reverses the order of the elements in A[].*/
public static void reverse(int A[]) {
int[] reverseCopy = new int[A.length];
for(int i=0,j=A.length-1;i<A.length && j >= 0;i++,j--) {
reverseCopy[i] = A[j];
}
}
/* reverses the ordering of the elements of each
* row of the two-dimensionl array to which A refers.
* */
public static void reverseAll(int A[][]) {
int[][] reverseCopy = new int[A.length][];
int rows = A.length;
int cols = A[0].length;
for (int i = 0; i< rows; i++) {
cols = A[i].length;
for (int j = cols - 1; j >= 0; j--) {
reverseCopy[i][cols - 1 - j] = A[i][j];
}
}
}
/* scores contains a collection of quiz grades ranging
* from 0 to maxScore inclusive. The method returns
* a new array where the value at index i is the number
* of times that grade i appeared in scores.
*
* */
public static int[] counts(int scores[], int maxScore) {
int max = scores[0];
for(int i=1;i<scores.length;i++) {
if(scores[i] > max) {
max = scores[i];
}
}
int[] gradeOccurences = new int[max];
int count = 0;
for(int i=0;i<max;i++) {
count = 0;
for(int j =0;j<scores.length;j++) {
if(i == scores[j]){
count++;
}
}
gradeOccurences[i] = count;
}
return gradeOccurences;
}
/* Extra credit:
*
* Returns a new array consisting of all of the
* elements of A, but with no duplicates. For example,
* if A[] is {10,20,5,32,5,10,9,32,8}, the method returns
* the array {10,20,5,32,9,8} */
public static int[] uniques(int A[]) {
int[] uniques = new int[A.length];
int occurence = 0;
int count = 0;
for(int i=0;i<A.length;i++) {
occurence = 0;
for(int j=0;j<uniques.length;j++) {
if(A[i] == uniques[j]) {
occurence++;
break;
}
}
if(occurence == 0) {
uniques[count] = A[i];
count++;
}
}
return uniques;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.