Page 461 - Introduction to Programming with Java: A Problem Solving Approach
P. 461
}
a) b)
// end class Reverse
reverse(simpsons);
System.out.println(
simpsons[0] + " " + simpsons[1] + " " + simpsons[2]);
} // end main
public static void reverse(String[] list)
{
}
// end reverse
String[] temp = new String[list.length];
for (int i=0; i<list.length; i++)
{
}
temp[i] = list[list.length-i-1];
list = temp;
What does the program print?
Fix the program by providing one or more lines of alternative code for the list = temp; line. You are not allowed to change any other code, just provide alternative code for that one line.
8. [after §10.6] Write a program that implements the example described at the beginning of Section 10.6. Your program should shift the array’s elements from position x to position x 1 as described in that section. (Move the value at position 1 to position 0; move the value at position 2 to position 1, and so on).
Start by creating two arrays, double[] initialHours and double[] hours. In its declarationinitializeinitialHourswiththevalues{8, 8, 6, 4, 7, 0, 0, 5},butdon’t
}
// end main
{
}
Apago PDF Enhancer
initialize hours when you declare and instantiate it with its 31 elements. Instead, initialize hours after its creation by using System.arraycopy to copy all the values in initialHours into the first elements in hours. Then perform one down-shift operation, and load zero into the (new) highest element.
9. [after §10.7] Write a class method named allPositive that receives an array named arr of double values and returns true if all the element values are positive and returns false otherwise. Use appropriate access modifiers. Make the method accessible from outside of its class.
10. [after §10.7] Assume that you have already successfully written a class named Students that handles student records for the Registrar’s office. Assume that the Students class:
• ContainsastudentIdsinstancevariable—anarrayofintsthatcontainsstudentIDnumbers. • Containsa1-parameterconstructorthatinitializesthestudentIdsinstancevariable.
• Containsthismainmethod:
public static void main(String[] args)
{
Students s1 = new Students(new int[]
Students s2 = new Students(new int[]
Students s3 = new Students(new int[]
if (s1.equals(s2))
{123, 456, 789});
{123, 456, 789, 555});
{123, 456, 789});
System.out.println("s1 == s2");
if (s1.equals(s3))
{
}
System.out.println("s1 == s3");
Exercises 427