what is the correct java program for given problem? Write a method readData(...)
ID: 3820283 • Letter: W
Question
what is the correct java program for given problem?
Write a method readData(...) that reads and returns a list of integers. The list is preceded by the number of items in the list. For example, the data 6 9 7 5 3 1 2 indicates that there are 6 items in the list. The leading 6 is not included in the list bust specifying the size of the list. Write another method printList(..) that displays all elements and an array. Write a main method to test above methods by calling readData to read an array of integers and calling printList to display these integers.
Explanation / Answer
ReadDataTest.java
import java.util.Scanner;
public class ReadDataTest {
public static void main(String[] args) {
int a[] = readData();
printList(a);
}
public static int[] readData() {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the list of integers: ");
int size = scan.nextInt();
int a[] = new int[size];
for(int i=0; i<size; i++){
a[i] = scan.nextInt();
}
return a;
}
public static void printList(int a[]){
System.out.println("List of integers that you entered: ");
for(int i=0; i<a.length; i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
Output:
Enter the list of integers:
6 9 7 5 3 1 2
List of integers that you entered:
9 7 5 3 1 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.