Complete this partially written Java program that reverses the order of five num
ID: 3766813 • Letter: C
Question
Complete this partially written Java program that reverses the order of five numbers stored in an array. First the program should print the 5 numbers then pass the array to a method where the numbers are reversed and finally the program should print the reversed numbers.
// Reverse.java - This program reverses numbers stored in an array.
// Input: Interactive.
// Output: Original contents of array and the reversed contents of the array.
import javax.swing.*;
public class Reverse
{
public static void main(String args[])
{
int numbers[] = {9, 8, 7, 6, 5};
int x;
// Print contents of array
// Call reverseArray method here
// Print contents of reversed array
System.exit(0);
} // End of main() method.
// Write reverseArray method here.
} // End of Reverse class.
Explanation / Answer
Here is the code for you. If you need any further refinements, just get back to me.
// Reverse.java - This program reverses numbers stored in an array.
// Input: Interactive.
// Output: Original contents of array and the reversed contents of the array.
import javax.swing.*;
public class Reverse
{
public static void main(String args[])
{
int numbers[] = {9, 8, 7, 6, 5};
int x;
// Print contents of array
display(numbers);
// Call reverseArray method here
reverseArray(numbers);
// Print contents of reversed array
display(numbers);
System.exit(0);
} // End of main() method.
// Write reverseArray method here.
public static void reverseArray(int numbers[])
{
for(int i=0;i<numbers.length / 2;i++)
{
int temp = numbers[numbers.length-i-1];
numbers[numbers.length-i-1] = numbers[i];
numbers[i] = temp;
}
}
public static void display(int numbers[])
{
for(int i = 0; i < numbers.length; i++)
System.out.print(numbers[i]+" ");
System.out.println();
}
} // End of Reverse class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.