1 Finding the smallest element Write a method that finds the

1). (Finding the smallest element) Write a method that finds the smallest element in an array of integers. Use {1, 2, 4, 5, 10, 100, 2, –22} to test the method.

2). (Increasing array size) Once an array is created, its size is fixed. Occasionally, you need to add more values to an array, but it is full. In such cases, you can create a new, larger array to replace the existing array. Write a method with the following header: public static int[] doubleCapacity(int[] list). The method returns a new array that doubles the size of the parameter list.

Solution

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;


public class Program {
  
public static int findSmallest(int[] arr)
{
int len = arr.length;
int smallest = arr[0];
  
for(int i=1;i<len;i++)
if(arr[i]<smallest)
smallest = arr[i];
  
return smallest;
}
  
public static int[] doubleCapacity(int[] arr)
{
int len = arr.length;
int[] doubleSizeArr = new int[2*len];
  
for(int i=0;i<len;i++)
doubleSizeArr[i] = arr[i];
  
return doubleSizeArr;
}
  
public static void main(String[] args)
{
int[] arr = {1,2,4,5,10,100,2,-22};
int smallest = findSmallest(arr);
System.out.println(\"Smallest is \"+smallest);
}
}

1). (Finding the smallest element) Write a method that finds the smallest element in an array of integers. Use {1, 2, 4, 5, 10, 100, 2, –22} to test the method.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site