Question

In: Computer Science

write a program using the main method where it searches through an array of positive, non-zero...

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:

  • Does the solution compile?
  • Does the solution have your name in the comments?
  • Does the solution have a high-level solution description (150-300 words) in the comments?
  • If the array has the values {9,1,8,2,7,3,6,4,5} does the program print out:

Max Even: 8

Max Odd: 9

  • If the array has the values {1,2,3,4,5,6,7,8} does the program print out:

Max Even: 8

Max Odd: 7

  • If the array has the values {1,1,1,1,1} does the program print out:

Max Even: -1

Max Odd: 1

  • If the array has the values {2,4,6,8,10} does the program print out:

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
 * 
 */

Solutions

Expert Solution

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}:


Related Solutions

Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and returns a second vector that contains only the odd numbers. It will return zero if all elements are even. Use error-traps to check against probable errors in user input. In case of an error, it will return NaN. You are allowed to use Matlab built-in function round(). Check your code with the following arrays: >> y1 = [18, -5, 89, -7, 4, 10, 12,...
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
Using the provided code (found here), write a program using the main method where the user...
Using the provided code (found here), write a program using the main method where the user enters Strings and the program echoes these strings to the console until the user enters “quit”. When user quits the program should print out, “Goodbye”. You may assume that the case is ignored when the user enters, “quit”, so “quit”, “QUIT”, “Quit”,“qUiT”, etc. are all acceptable ways to end the program. The results should be printed out to the console in the following format:...
write a program that finds the average of all positive, non-zero values found in a given...
write a program that finds the average of all positive, non-zero values found in a given array of doubles and prints that to the console. Any negative or zero values do not contribute to the average. 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: “Average: ”<<average value>> Values denoted in “<< >>” represent...
write a program using the main method where the user enters a number greater than 0,...
write a program using the main method where the user enters a number greater than 0, and the program prints out a set of stairs. The stairs consist of 3 boxes stacked on top of each other where the length and width of the first box is the number, the length and width of the second box is the number plus 1, and the length and width of the third box is the number plus 2. The stairs should be...
Write a program in Python that walks through a folder tree and searches for files with...
Write a program in Python that walks through a folder tree and searches for files with a certain file extension (such as .pdf or .jpg). Copy these files from whatever location they are in to a new folder. The user can enter an absolute path for the start folder, or if the user does not enter a folder, the current directory is used. Likewise, the user can enter extensions to copy but if the user does not enter an extension,...
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
Write a program that contains a main method and another method named userName. The main method...
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue). Sample Output Please enter your FULL name Billy Joe McCallister Here is the name Billy Joe McCallister in reverse: retsillaCcM eoJ ylliB...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT