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...
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 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