I could use a little help with this code... thanks! Directions: This exercise wi
ID: 3915022 • Letter: I
Question
I could use a little help with this code... thanks!
Directions:
This exercise will use command line args, arrays, and the Random class. Your Lab7.java program will take three values on the command line.
You will need to modify main so that the args from the comamnd line are converted to ints.
Fill in the method at the bottom. Do not modify the line of code that seeds the Random number generator.
-a small positive int representing the length of the array to be dimensioned.
-a small positive int representing the minimum value to be put into the array.
-a small positive int representing the maximum value to be put into the array.
Explanation / Answer
import java.io.*;
import java.util.*;
public class Lab7{
// Y O U M U S T M O D I F Y M A I N
public static void main( String args[] )
{
if(args.length == 3){
int dimension = Integer.parseInt(args[0]);
int lo = Integer.parseInt(args[1]);
int hi = Integer.parseInt(args[2]);
int[] array = new int[dimension];
randomFill( array, lo, hi ); // you write the code for this method below
printArray( array );
}
}
public static void printArray( int array[] )
{
System.out.printf("Array has %d values: ", array.length );
for( int i=0 ; i < array.length ; ++i )
System.out.print( array[i] + " " );
System.out.println();
}
// FILL THE ARRAY WITH RANDOM INTS
public static void randomFill( int array[], int lo, int hi )
{
Random rand = new Random( 17 ); // DO NOT REMOVE THIS LINE
for(int i = 0 ;i<array.length;i++)
array[i] = rand.nextInt((hi - lo) + 1) + lo;
// WRITE A LOOP THAT DOES THE FOLLOWING:
// FILLS THE ARRAY WITH RANDOM INTS.
// EACH RANDOM INT TACKED ONTO THE ARRAY MUST BETWEEN LO AND HI INCLUSIVE
}
} // END class
Have a Great Day :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.