Write a method to compute and return the value of max - min where max is the lar
ID: 3762076 • Letter: W
Question
Write a method to compute and return the value of max - min where max is the largest
element of an int array and min is the smallest element of an int array. The method is passed the
int array and an int value that denotes the number of elements in the array. You may assume that
the int array has at least 1 element in it.
5.
Rewrite the following nested if-else statement as an equivalent switch statement.
if (letter == 'A' | | letter == 'a') System.out.println("Excellent");
else if (letter == 'B' | | letter == 'b') System.out.println("You can do better");
else if (letter == 'C' | | letter == 'c') System.out.println("Try harder");
else if (letter == 'D' | | letter == 'd') System.out.println("Try much harder");
else System.out.println("Try another major! ");
Write a method to compute and return the value of max - min where max is the largest
element of an int array and min is the smallest element of an int array. The method is passed the
int array and an int value that denotes the number of elements in the array. You may assume that
the int array has at least 1 element in it.
5.
Rewrite the following nested if-else statement as an equivalent switch statement.
if (letter == 'A' | | letter == 'a') System.out.println("Excellent");
else if (letter == 'B' | | letter == 'b') System.out.println("You can do better");
else if (letter == 'C' | | letter == 'c') System.out.println("Try harder");
else if (letter == 'D' | | letter == 'd') System.out.println("Try much harder");
else System.out.println("Try another major! ");
Explanation / Answer
public static int MaxMinDifference(int a[],int size){
int min = Integer.MAX;
int max = Integer.MIN;
for(int i = 0; i < size; i++){
if(a[i] > max)
max = a[i];
if(a[i] < min)
min = a[i];
}
return max - min;
}
switch(ch){
case ‘A’:
case ‘a’:
System.out.println("Excellent");
break;
case ‘B’:
case ‘b’:
System.out.println("You can do better");
break;
case ‘C’:
case ‘c’:
System.out.println("Try harder");
break;
case ‘D’:
case ‘d’:
System.out.println("Try much harder");
break;
case default:
System.out.println("Try another major! ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.