2 questions in Java. all grey lines of code can not be edited. must edit inbetween grey blocks of code.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

2 questions in Java. all grey lines of code can not be edited. must edit inbetween grey blocks of code. 

Jump to level 1
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
7
8
9
10
11
12
13
14
15
16
17
18}
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();
Transcribed Image Text:Jump to level 1 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 7 8 9 10 11 12 13 14 15 16 17 18} 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();
Jump to level 1
Integer numElements is read from input and integer array userScore is declared with size numElements. Then, numElements
integers are read from input and stored into userScore. Assign integer sumVals with the sum of the first element and the last
element of the array.
Ex: If the input is:
4
11 43 77 35
then the output is:
46
I Import Java.util.scanner;
2
3 public class ScoreLog {
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static void main(String[] args) {
Scanner scnr = new Scanner (System.in);
int numElements;
int[] userScore;
int i;
numElements = scnr. nextInt();
userScore = new int[numElements];
for (i = 0; i < userScore.length; ++i) {
userScore [i] = scnr.nextInt();
}
int sumVals = 0;
System.out.println(sumVals);
«
1
2
3
Transcribed Image Text:Jump to level 1 Integer numElements is read from input and integer array userScore is declared with size numElements. Then, numElements integers are read from input and stored into userScore. Assign integer sumVals with the sum of the first element and the last element of the array. Ex: If the input is: 4 11 43 77 35 then the output is: 46 I Import Java.util.scanner; 2 3 public class ScoreLog { 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public static void main(String[] args) { Scanner scnr = new Scanner (System.in); int numElements; int[] userScore; int i; numElements = scnr. nextInt(); userScore = new int[numElements]; for (i = 0; i < userScore.length; ++i) { userScore [i] = scnr.nextInt(); } int sumVals = 0; System.out.println(sumVals); « 1 2 3
Expert Solution
Step 1: Program Approach

Program - 1

  1. Import the Scanner class to read input from the user.

  2. Define the MileData class, which contains the main method where the program execution starts.

  3. Create a Scanner object named scnr to read input from the standard input (keyboard).

  4. Declare a constant NUM_ELEMENTS with a value of 8, which represents the number of elements in the yearlyMiles array.

  5. Declare an integer array yearlyMiles with a length of NUM_ELEMENTS (8 in this case) to store the user's input.

  6. Declare an integer variable i to be used as a loop counter.

  7. Use a for loop to read integers from the user and store them in the yearlyMiles array. The loop iterates from 0 to yearlyMiles.length - 1 (from 0 to 7) using the variable i. Inside the loop, it uses scnr.nextInt() to read an integer and stores it in yearlyMiles[i].

  8. After reading all the integers, use another for loop to print the integers in the first half of the yearlyMiles array in reverse order. The loop starts from the middle of the array (NUM_ELEMENTS / 2 - 1) and goes backward until it reaches the first element (0). Inside the loop, it prints each element with a hyphen separator. The condition if (i > 0) is used to avoid printing a hyphen after the last element.

  9. After printing all the integers in reverse order, move to a new line to separate the output.

  10. Close the main method and the MileData class.


Program - 2

  1. Import the Scanner class to allow you to read input from the user.
  2. The public static void main(String[] args) method is the entry point of the program.
  3. Initialize variables:
    • Scanner scr is used to read user input.
    • int numElements will store the number of elements in the array.
    • int[] userScore is an array to store the user's scores.
    • int i is a loop counter.
  4. Use the nextInt() method of the Scanner to read an integer from the user. This will determine the size of the userScore array.
  5. Use a for loop to read numElements integers from the user and store them in the userScore array.
  6. Calculate and print the sum of the first and last elements:
    • Calculate the sum of the first and last elements by accessing userScore[0] and userScore[numElements - 1].
    • Print the result using System.out.println().
steps

Step by step

Solved in 5 steps with 6 images

Blurred answer
Knowledge Booster
Properties of Different Architectures
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education