Question

In: Computer Science

#4 You need to write a class MinMax with a method minmax that takes an array...

#4 You need to write a class MinMax with a method minmax that takes an array of integers as a parameter and returns min and max simultaneously (using one method and one call). (java oop)-> laboratory work

Solutions

Expert Solution

Compile using -> javac MinMax.java

Run using -> java MinMax

import java.util.*;
import java.io.*;
public class MinMax
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of array ");
n = sc.nextInt();
int A[] = new int[n];
System.out.println("Enter array's elements =>");
for(int i = 0; i < n; i++)
{
A[i] = sc.nextInt();
}
int[] ans = new int[2]; //to store the answers as min and max respectively at index 0 and 1
ans=minmax(A,n);
System.out.println("Max and min values are "+ans[0]+" ans "+ans[1]);
}
public static int[] minmax(int A[],int n)
{
int[] ans = new int[2];
int max=A[0];
int min=A[0];
for (int i=0;i<n;i++)
{
if(A[i]>max)
{
max=A[i];
}
if(A[i]<min)
{
min=A[i];
}
}
ans[0]=max;//store max at index 0 of ans[] array
ans[1]=min;//store min at index 1 of ans[] array
return ans;//return answer
}
}

Output -

Enter size of array 5
Enter array's elements =>
4 2 3 5 1
Max and min values are 5 ans 1


Related Solutions

Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
JavaScript Write out a function that takes in array of numbers. You do not need to...
JavaScript Write out a function that takes in array of numbers. You do not need to check if each item of the array is a number, assume this has already been done. Create a new array using the map function that that the original item and adds 4 to it. Then iterate through the new array and log each item to the console. If you do not use map it will be counted wrong. Call your function with the following...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
public class SumMinMaxArgs { private int[] array; // You will need to write the following: //...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: // // 1. A constructor that takes a reference to an array, // and initializes an instance variable with this // array reference // // 2. An instance method named sum, which will calculate // the sum of the elements in the array. If the array // is empty (contains no elements), then this will // will return 0. You will need a loop for...
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a...
Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call: isPalindrome(q) should...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT