Create a method that takes an array of integerscalled data and an integer value and uses aloop to determine if value is in the array. The methodshould return true if value is in the arrayand false if it is not in the array. Your code shouldwork for an array of any length, although you may assume that ithas at least one element.
Consider the following class constant, which is an arraycontaining strings representing abbreviations of theschools/divisions at Harvard:
Create a method named schoolNumber that takes as aparameter a single String representing a schoolabbreviation and returns the index of that school abbreviation inthe SCHOOL_ABBREVS array. If the string passed in as aparameter does not appear in the array, the method should return-1. For example:
Your method should use a for loop to traversethe SCHOOL_ABBREVS array looking for a match for theabbreviation passed in as a parameter. If it finds a match, itshould return the index of that abbreviation. If it never finds amatch, it should return -1. Youare NOT allowed to use any helpermethods from the Arrays class to implement this method.Don’t forget that you need to use the equals method todetermine if two strings are equal. Also, note that youdo not need to pass inthe SCHOOL_ABBREVS array as a parameter, because it is aclass constant that is accessible in any method.
Complete the following method to make it negate all of theelements of the array data. For example,if data refers to the array {1, -2, -5, 4, -9},calling negate(data) should change the array tobe {-1, 2, 5, -4, 9}. Use as many lines as are needed toperform this task. Your code should work for an array of anylength.
Note that the method has a return type of void. That isbecause the method is passed a copy ofa reference to the array, not a copy of thearray itself, and thus any changes that the method makes to thearray will still be visible after the method returns.
Create a method that takes an array of integers called data and an integer value and uses a loop to determine if value i
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am