Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a) Fill in the blanks below to create a method that looks through the array data

ID: 3632774 • Letter: A

Question

a) Fill in the blanks below to create a method that looks through the array data for the specified value and returns true if value is in the array and false if it is not in the array. Your code should work for an array of any length, although you may assume that it has at least one element. (3 points)
public static ____________ contains(int[] data, int value) {
for (int i = __________; i < ________________; i++) {
if (_________________) {
return _______;
}
}
return _______;
}

b) Consider the following class constant, which is an array containing strings representing abbreviations of schools at BU:
public static final String[] SCHOOL_ABBREVS = {"CAS", "CFA", "CGS",
"COM", "ENG", "GMS", "GRS", "GSM", "LAW", "MED", "MET", "SAR", "SED",
"SHA", "SMG", "SPH", "SSW", "STH"};
Create a method named schoolNumber that takes as a parameter a single String representing a school abbreviation and returns the index of that school abbreviation in the SCHOOL_ABBREVS array. If the string passed in as a parameter does not appear in the array, the method should return -1. For example:

schoolNumber("CAS") should return 0, because "CAS" has an index of 0 in the array.
schoolNumber("ENG") should return 4, because "ENG" has an index of 4 in the array.
schoolNumber("FOO") should return -1, because "FOO" does not appear in the array.
Your method should use a for loop to traverse the SCHOOL_ABBREVS array looking for a match for the abbreviation passed in as a parameter. If it finds a match, it should return the index of that abbreviation. If it never finds a match, it should return -1. Don't forget that you need to use the equals method to determine if two strings are equal.

(Note: A method like this one could be used in the context of a program that processes a dataset of information about students from BU and computes summary statistics (e.g., totals or averages) for each school. Given a student's school abbreviation, the schoolNumber() method could be used to determine the school's number, and that number could be used as an index into the arrays used to maintain counts and totals for each school.)

c) Complete the following method to make it negate all of the elements of the array data. For example, if data refers to the array {1, -2, -5, 4, -9}, calling negate(data) should change the array to be {-1, 2, 5, -4, 9}. Use as many lines as are needed to perform this task. Your code should work for an array of any length.
public static void negate(int[] data) {

}
Note that the method has a return type of void. That is because the method is passed a copy of a reference to the array, not a copy of the array itself, and thus any changes that the method makes to the array will still be visible after the method returns.

Explanation / Answer

please rate - thanks

a) Fill in the blanks below to create a method that looks through the array data for the specified value and returns true if value is in the array and false if it is not in the array. Your code should work for an array of any length, although you may assume that it has at least one element. (3 points)
public static ____boolean________ contains(int[] data, int value) {
for (int i = ___0_______; i < _____data.length___________; i++) {
if (____data[i]==value_____________) {
return __true_____;
}
}
return _false______;
}

b) Consider the following class constant, which is an array containing strings representing abbreviations of schools at BU:
public static final String[] SCHOOL_ABBREVS = {"CAS", "CFA", "CGS",
"COM", "ENG", "GMS", "GRS", "GSM", "LAW", "MED", "MET", "SAR", "SED",
"SHA", "SMG", "SPH", "SSW", "STH"};
Create a method named schoolNumber that takes as a parameter a single String representing a school abbreviation and returns the index of that school abbreviation in the SCHOOL_ABBREVS array. If the string passed in as a parameter does not appear in the array, the method should return -1. For example:

schoolNumber("CAS") should return 0, because "CAS" has an index of 0 in the array.
schoolNumber("ENG") should return 4, because "ENG" has an index of 4 in the array.
schoolNumber("FOO") should return -1, because "FOO" does not appear in the array.
Your method should use a for loop to traverse the SCHOOL_ABBREVS array looking for a match for the abbreviation passed in as a parameter. If it finds a match, it should return the index of that abbreviation. If it never finds a match, it should return -1. Don't forget that you need to use the equals method to determine if two strings are equal.

public static int schoolNumber(String school )
{for(int i=0;i<SCHOOL_ABBREVS.length;i++)
{if (SCHOOL_ABBREVS[i].compareTo(school)==0)
     {
return i;
     }
}

(Note: A method like this one could be used in the context of a program that processes a dataset of information about students from BU and computes summary statistics (e.g., totals or averages) for each school. Given a student's school abbreviation, the schoolNumber() method could be used to determine the school's number, and that number could be used as an index into the arrays used to maintain counts and totals for each school.)

c) Complete the following method to make it negate all of the elements of the array data. For example, if data refers to the array {1, -2, -5, 4, -9}, calling negate(data) should change the array to be {-1, 2, 5, -4, 9}. Use as many lines as are needed to perform this task. Your code should work for an array of any length.
public static void negate(int[] data) {

}
Note that the method has a return type of void. That is because the method is passed a copy of a reference to the array, not a copy of the array itself, and thus any changes that the method makes to the array will still be visible after the method returns.

public static void negate(int[] data) {
int i;
for(i=0;i<data.length;i++)
      data[i]*=-1;

}