Question

With your partner, brainstorm a program that will ask the user for a number of digits...

With your partner, brainstorm a program that will ask the user for a number of digits to be stored. The program should then create an array of that size and then prompt the user for numbers to fill each position in the array. When all of the positions are filled, display the contents of the array to the user.

Create a new Project named FillArray and a new class in the default packaged named FillArray.java. You and your partner should each submit a copy of your code to Carmen. Use the following template:

/**
* Short description of the program
* @author YOUR NAME HERE
* @author YOUR PARTNER'S NAME HERE
* @version DATE HERE
*/
import java.util.Scanner;

public class FillArray {
public static void main(String[] args) {
// Your code goes here
}
}
Below is a sample transcript of what your program should do. Text in bold is expected input from the user rather than output from the program.

Please enter the number of digits to be stored: 5
Enter integer 0: -1
Enter integer 1: 10
Enter integer 2: 15
Enter integer 3: -6
Enter integer 4: 3

The contents of your array:
Number of digits in array: 5
Digits in array: -1 10 15 -6 3

If the user provides a negative number of digits to be stored, the program should prompt the user to input a non-negative number of digits to store:

Please enter the number of digits to be stored: -1
ERROR! You must enter a non-negative number of digits!

Please enter the number of digits to be stored: 3
Enter integer 0: 0
Enter integer 1: -5
Enter integer 2: 16

The contents of your array:
Number of digits in array: 3
Digits in array: 0 -5 16
If the user asks to store 0 digits, the program should exit with a simple goodbye message:

Please enter the number of digits to be stored: 0
No digits to store? Goodbye!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**
* Short description of the program
* @author YOUR NAME HERE
* @author YOUR PARTNER'S NAME HERE
* @version DATE HERE
*/

import java.util.Scanner;

public class FillArray {

   public static void main(String[] args) {

       int n;

       Scanner inp = new Scanner(System.in); //Scanner object named "inp"

       System.out.print("\nPlease Enter the number of digits to be stored: ");

       n = inp.nextInt(); //Taking input for the size of n

       if(n<0){

           System.out.println("\nERROR! You must enter a non-negative number of digits!\n");

           return; //Exiting the program if size is negative integer

       }else if(n==0){

           System.out.println("\nNo digits to store? Goodbye!\n");

           return; //Exiting the program if size is 0

       }

       int a[] = new int[n];   //Array of size provided by the user.

       System.out.print("\n");

       for(int i=0; i<n; i++){

           System.out.print("Enter Integer "+i+": ");  

a[i] = inp.nextInt(); //Taking input for each index of array "a" by the user

           System.out.print("\n");
       }

       System.out.println("\nThe contents of your array:");

       System.out.println("Number of digits in array: "+n); //Printing the size of array

       System.out.print("Digits in array: "); //Printing the digits in array

       for(int i=0; i<n; i++){

           System.out.print(a[i]+" ");

       }

       System.out.println("\n");

   }

}

Add a comment
Know the answer?
Add Answer to:
With your partner, brainstorm a program that will ask the user for a number of digits...
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
  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • Name : StarryArrays Write a program called StarryArrays which prompts user for the number of items...

    Name : StarryArrays Write a program called StarryArrays which prompts user for the number of items in an array (a non-nega- tive integer), and saves it in an int variable called numltems. It then prompts user for the values of all the items (non-negative integers) and saves them in an int array called items. The program shall then print the contents of the array in a graphical form, with the array index and values represented by number of stars For...

  • This program will ask the user to enter a number of players, then ask the user...

    This program will ask the user to enter a number of players, then ask the user for each player's name and score. Once all of the players have been entered, the program will display a bar chart with each of their scores scaled as a percentage of the maximum score entered. See below for details on exactly how this should work - an example transcript of how the program should work is shown below (remember that values entered by the...

  • Write a program named ClassifyScores that classifies a series of scores entered by the user into...

    Write a program named ClassifyScores that classifies a series of scores entered by the user into ranges (0-9, 10-19, and so forth). The scores will be numbers between 0 and 100. Design and implement a program to prompt the user to enter each score separately. If the score is within the correct range, then increment the appropriate range If the score entered is greater than 100, display an error message, ignore the incorrect score, and prompt for the user to...

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

  • write a c++ program that prompts a user for a number then attempts to allocate an...

    write a c++ program that prompts a user for a number then attempts to allocate an array of as many integers as the user enters. In other words, the program might prompt the user with a line like: “How many integers would you like to allocate?” It should then allocate as many integers as the user enters. The program should then wait for the user to press enter before deleting the allocated array and quitting. We will use this time...

  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • Please write a java program that takes a large, user-entered number (usually 10 digits) and tallies...

    Please write a java program that takes a large, user-entered number (usually 10 digits) and tallies the 10 digits to be used in an array. Then, the method should tell the user how many of each number was input. For example, for the number 445903218, the program should say "2 4's, 1 5...," etc etc.

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