Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [20pts]
    Ex2.javaEx3.java
    public class Ex2
    {
      public static void main(String[] args)
      {
        String s;
        System.out.println(s.length());
      }  
    }
    public class Ex3
    {
      public static void main(String[] args)
      {
        String s = null;
        System.out.println(s.length());
      }  
    }
    Try compiling and running this.
    Do you get a compile-time or run-time error?
    
    
    What is the problem?
    
    
    
    
    
    
    Try compiling and running this.
    Do you get a compile-time or run-time error?
    
    
    What is the problem?
    
    
    
    
    
    
  2. [5pts] Why is it that String s = null; is OK, but double x = null; is not?
     
  3. [75pts] Write a program (in Java!) that reads in number n, followed by n strings, then prints out the strings columnwise (with spaces between column elements). A sample run looks like this:
    ~/$ java HW02
    4 hello goodbye destiny alive
    h g d a 
    e o e l 
    l o s i 
    l d t v 
    o b i e 
      y n   
      e y   
    Rules: you must define and use in your program a function public static int maxLength(String[] A) that takes an array of Strings as an argument and returns the length of the longest string. If I were you, I would define and thoroughly test this function before doing anything else!
    Hints: Here are a few helpful tidbits.
Turn In a printout of this sheet (filled in), the source code for your HW solution, and a screenshot of it running on the above input.