Question

1) Create a Method To find the maximum value, use the following array and search through...

1) Create a Method To find the maximum value, use the following array and search through it to find the max value like we did in a previous assignment. This Method must return the Max Value to the calling method.

int[] findMax = {2, 5, 3, 9, 11, 8}

2) Write a second method that accepts the maximum number from the method in #1 above as an argument and prints each number from 1 up to that maximum, inclusive, enclosed in square brackets. For example, if you passed 5 as an argument to the method, it would display:

[1] [2] [3] [4] [5]

You may name this method printNumbers.

Assume that any value passed to the method will be at least 1.

3) In your Main method you only need to call method1, which in turn calls method2.

Example:

public static void main(String[] args)

{ method1;}

Submit:

1) .java source file with your name

2) Screenshot of output

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

public class Main
{
   static int maxval(int findMax[]) // function to find the maximum value
   {
   int max=findMax[0]; //assigning first element in the array as maximum value
   for(int i=1;i<findMax.length;i++)
   {
   if(max<findMax[i]) //if any element is greater than the current maximum value then assign max value as that element
   max=findMax[i];
   }
   printNumbers(max); //passing the max value to printNumbers functions
   return max; // return max value
   }
   static void printNumbers(int n)
   {
   for(int i=1;i<=n;i++)
   System.out.print("["+i+"]"+" "); // printing numbers till max value
   System.out.println();
   }
   public static void main(String[] args)
   {
   int findMax[]={2, 5, 3, 9, 11, 8};
       int max=maxval(findMax); //calling maxval function
       System.out.println("Maximum Value: "+max); // printing maximum value
   }
}

Screen shot of the code:

Output:

Add a comment
Know the answer?
Add Answer to:
1) Create a Method To find the maximum value, use the following array and search through...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 11) Write a method (including header and body) that takes as an input an array of...

    11) Write a method (including header and body) that takes as an input an array of doubles and f three doubles containing the average, the maximum and the minimunm values of the input array. Test the program by calling the method from the main method. For example Input: 1 2 3 45 Output: 3 5 1 12) Write a Java method that takes a string as an argument and returns the reverse the string. Test the program by calling the...

  • Write a class named Month. The class should have an int field named monthNumber that holds...

    Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. For example. January would be 1. February would be 2. and so forth. In addition, provide the following methods A no-arg constructor that sets the monthNumber field to 1 A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value less than...

  • C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...

    C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise a number to a power. The function should accept two arguments, the number to be raised and the exponent. Assume that the exponent is a non-negative integer. String Reverser function that accepts a string object as its argument and prints the string in reverse order. Sum of Numbers this function accepts an integer argument and returns the sum of all the integers from 1...

  • Programming 4_1: Create a class call CellPhone. This class represents a cell phone. The data a...

    Programming 4_1: Create a class call CellPhone. This class represents a cell phone. The data a cellphone should contains: manufact- the name of the phone’s manufacturer; model – the phone’s model number; retailPrice – the phone’s retail price. The class will have the following methods: 1)A constructor that accepts arguments for manufacturer, model number, and retail price. 2)A setManufact method that accepts an argument for the manufacturer. This method will allow us to change the value of manufact field after...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • WRITTEN IN JAVA Complete the method printIndices that accepts a String source and a character search,...

    WRITTEN IN JAVA Complete the method printIndices that accepts a String source and a character search, and prints the position of each occurrence of the search value in source. For example: Input: source = "String Programming in Java is fun", search = 'i' Console Output: 3 15 19 27 Template provided from question:    public static int printIndices(String source, char search)    {    }    public static void main(String[] args)    {        System.out.println("Expected printIndices() prints indices 3...

  • Create 3 user-defined methods called add(). 1 accepts a single integer array parameter; this method should...

    Create 3 user-defined methods called add(). 1 accepts a single integer array parameter; this method should loop the array adding the values and return the summation (use 4, 1, 4, 6, 10, 15, 32, 79, 18 as the values in the array) 1 accepts 2 integer parameters; this method should add them and return the value 1 accepts 3 integer parameters; this method should add them all and return the value In main, define the parameters above (you may use...

  • Create in C# Write a method called MaximumDiffrence that accepts an integer array as a parameter...

    Create in C# Write a method called MaximumDiffrence that accepts an integer array as a parameter and return the maximum difference between adjacent values in the array, where the gap is defined as the absolute value of the difference between the 2 adjacent values. Example: if the array contains {5, 7, 4, 9, 6, 12, 8} so The first gap is (5,7)=-2 and the absolute value is 2 The second gap is (7,4)=3 Third gap is (4,9) = -5 its...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • 7.9.1: Find 2D array max and min. Find the maximum value and minimum value in milesTracker....

    7.9.1: Find 2D array max and min. Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program: Min miles: -10 Max miles: 40 import java.util.Scanner; public class ArraysKeyValue { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_ROWS = 2; final int NUM_COLS = 2; int [][] milesTracker = new int[NUM_ROWS][NUM_COLS]; int i; int j; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT