Please help me get through these drills... int myMet hod(int[] array) Descriptio
ID: 3690637 • Letter: P
Question
Please help me get through these drills...
int myMet hod(int[] array) Description. Given an array of ints, iterate through it, and look only at the positive values; calculate the minimum of all of these values. IGNORE any negative or zero values in the array. /w write your code here 4 To make your code simple, you may assume that the first element in the array is positive. But you should check this assumption, and throw an exception if it isn't true. (See details below.) 6 7 Input: array - an array of ints 9 // please ignore everything below this line! 10 String minPos(int[] array) Error Handling: Throw an lllegalArgumentException if the array is empty. Throw an lllegalArgumentException if the first element of the array is not positive. 12 13 14 15 16 17 18 19 20 return "" +myMethod(array); catch(ILlegalArgumentException e) Output: The minimum of the positive values from the array return "IllegaLArgumentException"; Hint This is the same code as a simple find-the-min loop, except thatExplanation / Answer
Question 1:
int myMethod(int[] array) throws IllegalArgumentException
{
int min=99999999;
if(array[0]<0)
throw new IllegalArgumentException();
for(int i=0;i<array.length;i++)
{
if(array[i]<min)
min=array[i];
}
return min;
}
Question 2:
boolean factor3or5exclusive(int val)
{
if((val%3==0 && val%5!=0) || (val%3!=0 && val%5==0))
return true;
return false;
}
Question 3:
boolean hasNeg(int[] vals)
{
for(int i=0;i<vals.length;i++)
if(vals[i]<0)
return true;
return false;
}
Question 4:
double[] mymethod(double[] array,double val)
{
double result[]=array;
return result;
/*
for(int i=0;i<array.length;i++)
{
result[i]=array[i];
}
*/
result[result.length]=val;
}
Question 5:
int[] mymethod(int[] array)
{
for(int i=1;i<array.length;i++)
array[i]=array[i+1];
return array;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.