Write a program, named last10.java, whose input is an unknown number of integers. The program stops reading when it encounters a -1. The program then prints the last ten numbers it read (not including the-1), in the order they were given. If less than 10 numbers were input, the program should print them all. For example, here are two screenshots of my program in action: The below screenshot shows an example where the user enters more 10 number. Input is shown on the first line (green), and the output is shown in black on the second line. 1 2 3 4 5 6 7 8 9 10 11 12 -1 3 4 5 6 7 8 9 10 11 12 Here is another example where the user enters less than 10 numbers. 1 2 3 4 5 6-1 1 2 3 4 5 6 Your program must not assume anything about the number of input values, which means that you can't declare an array to hold all of them. Instead, you should declare an array of 10 integers. In particular, keep a variable to the next open slot of the array. Use a Scanner to read an integer. If it is not -1, write it to that slot and increment the value. If incrementing the value puts it beyond the end of the array, set index of array back to the first slot. When you read a zero, print the contents of the array. Note that you need to begin with the number that was entered least recently, which is not necessarily the number at the beginning of the array. See the first example in the above screenshot. Some people will want to implement the answer like this: Once the array is full, you shift the values in the array one position to the left in order to make room the next input value, which you insert at the far right of the array. This is awkward and inefficient, and a bad idea. Please DON'T do it this way.

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

JAVA PROGRAM

Write a program, named last10.java, whose input is an unknown number of integers.
The program stops reading when it encounters a -1. The program then prints the last ten numbers
it read (not including the -1), in the order they were given. If less than 10 numbers were input, the
program should print them all. For example, here are two screenshots of my program in action:
The below screenshot shows an example where the user enters more 10 number. Input is shown
on the first line (green), and the output is shown in black on the second line.
1 2 3 4 5 6 7 8 9 10 11 12 -1
3 4 5 6 7 8 9 10 11 12
Here is another example where the user enters less than 10 numbers.
1 2 3 4 56-1
1 2 3 4 5 6
Your program must not assume anything about the number of input values, which means that you
can't declare an array to hold all of them. Instead, you should declare an array of 10 integers. In
particular, keep a variable to the next open slot of the array. Use a Scanner to read an integer. If it
is not -1, write it to that slot and increment the value. If incrementing the value puts it beyond the
end of the array, set index of array back to the first slot.
When you read a zero, print the contents of the array. Note that you need to begin with the number
that was entered least recently, which is not necessarily the number at the beginning of the array.
See the first example in the above screenshot.
Some people will want to implement the answer like this: Once the array is full, you shift the
values in the array one position to the left in order to make room the next input value, which you
insert at the far right of the array. This is awkward and inefficient, and a bad idea. Please DON'T
do it this way.
Transcribed Image Text:Write a program, named last10.java, whose input is an unknown number of integers. The program stops reading when it encounters a -1. The program then prints the last ten numbers it read (not including the -1), in the order they were given. If less than 10 numbers were input, the program should print them all. For example, here are two screenshots of my program in action: The below screenshot shows an example where the user enters more 10 number. Input is shown on the first line (green), and the output is shown in black on the second line. 1 2 3 4 5 6 7 8 9 10 11 12 -1 3 4 5 6 7 8 9 10 11 12 Here is another example where the user enters less than 10 numbers. 1 2 3 4 56-1 1 2 3 4 5 6 Your program must not assume anything about the number of input values, which means that you can't declare an array to hold all of them. Instead, you should declare an array of 10 integers. In particular, keep a variable to the next open slot of the array. Use a Scanner to read an integer. If it is not -1, write it to that slot and increment the value. If incrementing the value puts it beyond the end of the array, set index of array back to the first slot. When you read a zero, print the contents of the array. Note that you need to begin with the number that was entered least recently, which is not necessarily the number at the beginning of the array. See the first example in the above screenshot. Some people will want to implement the answer like this: Once the array is full, you shift the values in the array one position to the left in order to make room the next input value, which you insert at the far right of the array. This is awkward and inefficient, and a bad idea. Please DON'T do it this way.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Random Class and its operations
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