Question

Assume the following declarations have been made in main(): int howmany; Scanner kb = new Scanner(System.in);...

Assume the following declarations have been made in main():

int howmany;

Scanner kb = new Scanner(System.in);

Random r = new Random();

Write code to do the following:

  • Use a while loop to prompt the user for how many numbers are to be entered. If the user does not enter a positive number (a number greater than 0), display a message saying the number must be positive and to try again. Store this value in the howmany variable.
  • Declare an integer array named nums, using the howmany variable for the length of the array.
  • Use a do-while loop and the random number generator to give all elements of the array values between 5 and 45.
  • Use a for loop to display the elements of the array in a single row, with each element separated by commas. There should not be a comma after the last element. Example: 8, 42, 13, 12, 19

Rules:

  • Submit only the code you are asked to write. You do not have to create a main program.
  • The code you submit must still follow all Java syntax rules and compile.
  • The howmany variable cannot be given an initial value in a single line of code, (howmany = 5; for example). It must be given a value only through user input.
  • You must use the exact type of loop in each part that requires a loop.
  • The test conditions for the last two loops must not contain any constant numbers.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Random;
import java.util.Scanner;

public class RandomGenerator {

   public static void main(String[] args) {
int howmany;
       Scanner kb = new Scanner(System.in);
       Random r = new Random();
       System.out.println("Enter the count of numbers to be entered");
       howmany=kb.nextInt();
       while(true){
           if(howmany<=0){
               System.out.println("Enter a positive number (a number greater than 0)");
               howmany=kb.nextInt();
           }
           else{
               break;
           }
       }
       int nums[]=new int[howmany];
       int i=0;
       do{
           int result = r.nextInt(45-5) + 5;
nums[i]=result;
i++;
}while(i<howmany);
         
       for(int j=0;j<nums.length;j++){
           System.out.print(nums[j]);
           if(j!=nums.length-1){
               System.out.print(",");
           }
          
       }

   }

}

Expected output:

Enter the count of numbers to be entered Enter a positive number (a number greater than o) 5 19,18,10,14,38

Add a comment
Know the answer?
Add Answer to:
Assume the following declarations have been made in main(): int howmany; Scanner kb = new Scanner(System.in);...
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
  • ​Declare an array with 1000 elements of type int Then in a loop generate 1000 random numbers and assign one to each element in the array The random numbers should be between 1 and 50 Then, prompt the user to enter a number, store this number in a local

    Rules to follow are:Declare an array with 1000 elements of type intThen in a loop generate 1000 random numbers and assign one to each element in the arrayThe random numbers should be between 1 and 50do not use rand or srand, use code is providedThen, prompt the user to enter a number, store this number in a local variable, the number should be safety checked using the GetInteger() functionThen, iterate through the array and determine how many times the user's...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • public static void main(String[] args) { int option=0;    while(option<8){ Scanner input=new Scanner(System.in); System.out.println("Welcome! Please enter...

    public static void main(String[] args) { int option=0;    while(option<8){ Scanner input=new Scanner(System.in); System.out.println("Welcome! Please enter your name"); String name=input.next(); Person user= new Person(name); System.out.println("Your id: "+user.getID()); System.out.println("Login successful"); System.out.println("------------------------------"); while(option!=7){ System.out.println("1.Create and host a new meeting"); System.out.println("2.Cancel a meeting"); System.out.println("3.Attend an existing meeting"); System.out.println("4.Leave a meeting"); System.out.println("5.Display my meetings"); System.out.println("6.Display meetings organized by me"); System.out.println("7.Logout"); System.out.println("8.Exit the app");    option=input.nextInt(); switch(option){ case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break;...

  • QUESTION 1 Using the following declarations and a member of the Array class, int [ ]...

    QUESTION 1 Using the following declarations and a member of the Array class, int [ ] bArray = new int [10]; int location; Using a method in the Array class, write a statement that would change the order of the elements in the bArray array. The contents of the first cell should hold what was in the last cell. The second cell should hold what was in the next to last cell. __________________________ HINT: You must write the full statement...

  • Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive...

    Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive integer numbers from 1 to 999 (lottery numbers) and takes a single input from the user and checks the input against the 15 lottery numbers. The user wins if her/his input matches one of the lottery numbers. Your implementation must have a level of modularity (using functions) Functions you need to implement: Define function initialize() that takes array lotterNumbers[] as a parameter and assigns...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • Consider the following code segment: int main() cons int SIZE -20; int values [SIZE] - f0,...

    Consider the following code segment: int main() cons int SIZE -20; int values [SIZE] - f0, 23, 34, -7, 110, 42,-350, 424, 25, 99, 10, 05, 50, -5, 1, 200, -350, 437, 25, 147}; // your code goes here Write a complete program to display the values from each of the following four steps All displays must be done in main. Use separate functions for each step below, passing the array to each function. 1.1) Provide the sum of the...

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

  • In this problem you'll get to use one of Java's random number generators, java.util.Random (see the...

    In this problem you'll get to use one of Java's random number generators, java.util.Random (see the docs for Random). This class generates a random double precision number uniformly distributed in the range [0.0 ... 1.0). That is, the numbers go from 0 to 1, including 0 but excluding 1. It is a common need in Java programming to generate a random integer number that's uniformly distributed in the range of [0 ... n), such as we saw in the dice...

  • write a java program that does the following Part one Use a For loop to compute...

    write a java program that does the following Part one Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic (Nested loops: Indent text) so that the...

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