Java Code Write a method called listOfDigits(), which will accept two arguments
ID: 3844700 • Letter: J
Question
Java Code
Write a method called listOfDigits(), which will accept two arguments ( min and max ). The method will return an integer array that contains every digit between and including min and max, this method should implement a for loop, a while loop or a do while loop.
package finalexam;
import java.util.Scanner;
public class FinalExam
{
public static void main(String[] args)
{
Scanner stdin = new Scanner( System.in );
int min, max;
int[] array;
System.out.println( "Display Elements between Min and Max" );
System.out.print( "Enter Minimum: " );
min = stdin.nextInt();
System.out.print( "Enter Maximum: " );
max = stdin.nextInt();
array = listOfDigits( min, max );
System.out.print( "Output Array : " );
for ( int lp=0; lp<array.length; lp++ )
{
System.out.print( array[lp] + " " );
}
System.out.println();
}
}
Sample Execution
Explanation / Answer
import java.util.Scanner;
public class FinalExam {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int min, max;
int[] array;
System.out.println("Display Elements between Min and Max");
System.out.print("Enter Minimum: ");
min = stdin.nextInt();
System.out.print("Enter Maximum: ");
max = stdin.nextInt();
array = listOfDigits(min, max);
System.out.print("Output Array : ");
for (int lp = 0; lp < array.length; lp++) {
System.out.print(array[lp] + " ");
}
System.out.println();
}
//This method will return an array between min and max values
private static int[] listOfDigits(int min, int max) {
int arr[] = new int[max - min + 1];
int i = 0;
while (min <= max) {
arr[i++] = min++;
}
return arr;
}
}
__________________
Output:
Display Elements between Min and Max
Enter Minimum: 4
Enter Maximum: 9
Output Array : 4 5 6 7 8 9
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.