1. Give us an example of declaring an array and then a loop that assigns a rando
ID: 3773838 • Letter: 1
Question
1. Give us an example of declaring an array and then a loop that assigns a random number from 20 to 200 to each array element - this should be a single statement in the loop body. End the short piece of code with a foreach construct to output those values.
2. Give us an example that declares and assigns values to an array for some set of values (6 or so) using the bottom of page 248 as an example.
3. Show us an example of a method header statement that accepts an array of type String and is of type String[].
Explanation / Answer
3.answer
3.
Arrays A string is a sequence of values of type char. What if we wanted a sequence of other types of values such as int, double, or String values? One could imagine restricted solutions to this problem. Java could provide an IntSequence type for defining sequences of int values. Similarly, it could define the types DoubleSequence, StringSequence, and so on. The problem with this approach is that no matter how many predefined sequence types we have, we might want a kind of sequence that was not anticipated by Java. For instance, what if we wanted a sequence of instances of the type Loan we defined earlier? This is not a type known to Java, so it cannot predefine such a sequence. Therefore, instead, it lets us, as programmers, define our own indexable sequences, called arrays, which can contain elements of some type specified by us. Like other values, they can be stored in variables of the appropriate type. The following declaration illustrates how an array variable and array value are
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.