Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a method named getNums: Write a method named getNums which will fill an array with positive integers (including zero). The array which is to

Write a method named getNums:

Write a method named getNums which will fill an array with positive integers (including zero). The array which is to be filled should be passed to the method as a parameter. Your method should expect that the array would have already been created in the main program, and so the method does not have to create or initialize the array.

The method should read integers from the keyboard and place them into the array. The method should continue doing this until the array has been completely filled, or until the user enters a negative number (whichever happens first). The method should be able to handle an array of any size.

When the method finishes, it should return the number of integers that were actually stored in the array.

Write the main program:

Write a program that will read in a list of up to 8 positive integers (including zero) and store them into an array. After getting the numbers, the main method should display the number of items that were actually stored into the array.

Basically, the main program should create an array of size 8, call on the getNums method to fill the array, then print out a message stating how many numbers were stored into the array. If the array is completely filled, the main program should also display a message stating that the maximum size of the list has been reached.

You should also create a class constant (global constant) named MAXSIZE and initialize it to 8. Use this constant when printing your prompt to the user and also when initializing the array -- so that the number 8 is not used more than one time in your program.

The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's values are shown below in italics:

Enter a list of up to 8 positive integers. Enter a negative value to stop. 14 96 32 8 17 56 30 17 The maximum size of the list has been reached. The count of items entered is: 8

Here is another example:

Enter a list of up to 8 positive integers. Enter a negative value to stop. 14 96 -1 The count of items entered is: 2

Here is the starter code:

import java.util.*;

public class GetNumbers { public static Scanner kbd; public static void main(String[] args) { kbd = new Scanner(System.in); kbd.close(); } /** * Gets a list of positive integers (including zero) from the user, and places them * into an array which has been previously initialized. The method stops when the * user enters a negative integer, or when the array is full. * @param list The array into which the numbers will be placed. * @return The count of numbers which were placed into the array. */ public static int getNums(int [] list) { return 0; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How is the expected yield on most bonds determined?

Answered: 1 week ago

Question

2. Outline the functions of nonverbal communication

Answered: 1 week ago