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

  • in a c++ visual studio 2017 ...Write a program that simulates a lottery. The program should...

    in a c++ visual studio 2017 ...Write a program that simulates a lottery. The program should have an array of five integers named lottery and should generate a random number in the range 0 through 9 for each element in the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding element in the two arrays and keep a count of the digits that match. For...

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

  • Write a c++ complete program to meet the specifications. The program should prompt the user for...

    Write a c++ complete program to meet the specifications. The program should prompt the user for a positive integer. The program should print a message whether the integer is even or odd. The looping should end when the user enters a negative number. The negative number will not be tested for even or odd. The program will print out a message of how many numbers were entered (not counting the negative number) and how many even and odd numbers were...

  • In C++ write a program: Your goal is to ask record the sales for 5 different...

    In C++ write a program: Your goal is to ask record the sales for 5 different types of salsa, the total sales, and the names of the highest and lowest selling products. Your program should have the following:  The name of the program should be Assignment7.  3 comment lines (description of the program, author, and date).  Create a string array that stores five different types of salsas: mild, medium, sweet, hot, and zesty. The salsa names should...

  • C++ Your goal is to ask the user for a number and to determine if that...

    C++ Your goal is to ask the user for a number and to determine if that number is a prime number. Use a function to determine if the number is prime. Your program should have the following:  The name of the program should be Assignment 6.  3 comment lines (description of the program, author, and date).  Ask the user for an integer. Store this result in a variable with an appropriate name. (1 point)  Write a...

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

  • With this program you are going to design a math practice program for younger users. You...

    With this program you are going to design a math practice program for younger users. You will ask the user to enter two numbers. Only numbers may be entered. If the user enters a word, the program should disregard the entry and wait for an integer entry. There will not be another prompt if the user enters data other than whole numbers (integers). Here is a sample run: Your static methods should accept two integers and return an integer. Do...

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