Please help with java program. Please use basic programing methods. Develop a me
ID: 3820281 • Letter: P
Question
Please help with java program. Please use basic programing methods.
Develop a method called max that takes a parameter of an integer array and returns the largest value stored in the parameter. Develop a method called min that takes a parameter of an integer array and returns the smallest value stored in the parameter. Develop a method called maxMin that takes a parameter of an integer array and returns both the largest value and the smallest value in the parameter in an array of length two. Test your methods in a program and include a method that reads a list, terminated by -999, into an array. In total, your program will contain 5 methods, max, min, maxMin, the method that reads input, and main.
Explanation / Answer
MethodsTest.java
import java.util.ArrayList;
import java.util.Scanner;
public class MethodsTest {
public static void main(String[] args) {
int a[] = readArray();
int values[] = maxMin(a);
System.out.println("Max value is "+values[0]);
System.out.println("Min value is "+values[1]);
}
public static int[] readArray() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the list of integers: ");
int n = scan.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
while( n != -999){
list.add(n);
n = scan.nextInt();
}
int a[] = new int[list.size()];
for(int i=0; i<list.size(); i++){
a[i]=list.get(i);
}
return a;
}
public static int max (int array[]){
int maxValue = array[0];
for(int i=0; i<array.length; i++){
if(maxValue<array[i]){
maxValue = array[i];
}
}
return maxValue;
}
public static int min (int array[]){
int minValue = array[0];
for(int i=0; i<array.length; i++){
if(minValue>array[i]){
minValue = array[i];
}
}
return minValue;
}
public static int[] maxMin (int array[]){
int values[] = new int[2];
values[0]=max(array);
values[1]=min(array);
return values;
}
}
Output:
Enter the list of integers:
5
4
3
8
7
9
1
2
5
6
-999
Max value is 9
Min value is 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.