I\'m in intro to Java Write a method that finds the largest element in an array
ID: 3700633 • Letter: I
Question
I'm in intro to Java
Write a method that finds the largest element in an array of integer values using the following header: public static int madMax(int[] array) have your main method prompt the user to enter up to 10 integer values( hint the array size) terminate with negative sentinel value. Call the madMax method using(numbArray); Use printf for all printing. Sample program interaction: Enter up to 10 integer numbers; terminate input with a negative number: 0 5 20 100 5 -1 The largest integer that you entered was 100.
Explanation / Answer
MadMaxTest.java
import java.util.Scanner;
public class MadMaxTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int numbArray[]= new int[10];
int i=0;
System.out.println("Enter up to 10 integer numbers;");
int n = scan.nextInt();
while(i<10 && n!=-1) {
numbArray[i]=n;
n = scan.nextInt();
i++;
}
System.out.println("The largest integer that you entered was "+madMax(numbArray));
}
public static int madMax(int[] array) {
int max = array[0];
for(int i=0;i<array.length;i++) {
if(max<array[i])
max = array[i];
}
return max;
}
}
Output:
Enter up to 10 integer numbers;
0 5 20 100 5 -1
The largest integer that you entered was 100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.