Question

In: Computer Science

[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The...

[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The method finds and returns an array which contains the positive numbers. For example, if the method receives an array with the following elements: 0 3 -1 2 5 1 -5 2 -2 0 the method should return an array with the following elements: 3 2 5 1 2 In the main method, randomly generate an array of 10 random integers between -5 and 5, inclusive, invoke the method, and display the returned array. java program

Solutions

Expert Solution

JAVA PROGRAM

import java.util.*;
import java.util.Random;

public class Main
{
public static int[] fun(int arr[])
{
int j=0;
// runs the loop to find how many positive numers are there in array
for(int i =0;i<10;i++){
if(arr[i]>=0){
j++;
}
}
// declaration of array only how many positive numbers are there
int arr2[] = new int[j];
int k =0;
// runs the loop
for(int i =0;i<j;i++)
{
// condition finds the positive number
if(arr[i]>=0)
{
arr2[k] = arr[i];
k++;
}
}
// returning only positive numbers array
return arr2;
}
  
//main function
   public static void main(String[] args)
   {
   Random rand = new Random();
   // array declaration
   int arr[] = new int[10];
   int random_int ;
   //runs the loop
   for(int i=0;i<10;i++)
   {
   // Generates the random number from -5 to 5 inclusive
   arr[i] = (int)(Math.random() * (6 - (-5) + 1) + (-5));
   }
   //calling a function & passing the parameter, returned value is stored in arr3[] array
   int arr3[] = fun(arr);
   //printing the returned value only positive numbers
   for(int i=0;i<arr3.length;i++){
   System.out.print(arr3[i]+" ");
   }
  
   }
}

OUTPUT

THUMBS UP


Related Solutions

.. Write a method called findNums that takes a two-dimension array of integers as a parameter...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter and returns the number of times a two-digit number appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 The value returned would be 5 (there are 5 two-digit numbers in the array) public class Question2 {    public static void main(String args[]){      int arr[][] = {{10, 45,...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Write a RECURSIVE method that receives a string as a parameter. The method will return true...
Write a RECURSIVE method that receives a string as a parameter. The method will return true if the string received as a parameter is a palindrome and false if it is not. The method must not have any loops! In JAVA
In an application write a method filterStack that takes a stack of integers as a parameter...
In an application write a method filterStack that takes a stack of integers as a parameter and filters its elements (in a new Stack) in a way that places the even elements at the bottom and the odd ones at the top. The original stack should remain unchanged. You should use a queue (only one queue) as a temporary storage. Use stack and queue operations only to solve this problem. No need to write the main method. For example, if...
[15 marks] (NumbersAboveAve.java) Write a method that randomly generates n integers between 0 and 100, and...
[15 marks] (NumbersAboveAve.java) Write a method that randomly generates n integers between 0 and 100, and stores them into an array, where n is a parameter of the method. The method returns all the numbers above the average of the n random numbers. Use loops wherever possible. For example, if the 10 random numbers are 44 61 98 45 45 17 63 24 9 95 The method should returns an array which contains 61 98 63 95 In the main...
Write a method that takes an array of integers as input. The method should find and...
Write a method that takes an array of integers as input. The method should find and display two indices m and n such that if you sort the elements from index m to index n the entire array would be sorted. The method should minimize m − n, that is it should find the smallest subsection for the array that needs to be sorted. For example, int[] a = {2, 4, 6, 7, 9, 8, 12, 15, 5, 13, 18,...
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Write a method called printIsMultiple that receives a pair of integers and prints true if the...
Write a method called printIsMultiple that receives a pair of integers and prints true if the second is a multiple of the first; false, otherwise
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 recursive method to sum the values in an array of integers. Create a file...
Write a recursive method to sum the values in an array of integers. Create a file ArraySum.java and add the recursive method public int sumOfArray (). Use the driver class ArraySumDriver.java to populate your array and demonstrate that your method works. ////ArraySumDriver.java/////// public class ArraySumDriver { private final static int ARRAY_SIZE = 6; /** * @param args */ public static void main(String[] args) { int index = 0; Integer[] myArray = new Integer[ARRAY_SIZE]; ArraySum arraySum = new ArraySum(); myArray[index++] =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT