1. Give a segment of code that will find the position, posLastZero,
of the last zero element among a[0], a[1], ..., a[n-1]. That is,
posLastZero should be the largest integer such that
a[posLastZero] is equal to zero. The value of n is given. If
there is no zero element among a[0], a[1], ..., a[n-1], the code
should set posLastZero equal to a negative one.
Example: n = 7 a[0] = 5
a[1] = 0
a[2] = 7
a[3] = 0
a[4] = 3
a[5] = 0
a[6] = 9
The code should leave posLastZero equal to 5 in this case, because
a[5] is the last zero element among a[0], a[1], ..., a[n-1].
This is only an example; your code must handle any case.
2. Copy the elements a[0], a[1], ..., a[n-1] into the array b[], but
rescale them by dividing them by a[0]. For example, if n=4,
a[0]=1.5, a[1]=9.0, a[2]=3.0, and a[3]=6.0 before the code is
executed, then afterwards the values of the elements of b()
should be b[0]=1.0, b[1]=6.0, b[2]=2.0, and b[3]=4.0.
Each element has been divided by the value, 1.5, of a[0].
This is only an example; of course your code must be general
and must handle any number of elements.
3. Given nA and a[0], a[1], ..., a[nA - 1], with the elements a[j] in
nondecreasing order (a[0] <= a[1] <= ... <= a[nA-1]), give a segment
of code that will "squeeze out the duplicates in-place" and
will set nA equal to the number of unique elements in a.
That is, if nA=5, a[0]=0.0, a[1]=0.0, a[2]=1.2, a[3]=2.3, and a[4]=2.3,
then after execution of the code the contents should be a[0]=0.0,
a[1]=1.2, and a[2]=2.3, and nA=3. The duplicate elements (the second
value of 0.0 and the second value of 2.3) have been eliminated from
the array.
Note:
(The final contents of a[3] and a[4] can be anything.)
This is only an example. Your code must be general and must not
contain any "magic numbers".
4. How should you declare your instance variables in a class?
5. Write a constructor for a class named Dog that has instance variables of
String name, String breed, and int age.