In: Computer Science
write a program using the main method where it searches through an array of positive, non-zero integer values and prints out the maximum even and odd values. The program should use the array provided in the code, and you may assume that it has already been populated with values. The results should be printed out to the console in the following format:
“Max Even: ”<<max even
value>>
“Max Odd: ”<<max odd value>>
Values denoted in “<< >>” represent variable values, and strings in quotations denote literal values (make sure to follow spelling, capitalization, punctuation, and spacing exactly). Also, if there are either no even or odd values then it should print out “-1” for those values.
Solution Tests:
Max Even: 8
Max Odd: 9
Max Even: 8
Max Odd: 7
Max Even: -1
Max Odd: 1
Max Even: 10
Max Odd: -1
PROVIDED CODE:
* Provided code. Do not alter the code that says "Do not alter" * Make sure this at least compiles (no syntax errors) * You may write additional methods to help */ //Do not alter----------------------------------------------------------------------- import java.util.Scanner; public class Question03 { public static int[] array;//The array to be used in the problem public static void main(String[] args) { int number;//Used for the stairs if(args == null || args.length == 0) { //----------------------------------------------------------------------------------- int[] tempArray ={2,4,6,8,10};//You may change these values to test your solution //Do not alter----------------------------------------------------------------------- array = tempArray; } else { } //----------------------------------------------------------------------------------- //Write your solution here }//Do not alter this //Space for other methods if necessary----------------------------------------------- //Write those here if necessary //----------------------------------------------------------------------------------- }//Do not alter this /*Solution Description * */
Explanation: I have modified the code as per the requirement. I have shown the output as the per the sample output.I have commented the output statement with <<>> if you want that you can uncomment it. I have also shown the output of the program for two scenarios and have also added the description, please find the images attached with the answer..Please upvote if you liked my answer and comment if you need any modification or explanation.
//code starts
public class Question03 {
public static int[] array;// The array to be used in the problem
public static void main(String[] args) {
int number;// Used for the
stairs
if (args == null || args.length ==
0) {
//
-----------------------------------------------------------------------------------
int[] tempArray
= {2, 4, 6, 8, 10};// You may change these values to
// test
your solution
// Do not
//
alter-----------------------------------------------------------------------
array =
tempArray;
} else {
}
//
-----------------------------------------------------------------------------------
// Write your solution here
int maxEven = -1, maxOdd =
-1;
for (int i = 0; i <
array.length; i++) {
if (array[i] % 2
== 0) {
if (array[i] > maxEven)
maxEven = array[i];
} else {
if (array[i] > maxOdd)
maxOdd = array[i];
}
}
System.out.println("Max Even: " +
maxEven);
System.out.println("Max Odd: " +
maxOdd);
// System.out.println("\"Max Even:
\"<<" + maxEven + ">>");
// System.out.println("\"Max Odd:
\"<<" + maxOdd + ">>");
/**
* The solution to the program
involves declaring two maxEven and maxOdd
* variables initialized to -1
initially and then iterating over the
* array and to find if a number is
greater than the current
* maxEven/maxOdd and assign that
number to the corresponding
* maxEven/maxOdd variable.Both the
values are then printed and if no
* maxEven/maxOdd is present -1 is
printed
*/
}
}
Output: For input array: {2, 4, 6, 8, 10}:
Output: For input array: {2, 4, 6, 8, 10}: