Write a program with a main method that asks the user to enter an array of 10 in
ID: 3765252 • Letter: W
Question
Write a program with a main method that asks the user to enter an array of 10 integers. Your main method then calls each of the three methods described below and prints out the results of the two methods which return values.
printReverse - a void method that prints out the array in reverse of the way it was entered all on one line separated by commas (see sample output below).
getLargest - an int method that returns the largest value in the array.
computeTwice- an method that returns an array of int which contains two times of all the numbers in the array (see the sample output below).
Sample output:
Enter a number: 22
Enter a number: 34
Enter a number: 21
Enter a number: 35
Enter a number: 12
Enter a number: 4
Enter a number: 2
Enter a number: 3
Enter a number: 99
Enter a number: 81
Here are your numbers in reverse: 81, 99, 3, 2, 4, 12, 35, 21, 34, 22
The highest number is 99
The array with two times the numbers: [44, 68, 42, 70, 24, 8, 4, 6, 198, 162]
Explanation / Answer
/**
*
*/
/**
* @author srinu
*
*/
public class ArrayUtils {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int a[] = new int[10];
for (int i = 0; i < a.length; i++) {
System.out.print("Enter a number: ");
a[i] = scanner.nextInt();
}
ArrayUtils arrayUtils = new ArrayUtils();
arrayUtils.printReverse(a);
int max = arrayUtils.getLargest(a);
System.out.println(" The highest number is " + max);
int twiceA[] = arrayUtils.computeTwice(a);
System.out.print(" The array with two times the numbers: [");
for (int i = 0; i < twiceA.length; i++)
System.out.print(twiceA[i] + ",");
System.out.print("]");
}
/**
* @param a
*/
public void printReverse(int a[]) {
System.out.println(" Here are your numbers in reverse:");
for (int i = a.length - 1; i >= 0; i--)
System.out.print(a[i] + ",");
}
/**
* @param a
* @return
*/
public int getLargest(int a[]) {
int max = a[0];
for (int i = 1; i < a.length; i++) {
if (max < a[i]) {
max = a[i];
}
}
return max;
}
/**
* @param a
* @return
*/
public int[] computeTwice(int a[]) {
int[] twiceNums = new int[a.length];
for (int i = 0; i < a.length; i++) {
twiceNums[i] = a[i] * a[i];
}
return twiceNums;
}
}
OUTPUT:
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
Enter a number: 6
Enter a number: 7
Enter a number: 8
Enter a number: 9
Enter a number: 10
Here are your numbers in reverse:10,9,8,7,6,5,4,3,2,1,
The highest number is 10
The array with two times the numbers: [1,4,9,16,25,36,49,64,81,100,]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.