Question

In: Computer Science

5. Write a method to accept five int parameters and return the largest of the five.  ...

5. Write a method to accept five int parameters and return the largest of the five.  

      Example: largest(45, 13, 65, 19, 23) returns 65. Java Program (METHOD ONLY!!!)

Solutions

Expert Solution

Method 1:-  

public int max(int a,int b,int c,int d,int e)
{

return Math.max(a,Math.max(b,Math.max(c,Math.max(d,e))));

// OR

return Math.max(Math.max(Math.max(Math.max(a,b),c),d),e);

}

Method 2:-

public int largest(int a,int b,int c,int d,int e)
{
    if(a>b && a>c && a>d && a>e)
    result=a;
    else if(b>a && b>c && b>d && b>e)
    result=b;
     else if(c>a && c>b && c>d && c>e)
    result=c;
     else if(d>a && d>b && d>c && d>e)
    result=d;
    else result =e;
    return result;
}
kindly try any of the methods and please feel free to comment if you get any further help......please give a like if you find it needfull...


Related Solutions

Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if...
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if the 2nd string is a subsequence of the 1st string. If not, the method will return false. An empty string is a subsequence of every string. This is because all zero characters of the empty string will appear in the same relative order in any string This method must not contain any loops. In java
Which row has the largest sum? Write a method that takes a 2D int array and...
Which row has the largest sum? Write a method that takes a 2D int array and prints: of The index of the row that has the largest sum The sum of elements in that row java
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the follwing is true: public static void main(String[] args) { A System.out.println(test ( 7, 14, 23)) ; B System.out.println(test ( test ( 7,9, 14) , 23 ); C System.out.println( test ( 14, 23 ) ; D System.out.println(test(1,2,4), 14, 23)) ;
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...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT