numbers.text
, then print out various
items of information about the array (see below). You should assume a maximum of 100 numbers.
Sample Test Run
For example if the file numbers.text
contains:
12.3 2.5 9.4 3.14 22.15 17 54.3 7.6the program should produce the following output:
The array: {12.3, 2.5, 9.4, 3.14, 22.15, 17.0, 54.3, 7.6} contains 100 elements of which 8 elements are being used The first element of the array is 12.3 The last element of the array is 7.6 and is at position 7 The middle element of the array is 3.14 and is at position 3 There are 92 empty elements in the arrayNotes:
arr.length
is not very useful; what we are really interested in are the number of values
actually read in
while
, there's no index variable to use as the subcript, as would be the case for a for loop
(BTW, the same is true for reading in with a trailer value, which aso uses a while loop)
size
), initialized to 0 — which is both the initial number
of elements actually placed into the array, as well as the location/index of the next element to be filled
size
and then increment size
size
is incremented
to 1
, which is both the number of elements currently in the array, as well as the next location
in which an element is to be placed.
size
(instead of arr.length
) since it is only the
elements from 0
… size-1
that are of interest to us.
length
field which is carried along with the array, size
is not part of the array
size
arr.length
instead of size
. yo'll very quickly see your
mistake (there will be a ton of elements with value 0 printed).
flight_manifest
contains names of passengers preceded by the class they are flying (first, business or econom —
see below for the exact format). Write a program that reads in the data — a line at a time (using nextLine
—
and prints out the passengers by class. You should assume a maximum of 100 passengers.
Sample Test Run
For example if the file flight_manifest
contains:
Business class: Bill Gates Economy: Gerald Weiss Economy: David Arnow First class: Warren Buffet Business class: Paula Whitlock First class: Jim Cox Economy: Dina Sokolthe program should produce the following output:
First class: Warren Buffet First class: Jim Cox Business class: Bill Gates Business class: Paula Whitlock Economy: Gerald Weiss Economy: David Arnow Economy: Dina SokolNotes:
nextLine
)
startWith
(look it up in the API for practice) that accepts another
String as an argument and returns true/false whethe the original String begins with the passed argument. For example, if we have:
String s = "Hello world";then the call
s.beginsWith("Hello")
returns true, while s.beginsWith("ell")
returns false.
startsWith
method, is there a … method ???.
While you're at the API looking up startsWith
, see what other methods there are and if any of them make 'sense' to you.
.length
)
beginsWith
); anticipating the coming lecture on Strings and their
methods
pairs.text
, and aligns them (my term) before writing them out to the
file aligned_pairs.text
. If the number of pairs read in contains more pairs in which the first number is less than the second, then the pairs
should aligned with first < second; otherwise they should be paired using first > second.
Sample Test Run
For example if the file numbers.text
contains:
1 4 10 5 12 14 22 27 25 10then after program execution, the file
aligned_pairs.text
should contain the following:
1 4 5 10 12 14 22 27 10 25Notes:
numbers.text
contains a header value followed by the corresponding number of integers.
Read the values int an array and print out the a sequence of subarrays, with the first subarray consisting of the entire array,
the second consisting of the second to next to last element, the third consisiting of the thitd to third from last element, and so on,
until a subarray of size one or two is printed.
Sample Test Run
For example if the file numbers.text
contains:
7 12 41 26 77 11 19 44the program should produce the following output:
{12, 41, 26, 77, 11, 19, 44} {... 41, 26, 77, 11, 19 ...} {... 26, 77, 11 ...} {... 77 ...}Notes:
print(int [] arr, int first, int last)
to print out the subarray
last
paramter).
{
to indicate there are elements
preceding the subarray in the array. Similary for subarrays in which the last element of the subarray is
not the last element of the array.
Sample Test Run
For example if the file numbers.text
contains:
7 12 41 26 77 11 19 44the program should produce the following output:
{12, 41, 26, 77, 11, 19, 44} {12 ...} {12, 41 ...} {12, 41, 26 ...} {12, 41, 26, 77 ...} {12, 41, 26, 77, 11 ...} {12, 41, 26, 77, 11, 19 ...} {12, 41, 26, 77, 11, 19, 44} {... 44} {... 19, 44} {... 11, 19, 44} {... 77, 11, 19, 44} {... 26, 77, 11, 19, 44} {... 41, 26, 77, 11, 19, 44} {12, 41, 26, 77, 11, 19, 44} {12 ...} {... 41 ...} {... 26 ...} {... 77 ...} {... 11 ...} {... 19 ...} {... 44} {12, 41 ...} {... 41, 26 ...} {... 26, 77 ...} {... 77, 11 ...} {... 11, 19 ...} {... 19, 44} {12, 41, 26 ...} {... 41, 26, 77 ...} {... 26, 77, 11 ...} {... 77, 11, 19 ...} {... 11, 19, 44} {12, 41, 26, 77 ...} {... 41, 26, 77, 11 ...} {... 26, 77, 11, 19 ...} {... 77, 11, 19, 44} {12, 41, 26, 77, 11 ...} {... 41, 26, 77, 11, 19 ...} {... 26, 77, 11, 19, 44} {12, 41, 26, 77, 11, 19 ...} {... 41, 26, 77, 11, 19, 44} {12, 41, 26, 77, 11, 19, 44} {12, 41, 26, 77, 11, 19, 44} {... 41, 26, 77, 11, 19 ...} {... 26, 77, 11 ...} {... 77 ...}Notes: