Gery blocks of code (lines 1-13 & 16-end) can NOT be edited. JAVA code needs to be added inbetween the grey blocks of code.
Gery blocks of code (lines 1-13 & 16-end) can NOT be edited.
JAVA code needs to be added inbetween the grey blocks of code.
![Given the integer array yearlyMiles with the size of NUM_ELEMENTS, write a for loop to output the integers in the first half of
yearlyMiles in reverse order. Separate the integers with a dash surrounded by spaces ("- ").
Ex: If the input is 110 46 31 69 103 83 70 60, then the output is:
69 31 46 110
1 import java.util.Scanner;
2
3 public class MileData {
4
5
6
7
8
9
10
1231415 16 17 18 10
19
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_ELEMENTS = 8;
int[] yearlyMiles = new int [NUM_ELEMENTS];
int i;
}
for (i = 0; i < yearlyMiles.length; ++i) {
yearlyMiles [i] = scnr.nextInt ();
}
for
System.out.println();
1
2
3](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F0cc153ae-b205-4fb2-9991-7cf6a21d5016%2F0c135283-12df-4787-b1b1-20577d94ebfd%2F0rtcynr_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Import the Scanner class to enable user input.
Define a class named
MileData
.In the
main
method:a. Create a
Scanner
objectscnr
to read input from the console.b. Declare a constant
NUM_ELEMENTS
with a value of 8, representing the number of elements to read.c. Create an integer array
yearlyMiles
with a length ofNUM_ELEMENTS
to store the user input.d. Declare an integer variable
i
for iteration.e. Start a
for
loop withi
initialized to 0, running untili
is less than the length ofyearlyMiles
:i. Inside the loop, read an integer from the user using
scnr.nextInt()
and store it in theyearlyMiles
array at indexi
.f. After the loop, a filled
yearlyMiles
array will contain the user's input.g. Start a second
for
loop with a new integer variablei
initialized toNUM_ELEMENTS / 2 - 1
, running as long asi
is greater than or equal to 0:i. Inside this loop, print the element in the
yearlyMiles
array at indexi
.ii. Check if
i
is greater than 0, and if so, print a hyphen ("-") as a separator.h. After the loop, a reverse order of the first half of the
yearlyMiles
array is printed with hyphens between the elements.Finally, print a newline character to end the output.
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)