In: Computer Science
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts)
FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers.
SECOND PART: Now write a class MaxValue that uses a MAIN METHOD to prompts a user to input a series of integers(same numbers/ints will be used in both parts.). THEN CALL THE MAX METHOD USED IN PART ONE TO RETURN THE HIGHEST VALUE/INTEGER INPUTTED. Please show the complete work and use comments and write neatly!
SOURCE CODE
PART 1
public static int Max(int[] arr){
int max = arr[0];
for(int i=1;i < arr.length;i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
PART 2
import java.util.Scanner;
public class Max_Number
{
public static int Max(int[] arr){
int max = arr[0];
for(int i=1;i < arr.length;i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
public static void main(String[] args)
{
int n, max;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int arr[] = new int[n];
System.out.println("Enter elements of array:");
for(int i = 0; i < n; i++)
{
arr[i] = s.nextInt();
}
max = Max(arr);
System.out.println("Maximum Value is: "+max);
}
}
OUTPUT SCREENSHOT
please give a upvote if u feel helpful.