CSE 201 Homework 6


Read Section 4.5 in Chapter 4 and Sections 8.1-8.8 and 8.12 in Chapter 8 in the textbook, and then answer the following questions.

  1. What is the output of this code segment?
  2.     double [] a = {-10.2, 23.1, 41.7, -31.8, 0.0};
        double temp = a[0];
    
        for (int i = 1; i < a.length; i++) {
            if (a[i] > temp) {
                temp = a[i];
            }
        }
    
        System.out.println (temp);
    
  3. What does this method do? In one short English sentence describe the task accomplished by this method.
  4.     public static int foo(int [] a)
        {
            int temp = 0;
            for (int i = 0; i < a.length; i++) {
                if (a[i] >= 0) {
                    temp++;
                }
            }
            return temp;
        }
    
  5. What does this method do? In one short English sentence describe the task accomplished by this method.
  6.     public static char [] bar(String [] a)
        {
            char [] tmp = new char[a.length];
    
            for (int i = 0; i < a.length; i++) {
                if (a[i].length() > 0) {
                    tmp[i] = a[i].charAt(0);
                }
                else {
                    tmp[i] = ' ';
                }
            }
            return tmp;
        }
    
  7. Write a class/static method that takes an array of ints as a parameter and returns true if the number of positive values in the array is greater than then number of negative values, and false otherwise.
  8. 
           
  9. Write a class/static method that takes an array of ints as a parameter and returns an array of booleans (of the same length as the parameter array). For each element in the parameter array whose value is even, the corresponding element of the returned array will be assigned the value true; all the other elements of the returned array will be assigned the value false.
  10. 
           
  11. Write a class/static method that takes an array of Strings as a parameter and returns an array of Strings (of the same length as the parameter array) containing the elements of the parameter array truncated to the first 5 characters (or not truncated if not longer than 5 characters).