Question

LAB: How many negative numbers Write a program that generates a list of integers, and outputs...

LAB: How many negative numbers

Write a program that generates a list of integers, and outputs those integers, and then prints the number of negative elements. Create a method (static void fillArray(int [] array)) that uses Random to generate 50 values (Random.nextInt()), put each value in the array (make sure to pass the array in to this method as an argument). Write another method

(static int printAndCountNeg(int [] array)) that prints each element’s value and count the number of negative elements in the array. This method should return the number of negative elements found. The main method should declare an array of integers of size 50, call the method fillArray and pass it the array. When this method returns, main should then call the method printAndCountneg and save the return value (number of negative values). The main method should then print out the

number of negative values in the array. Ex:
the output could be :

-374937618 -1863905254 -1348969776 938596197 907952957 -327993586 -

1743601907 382736921 1712532071 642907127 1002963315 -1688774920 322796671

1755144485 -1052711727 1180372010 -70992450 2066690368 -1136894786 -

909934548 715799702 -594362157 -567571763 382028023 -1240992762 -

1080900594 1649662718 -521690784 -1448427729 1571306695 -1265503370 -

732942143 2112074280 1599849666 -1171297422 -602855207 2077600781

1157281841 -722961031 -375333392 -1757317538 1075184765 6170833 2003844415

321127813 1498980166 -855345613 -536361587 262123192 -627054168

There are 26 negative elements

The number should be different each time the program is run and the number of negative values is

usually is between 20-30 (25 +/- 5).

import java.util.Scanner;

public class howManyNegative {
public static void main(String[] args) {

/* Add variables as needed */

/* Type your code here. */ }

/* Add methods as needed */ }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA CODE:

import java.util.Scanner;
import java.util.Random;
public class howManyNegative {
    public static void fillArray(int[] array){
        // Create Random object to get random integers
        Random rand = new Random();
        for(int i = 0; i < array.length; i++){
            // Generate a random number and fill in the array
            array[i] = rand.nextInt();
        }
    }
    public static int printAndCountNeg(int[] array){
        // Counter to count negative numbers
        int count = 0;
        for(int i = 0; i < array.length; i++){
            // Print the i-th array element
            System.out.print(array[i] + " ");
            // If i-th array element is negative then increment the count
            if(array[i] < 0)
                count++;
        }
        System.out.println();
        // Return number of negative elements in the array
        return count;
    }
    public static void main(String[] args) {
        // Create an array of 50 integers
        int[] array = new int[50];
        // Fill array with random numbers
        fillArray(array);
        // Print array elements and get negative numbers
        int negNum = printAndCountNeg(array);
        System.out.println("There are " + negNum + " negative elements");
    }
}

SAMPLE OUTPUT:

-324239772 -1067735606 1499012173 1481621370_889490388 -282872537 810663049 1562000466 -15 52013162 1788526429 2097402977 -52

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
LAB: How many negative numbers Write a program that generates a list of integers, and outputs...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • Write a program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • For the following C# program: Let’s add one more user defined method to the program. This...

    For the following C# program: Let’s add one more user defined method to the program. This method, called ReverseDump is to output the values in the array in revere order (please note that you are NOT to use the Array.Reverse method from the Array class). The approach is quite simple: your ReverseDump method will look very similar to the Dump method with the exception that the for loop will count backwards from num 1 to 0 . The method header...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT